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.
  • 相关阅读:
    【整理】七大查找算法
    centos GUI界面与命令行的切换
    BogoMIPS与calibrate_delay
    Printk与sched_clock_init的一点分析
    系统启动 之 Linux系统启动概述(2)
    Linux Bootup Time
    系统启动 之 Linux系统启动概述(1)
    如何参与Linux内核开发(转)
    如何开始参与开源项目?
    非编程天才参与开源项目的14种方式(转)
  • 原文地址:https://www.cnblogs.com/taoxu0903/p/1491289.html
Copyright © 2011-2022 走看看