如题,我自己按书上的方法写了一个小程序,目的是练习在菜单中调出一个无模式对话框,
对话框的类名为CTestDlg
相关的代码如下:
void CTestDlg::Create()        //这是我自己加的一个成员函数,是照书写的

  CDialog::Create(CTestDlg::IDD);//这一句是照书写的,我还没太明白它的作用
}CTestDlg::CTestDlg(CWnd* pParent /*=NULL*/)    //默认的构造函数
: CDialog(CTestDlg::IDD, pParent)

   //代码为自动生成的,没有改动,故省略
}
在CView里添加了一个CTestDlg的指针,并包含了头文件"TestDlg.h"
class CTestView : public CView
{
protected:
  CTestDlg* pDlg; 
};cview中相关代码如下
CTestView::CTestView()
{
  // TODO: add construction code here
  pDlg = new CTestDlg((CWnd*)this);
}
CTestView::~CTestView()
{
  delete pDlg;
}
void CTestView::OnTestDlg()           //菜单响应函数
{
  // TODO: Add your command handler code here
  if (pDlg->GetSafeHwnd()==0)
  {
    pDlg->Create();
  }
}
编译无错误,但运行时点菜单后对话框并没显示出来,请问应该如何修改?

解决方案 »

  1.   

    楼上的,是pDlg->ShowWindow(WM_SHOWWINDOW)吧
    我加了,还是不好用:-(
      

  2.   

    CMyDialog* pDialog;void CMyWnd::OnSomeAction()
    {
       //pDialog initialized to NULL in the constructor of CMyWnd class
       pDialog = new CMyDialog();
       //Check if new succeeded and we got a valid pointer to a dialog object
       if(pDialog != NULL)
       {
          BOOL ret = pDialog->Create(IDD_MYDIALOG,this);
          if(!ret)   //Create failed.
             AfxMessageBox("Error creating Dialog");
          pDialog->ShowWindow(SW_SHOW);
       }
       else
          AfxMessageBox("Error Creating Dialog Object");
    }
      

  3.   

    pDialog->Create(IDD_MYDIALOG,this);这是关键◎
      

  4.   

    我也找到关键了:-)
    应该是
        pDlg->ShowWindow(SW_SHOW);另外,也请教一下,
      if (pDlg->GetSafeHwnd()==0)这一句的道理是什么?为什么要判断指针是否为空呢?
      

  5.   


    输入Create()的两个参数,第一个是对话框资源号,第二个是父窗口指针;
    然后调用pDlg->ShowWinddow(SW_SHOW);刚才打错了,不好意思
      

  6.   

    if (pDlg->GetSafeHwnd()==0)这个是判断当前指针指向的窗口句柄是否为NULL,如果不为空,直接ShowWindow就可以了
      

  7.   

    哦,就是说如果为空的话还要先create是吗?