我做了一个对话框ID为IDD_ADDSHORTCUTS,又在菜单栏添加了一个按钮,响应按钮的Command函数为                    void CMainFrame::OnDrawAddshortcuts()。我想当点击此按钮时弹出我的对话框,代码如下,但为什么什么反应都没有呢?
void CMainFrame::OnDrawAddshortcuts()
{
// TODO: Add your command handler code here
CDialog dlg;
if(!dlg.Create(IDD_ADDSHORTCUTS,this))
AfxMessageBox("Error");
}
另外,我把代码改成如下:
void CMainFrame::OnDrawAddshortcuts()
{
// TODO: Add your command handler code here
CDialog dlg;
if(!dlg.Create(IDD_ADDSHORTCUTS,this))
;
AfxMessageBox("Error");
}
居然能出来!可是我点MessageBox的确定后就连同对话框一起消失了,这是怎么回事呢?
另外,我知道给对话框弄一个类,再用DoModal()可以出来对话框,但没有别的方法了么,create()为什么不行呢?
请大家指教。

解决方案 »

  1.   

    你这里CDialog dlg是局部变量,出了这个函数就被销毁了。
      

  2.   

    static CDialog dlg; 你函数结束 dlg 变量被系统回收了,不是没出来 是出来后马上消失了
      

  3.   

    void CMainFrame::OnDrawAddshortcuts()
    {
    // TODO: Add your command handler code here
    CDialog dlg;
    if(dlg.Create(IDD_ADDSHORTCUTS,this))
         dlg.ShowWindow(SW_SHOW);
    AfxMessageBox("Error");
      

  4.   

    当利用Create创建非模式对话框时,要调用ShowWindow函数将其显示:dlg.ShowWindow(SW_SHOW);
    dlg是局部变量,函数执行完后会销毁,应该定义成类的成员函数。或者将其定义为指针,再堆上分配内存:
     pDialog = new CMyDialog();
         if(pDialog != NULL)
       {
          BOOL ret = pDialog->Create(IDD_MYDIALOG,this);
          if(!ret)
            AfxMessageBox("Error creating Dialog");
          pDialog->ShowWindow(SW_SHOW);
       }
       else
          AfxMessageBox("Error Creating Dialog Object");