zoukankan      html  css  js  c++  java
  • VC非模式对话框创建和销毁

    http://apps.hi.baidu.com/share/detail/11053326

    非模态对话框相对于模态对话框,他的创建和销毁过程和模态对话框有一定的区别

    先看一下MSDN的原文:

    When   you   implement   a   modeless   dialog   box,   always   override   the   OnCancel   member   function   and   call   DestroyWindow   from   within   it.   Don’t   call   the   base   class   CDialog::OnCancel,   because   it   calls   EndDialog,   which   will   make   the   dialog   box   invisible   but   will   not   destroy   it.   You   should   also   override   PostNcDestroy   for   modeless   dialog   boxes   in   order   to   delete   this,   since   modeless   dialog   boxes   are   usually   allocated   with   new.   Modal   dialog   boxes   are   usually   constructed   on   the   frame   and   do   not   need   PostNcDestroy   cleanup.

    MS的指示:非模态对话框需要重载函数OnCanel,并且在这个函数中调用DestroyWindow。并且不能调用基类的OnCancel,因为基类的OnCancel调用了EndDialog这个函数,这个函数是针对模态对话框的。
    还有一个必须重载的函数就是PostNcDestroy,这也是一个虚函数,通常的非模态对话框是用类的指针,通过new创建的,这就需要在PostNcDestroy函数中delete掉这个指针。

    了解了理论过后,下面我们就可以用代码实现一下非模态对话框的创建和销毁过程:
    //建立
    //主框架中:

    if (NULL==m_pDlg||m_pDlg->m_bNeedCreateDlg)
    {

    CTestDlg *m_pDlg=new CTestDlg;
    m_pDlg->Create(IDD_TESTDLG,this);
    m_pDlg->ShowWindow(SW_SHOW);

    }

     

    CTestDlg 添加成员变量

    bool m_bNeedCreateDlg = true;  如果 CTestDlg 对象 m_pDlg 本身是一个成员变量,当对话框关闭后m_pDlg指针不会自动的变为 NULL,如果要唯一如果有if (NULL==m_pDlg)判断将无法执行

                                                       所以必须有一个判断变量来实现判断


    //对话框中:
    void CTestDlg::OnCancel()
    {
         DestroyWindow();
    }

    void CTestDlg::PostNcDestroy()
    {
         CDialog::PostNcDestroy();
         delete this;

          m_bNeedCreateDlg = true;
    }

    void CTestDlg::OnInitDialog()
    {
         m_bNeedCreateDlg = false;

    }


    如果要在点击按钮的情况下,销毁非模态对话框,只需要把按钮的事件映射到OnCancel函数即可。

    另外注意一个问题:

  • 相关阅读:
    NFine框架JqGrid导出选中行为Excel实现方法
    NFine框架全选checkBox列错位
    VS 在文件中查找替换界面死掉。
    WCF各个Service之间共享数据
    Devexpress Winform 使用MVVM
    FontAwesome图标选择器
    Xampp PHPStorm XDebug配置
    SDL 库 无法解析的外部符号 __imp__fprintf
    ffmpeg mp4转yuv
    JAVA环境变量配置
  • 原文地址:https://www.cnblogs.com/carl2380/p/1927645.html
Copyright © 2011-2022 走看看