您的位置:首页 > 其它

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

2016-11-27 21:52 260 查看
两种方法:
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 plain copy print?void CbpmDlg::OnMouseMove(UINT nFlags, CPoint point)  {      // TODO: 在此添加消息处理程序代码和/或调用默认值      CRect adbanerRect;      CRect  btnSelectPathRect;      CRect  btnStartRect;      m_GifPic.GetWindowRect(&adbanerRect);      m_btnSelectPath.GetWindowRect(&btnSelectPathRect);      m_btnStart.GetWindowRect(&btnStartRect);        //如何鼠标在广告位置或按钮上,改变鼠标指针      if(adbanerRect.PtInRect(point)||btnSelectPathRect.PtInRect(point)||btnStartRect.PtInRect(point))      SetCursor(AfxGetApp()->LoadStandardCursor(IDC_HAND));        CDialog::OnMouseMove(nFlags, point);  }  
光有上面一个函数还是不行的,还需要重载CWnd类的虚拟函数PreTranslateMessage()
[cpp] view plain copy print?BOOL CbpmDlg::PreTranslateMessage(MSG *pMsg)  {      if(pMsg->message == WM_MOUSEMOVE)      {          OnMouseMove(pMsg->wParam,pMsg->pt);      }      return CDialog::PreTranslateMessage(pMsg);  } 

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: