zoukankan      html  css  js  c++  java
  • 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.
  • 相关阅读:
    Flask_之使用过程中出现的异常
    GO-学习之golang 自动下载所有依赖包
    数据库-Oracle如何将clob数据以字符串打印出来
    数据库-Oracle条件判断语句
    python框架Django汇总
    每日一模块_os及subprocess模块【执行系统命令】
    每日一模块-sftp的使用
    python并发编程-队列MQ的学习
    python高效配置-响应code枚举类设计
    git-使用大全
  • 原文地址:https://www.cnblogs.com/taoxu0903/p/1491289.html
Copyright © 2011-2022 走看看