问题清晰贴:无法准确判断鼠标是否移动到了按钮上?
之前发过类似的帖子,这次想把问题说的简单明了一点,方便大家帮我找出问题的所在,谢谢!!!
在对话框上动态创建一个按钮,然后判断鼠标是否移动到按钮上,如果移动到的话,在对话框上做一些事情
PS:不想单独在按钮的派生类中做这件事!!!
具体代码如下:
- C/C++ code
void CButtonEx::OnMouseMove(UINT nFlags, CPoint point){ ClientToScreen( &point ); CRect rectBtn; GetWindowRect( &rectBtn ); if ( rectBtn.PtInRect( point ) ) { m_bInBn = true; } else { m_bInBn = false; } CButton::OnMouseMove(nFlags, point);}void Ctest2Dlg::OnMouseMove(UINT nFlags, CPoint point){ if ( NULL != m_pBn ) { if ( m_pBn->m_bInBn ) { MessageBox( "在按钮上了!" ); } } CDialog::OnMouseMove(nFlags, point);}问题:当m_bInBn为true的时候,没有弹出MessageBox?但竟然会在离开按钮的时候会弹出。。。见鬼了!!!
[解决办法]
BOOL CxxxxDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
if(pMsg->message==WM_MOUSEMOVE)
{
CRect rc;
CButton *pBt=(CButton *)GetDlgItem(IDC_BUTTON1);
pBt->GetWindowRect(&rc);
if(rc.PtInRect(pMsg->pt))//不要用AfxMessageBox()!
afxDump << "In Button1\n";
else
afxDump << "Out of Button1\n";
}