在客戶區雙擊出現打開對話框
工具/原料
Visual Studio 2010或者Visual C++6.0
方法/步驟
新建一個多重文檔的MFC應用,工程的名字為Open
在工作區選擇classview標籤,展開Open classes,右擊CMainFrame類,在彈出的菜單中選擇add WindowsMessage Handler 命令,在彈出的對話框中的新建windows消息/事件列表框中選擇WM_LBUTTONDBLCLK選項,單擊add and Edit
在OnLButtonDblClk()函數裡面添加代碼如下:
void CMainFrame::OnLButtonDblClk(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
AfxGetApp()->m_pDocManager->OnFileOpen();
CMDIFrameWnd::OnLButtonDblClk(nFlags, point);
}
在上面的OnCreate()函數中添加一下代碼
// 為響應鼠標雙擊客戶區的事件所增加的屬性:CS_DBLCLKS
HWND hMDIClientArea = GetWindow(GW_CHILD)->GetSafeHwnd();
::SetClassLong(hMDIClientArea, GCL_STYLE, ::GetClassLong(hMDIClientArea,GCL_STYLE) CS_DBLCLKS);
在工作區選擇classview標籤,展開Open classes,右擊CMainFrame類,在彈出的菜單中選擇add virtual function 命令。在彈出的對話框中的New virtual function列表框中選擇PreTranslateMessage,最後單擊add and edit
在函數PreTranslateMessage()添加一下代碼:
BOOL CMainFrame::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
// 如果是在主客戶區雙擊鼠標,則發送WM_LBUTTONDBLCLK消息
if(pMsg->hwnd == m_hWndMDIClient && pMsg->message == WM_LBUTTONDBLCLK)
PostMessage(WM_LBUTTONDBLCLK); // 發送鼠標雙擊消息
return CMDIFrameWnd::PreTranslateMessage(pMsg);
}
運行結果在客戶區雙擊出現打開對話框如下:
注意事項
注意函數的添加