在PreTranslateMessage(MSG* pMsg)中調用DoModal()模態窗口如下:
1 BOOL CMainDlg::PreTranslateMessage(MSG* pMsg)
2 {
3 // TODO: Add your specialized code here and/or call the base class
4 if ( pMsg->message == WM_LBUTTONDOWN)
5 {
6 GetWindowRect(m_oldRect);
7 ::SetCapture(this->m_hWnd);
8 m_bCanDrag = TRUE;
9 m_lastPt = pMsg->pt ;
10 }
11 else if ( pMsg->message == WM_LBUTTONUP)
12 {
13 if( m_bCanDrag )
14 {
15 ::ReleaseCapture();
16 m_bCanDrag = FALSE;
17 GetWindowRect(m_newRect);
18 if (m_oldRect.EqualRect(m_newRect))
19 {
20 GetMainItemID(pMsg);//調用對話框函數
21 //return TRUE;
22 }
23 }
24 }
25 else if( pMsg->message == WM_MOUSEMOVE)
26 {
27 if( m_bCanDrag )
28 {
29 CRect rc;
30 GetWindowRect(&rc);
31 rc.OffsetRect( pMsg->pt.x - m_lastPt.x , pMsg->pt.y - m_lastPt.y ) ;
32 m_lastPt = pMsg->pt;
33 this->MoveWindow( rc );
34 }
35 }
36
37 return CDialog::PreTranslateMessage(pMsg);
38 }
39 void CMainDlg::GetMainItemID(MSG* pMsg)
40 {
41 if (pMsg->hwnd == GetDlgItem( IDC_BTN_MYCOMPUTER )->m_hWnd)
42 {
43 CTestDlg dlg;
44 dlg.DoModal();
45 }
46 }
再單擊對話框上的按鈕時發送斷言中斷,具體位置如下:
::IsWindow(m_hWnd)
函數功能:該函數確定給定的
窗口句柄是否標識一個已存在的窗口。
函數原型:BOOL IsWindow(HWND hWnd);
參數:
hWnd:被測試窗口的句柄。
返回值:如果
窗口句柄標識了一個已存在的窗口,返回值為非零;如果窗口句柄未標識一個已存在窗口,返回值為零。
可能原因:在PreTranslateMessage里的獲取對應m_hWnd,DoModal()模態對話框退出后,m_hWnd不是有效的窗口句柄。解決辦法:處理完WM_LBUTTONUP后,需要返回TRUE。
posted on 2013-12-05 10:58
王海光 閱讀(2463)
評論(0) 編輯 收藏 引用 所屬分類:
MFC