当前位置: 代码迷 >> 综合 >> VC让鼠标指到按钮上变成手型
  详细解决方案

VC让鼠标指到按钮上变成手型

热度:86   发布时间:2024-01-16 12:52:47.0
两种方法: 
1、派生一个CButton类,然后重载OnSetCursor函数,在里面 
使用SetCursor函数设置鼠标指针。 
然后删除 
return CButton::OnSetCursor(pWnd, nHITTest, message); 
改成 
return TRUE; 例如:
BOOL   CMyButton::OnSetCursor(CWnd*   pWnd,   UINT   nHITTest,   UINT   message)     {   ::SetCursor(::LoadCursor(NULL,   MAKEINTRESOURCE(IDC_HAND)));   return   TRUE;   } 2、直接在程序里,响应WM_MOUSEMOVE消息,在里面判断鼠标指针是否指到了按钮上,是的话就用SetCursor函数设置鼠标指针,离开再设回以前的指针
 
  
[cpp] view plaincopy
print?
  1. void CbpmDlg::OnMouseMove(UINT nFlags, CPoint point)  
  2. {  
  3.     // TODO: 在此添加消息处理程序代码和/或调用默认值  
  4.     CRect adbanerRect;  
  5.     CRect  btnSelectPathRect;  
  6.     CRect  btnStartRect;  
  7.     m_GifPic.GetWindowRect(&adbanerRect);  
  8.     m_btnSelectPath.GetWindowRect(&btnSelectPathRect);  
  9.     m_btnStart.GetWindowRect(&btnStartRect);  
  10.   
  11.     //如何鼠标在广告位置或按钮上,改变鼠标指针  
  12.     if(adbanerRect.PtInRect(point)||btnSelectPathRect.PtInRect(point)||btnStartRect.PtInRect(point))  
  13.     SetCursor(AfxGetApp()->LoadStandardCursor(IDC_HAND));  
  14.   
  15.     CDialog::OnMouseMove(nFlags, point);  
  16. }  
光有上面一个函数还是不行的,还需要重载CWnd类的虚拟函数PreTranslateMessage()
 
  
[cpp] view plaincopy
print?
  1. BOOL CbpmDlg::PreTranslateMessage(MSG *pMsg)  
  2. {  
  3.     if(pMsg->message == WM_MOUSEMOVE)  
  4.     {  
  5.         OnMouseMove(pMsg->wParam,pMsg->pt);  
  6.     }  
  7.     return CDialog::PreTranslateMessage(pMsg);  
  8. } 

  相关解决方案