1、获取鼠标位置(在屏幕的位置)
CPoint m_mouse;
GetCursorPos(&m_mouse);
2、 屏幕转化为客户端(控件的相对位置)& 客户端位置转化为屏幕位置
ClientToScreen(this->m_hwnd,m_mouse); //客户端位置转化为屏幕位置
ScreenToClient(this->m_hwnd,m_mouse) ; //屏幕转化为客户端
3、获取控件关于在屏幕的位置
CRect rc
GetWindowRect(&rc);
4、检测鼠标的action的方式多,自己用的多的是msdn上介绍的callback的鼠标钩子(低级钩子)函数来实现
1> 、对鼠标进行的几个全局变量进行声明:
将这部分放置在C++类的外部
/* 全局变量和全局函数定义 设定一个鼠标低级钩子变量 */ HHOOK mouse_Hook ; LRESULT CALLBACK LowLevelMouseProc ( INT nCode, WPARAM wParam, LPARAM lParam ); BOOL UninstallHook(); //卸载 BOOL InstallHook(); //安装
2>、在构造函数中初始化
1 mouse_Hook=NULL; //将钩子NULL
3>、在初始化函数中安装Hook
1 InstallHook()
4>、在析构函数中添加
UninstallHook()
5>、 在当前cpp的类外中定义声明的三个函数
1 BOOL InstallKbHook( ) 2 { 3 4 if (mouse_Hook) UninstallHook(); 5 mouse_Hook = SetWindowsHookEx(WH_MOUSE_LL, 6 (HOOKPROC)(LowLevelMouseProc), AfxGetApp()->m_hInstance, NULL); 7 8 return( mouse_Hook != NULL); 9 } 10 11 BOOL UninstallHook() 12 { 13 14 BOOL jud = FALSE; 15 if (mouse_Hook) { 16 jud= UnhookWindowsHookEx(mouse_Hook ); 17 mouse_Hook = NULL; //set NULL 18 } 19 20 return jud; 21 } 22 23 LRESULT CALLBACK LowLevelMouseProc (INT nCode, WPARAM wParam, LPARAM lParam) 24 { 25 CPoint _mousepoint; 26 MSLLHOOKSTRUCT *pkbhs = (MSLLHOOKSTRUCT *)lParam; 27 switch (nCode) 30 { 31 32 case HC_ACTION: 33 { 34 //鼠标左击 35 if(wParam == WM_LBUTTONDOWN||wParam == WM_RBUTTONDOWN||wParam == WM_MBUTTONDOWN||wParam ==WM_LBUTTONUP||wParam ==WM_RBUTTONUP||wParam == WM_MBUTTONUP){ 36 //获取鼠标的位置,并进行必要的判断 37 38 //鼠标右键按下 39 if(Mmdlg!=NULL){ 40 if( Mmdlg->setingmenu!=NULL&& Mmdlg->bo_back!=1) 41 { 42 //如果鼠标不在当前客户端位置之内 43 GetCursorPos(&_mousepoint); 44 CRect m_crect; 45 //Mmdlg->setingmenu->GetClientRect(m_crect); 46 Mmdlg->setingmenu->GetWindowRect(m_crect); 47 //ScreenToClient(Mmdlg->setingmenu->m_hWnd,&m_crect); 48 if(m_crect.bottom>_mousepoint.y && m_crect.top<_mousepoint.y && 49 m_crect.left<_mousepoint.x && m_crect.right >_mousepoint.x 50 ){ 51 ; //啥也不干 52 } 53 else{ 54 delete [] Mmdlg->setingmenu; 55 Mmdlg->setingmenu=NULL; 56 } 57 } 58 Mmdlg->bo_back=0; 59 } 60 } 61 } 62 default: break; 63 } 64 65 return CallNextHookEx (NULL, nCode, wParam, lParam); 66 }
原文:http://www.cnblogs.com/gongxijun/p/5043825.html