父窗口PreTranslateMessage里过滤WM_LBUTTONDOWN消息利用
BOOL CXXDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
if(WM_LBUTTONDOWN == pMsg->message)
{
CPoint pt;
GetCursorPos(&pt);
ScreenToClient(&pt);
CWnd* pWnd = ChildWindowFromPoint(pt, CWP_ALL);
if(pWnd)
{
CString str;
pWnd->GetWindowText(str);
AfxMessageBox(str);
}
}
return CDialog::PreTranslateMessage(pMsg);
}
BOOL CXXDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
if(WM_LBUTTONDOWN == pMsg->message)
{
CPoint pt;
GetCursorPos(&pt);
ScreenToClient(&pt);
CWnd* pWnd = ChildWindowFromPoint(pt, CWP_ALL);
if(pWnd)
{
CString str;
pWnd->GetWindowText(str);
AfxMessageBox(str);
}
}
return CDialog::PreTranslateMessage(pMsg);
}
先是获取edit控件相对于父窗体的CRECT,判断点击坐标是否在里面,如果是则按F10显示edit内容
{
//获取鼠标坐标
CPoint pt;
GetCursorPos(&pt);
ScreenToClient(&pt);
//获取edit与combobox的rect
CRect rectEdit, rectCombo;
m_edit.GetWindowRect(&rectEdit); //获取edit相对于屏幕的位置
ScreenToClient(rectEdit); //转化为对话框上的相对位置 m_combo.GetWindowRect(&rectCombo); //获取combobox相对于屏幕的位置
ScreenToClient(rectCombo); //转化为对话框上的相对位置 if(rectEdit.PtInRect(pt))
{//如果鼠标在Edit控件范围之内
if (pMsg->wParam == VK_F10)
{//如果按下F10键,弹出Edit里面的字符串
CString str;
m_edit.GetWindowText(str);
AfxMessageBox(str);
}
}
else if (rectCombo.PtInRect(pt))
{//如果鼠标在combobox控件范围之内
if ( pMsg->wParam == VK_F10)
{//如果按下F10键,弹出combobox里面的字符串
CString str;
m_combo.GetWindowText(str);
AfxMessageBox(str);
}
} return CDialog::PreTranslateMessage(pMsg);
}
m_edit、m_combo是控件的变量