CDialog::DoModal

Model Dialog in Win32 c++
Creating model dialog is very easy in MFC. It has neat class for dialog , CDialog. The DoModel functions of CDialog provides a way to appear model dialog.
 
In Win32 this is another story. Many people doesn't know what is a model dialog. Some may thought it is a special type of dialog, like the different window classes(EDIT,BUTTON etc) . Actually model dialog is just a modeless dialog with some additions. Creating a model dialog requires to create modeless dialog ( using CreateDialog ,or even CreateWindow(wnd) wnd = dialog class name).
 
Following is a sample class , which will show a modle dialog in win32, (Just like the CDialog::DoModal() in MFC).
 
class name CBaseDlg, In this class i wrote a function DoModel, see below.
The MyDlgProc is the dialog procedure function . In this procedure you can see that it calls the CBaseDialog::DialogProc function (blue color). So the derived class of CBaseDlg can override this function and add their command handlers.
 
LRESULT CALLBACK MyDlgProc(HWND hDlg,UINT nMsg,WPARAM wParam, LPARAM lParam)
{
     CBaseDlg* pWnd = NULL;
     pWnd = CBaseDlg::FromHandle(hDlg);
     if(pWnd)
            return pWnd->DialogProc(hDlg,nMsg,wParam,lParam);
     return FALSE;
}
 
void
CBaseDlg::DoModel(HINSTANCE hInst,HWND hParent,UINT nResourceID)
{
     HWND hwnd = CreateDialog(hInst,(LPCTSTR)nResourceID, hParent, (DLGPROC)MyDlgProc);
     HwndMaps.insert(std::pair<HWND,CBaseDlg*>(hwnd,this) ); //Add this hwnd for using in MyDlgProc.
    
     ShowWindow(hwnd,SW_SHOWNORMAL); //show dialog.
     SendMessage(hwnd,WM_INITDIALOG,0,0); ///send INIT_DIALOG message as usual.
     EnableWindow(hParent,FALSE);//Disable parent window
 
     MSG msg;
     while(GetMessage(&msg,0,0,0))
     {
         TranslateMessage(&msg);
        DispatchMessage(&msg);
      }
 
      EnableWindow(hParent,TRUE); 
      DestroyWindow(hwnd); //Destroy me
      HwndMaps.erase(HwndMaps.begin(),HwndMaps.end()); //Clear the window map , this is just like the MFC way in win32.
}
 
Here in DoModel Passing the parent window handle and resource id , It will show a model dialog. The inside message loop will prevent the function from returning . The parent window should be disable , otherwise still user can do on parent window. So by disabling the window , the only active window is the dialog.

转载于:https://www.cnblogs.com/taoxu0903/archive/2009/05/28/1491289.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值