1. 程式人生 > >Duilib 模態對話方塊和非模態對話方塊

Duilib 模態對話方塊和非模態對話方塊

  1. void CMainDlg::DoTask()  
  2. {  
  3.     CTaskDlg * pDlg = NULL;  
  4.     pDlg = new CTaskDlg(XML_FILE_NAME_TASK_DLG, WND_CLASS_NAME_TASK_DLG);  
  5.     pDlg->Create(this->m_hWnd, MAIN_RORG_DISP_NAME, UI_WNDSTYLE_FRAME, WS_EX_WINDOWEDGE); ///< 如果引數1是NULL, 為桌面, 子視窗就不是模態對話方塊
  6.     pDlg->CenterWindow();  
  7.     pDlg->ShowModal(); ///< 彈出的是模態視窗
  8. }  
pDlg->Create 的引數1,開始寫成了NULL, 子視窗彈出後, 還可以操作主視窗.

因為要彈出模態對話方塊, 跟進 pDlg->ShowModal(),  看到了duilib禁止主視窗的程式碼,  才想到引數1應為父視窗視窗控制代碼.

可以看出, 當要彈出非模態視窗時, 可以將pDlg->Create 的引數1 填成 NULL.


  1. UINT CWindowWnd::ShowModal()  
  2. {  
  3.     ASSERT(::IsWindow(m_hWnd));  
  4.     UINT nRet = 0;  
  5.     HWND hWndParent = GetWindowOwner(m_hWnd); 
    ///< 如果子視窗建立時,引數1為NULL, 這裡得到的 hWndParent 就為 NULL
  6.     ::ShowWindow(m_hWnd, SW_SHOWNORMAL);  
  7.     ::EnableWindow(hWndParent, FALSE); ///< 當 (NULL == hWndParent) 時, EnableWindow 不生效, 導致彈出的是非模態視窗.
  8.     MSG msg = { 0 };  
  9.     while( ::IsWindow(m_hWnd) && ::GetMessage(&msg, NULL, 0, 0) ) {  
  10.         if
    ( msg.message == WM_CLOSE && msg.hwnd == m_hWnd ) {  
  11.             nRet = msg.wParam;  
  12.             ::EnableWindow(hWndParent, TRUE);  
  13.             ::SetFocus(hWndParent);  
  14.         }  
  15.         if( !CPaintManagerUI::TranslateMessage(&msg) ) {  
  16.             ::TranslateMessage(&msg);  
  17.             ::DispatchMessage(&msg);  
  18.         }  
  19.         if( msg.message == WM_QUIT ) break;  
  20.     }  
  21.     ::EnableWindow(hWndParent, TRUE);  
  22.     ::SetFocus(hWndParent);  
  23.     if( msg.message == WM_QUIT ) ::PostQuitMessage(msg.wParam);  
  24.     return nRet;  
  25. }