C + + gets mouse position and global detection mouse behavior

Source: Internet
Author: User

1. Get the mouse position (in the position of the screen)

CPoint M_mouse;

GetCursorPos (&m_mouse);

2. Screen conversion to client (relative position of control) & client location to screen position

ClientToScreen (This->m_hwnd,m_mouse); Client location converted to screen location

ScreenToClient (This->m_hwnd,m_mouse); Screen conversion to Client

3. Get control about the position on the screen

CRect RC

GetWindowRect (&RC);

4, the way to detect the mouse action, many of their own is the MSDN on the introduction of the callback mouse hook (low-level hook) function to achieve

1>, a few global variables of the mouse are declared:

Place this part outside of the C + + class

/*    Global variables and global function definitions   set a mouse low-level hook variable */  hhook Mouse_hook;  LRESULT CALLBACK Lowlevelmouseproc (INT nCode,                                     WPARAM WPARAM,                                      LPARAM LPARAM                                   );  BOOL Uninstallhook ();   // Uninstall   BOOL Installhook ();     // installation

2>, initializing in constructors

1 mouse_hook=null;   // The hook is null

3>, installing hooks in the initialization function

1 installhook ()

4>, adding in destructors

Uninstallhook ()

5>, defining three functions declared in a class outside the current CPP

1 BOOL Installkbhook ()2 {  3 4     if(Mouse_hook) Uninstallhook (); 5Mouse_hook =SetWindowsHookEx (Wh_mouse_ll,6(HOOKPROC) (Lowlevelmouseproc), AfxGetApp ()m_hinstance, NULL); 7 8     return(Mouse_hook! =NULL); 9 }  Ten  One BOOL Uninstallhook () A {   -  -BOOL Jud =FALSE;  the     if(mouse_hook) { -Jud=UnhookWindowsHookEx (Mouse_hook);  -Mouse_hook =NULL; Set NULL -     }   +  -     return Jud;  + }  A  at LRESULT CALLBACK Lowlevelmouseproc (INT nCode, WPARAM WPARAM, LPARAM LPARAM) - {   - CPoint _mousepoint; -Msllhookstruct *PKBHS = (msllhookstruct *) LParam;  -     Switch(NCode) -     {   to  +      Casehc_action: -         {   the             //left mouse click *             if(WParam = = wm_lbuttondown| | WParam = = wm_rbuttondown| | WParam = = wm_mbuttondown| | WParam ==wm_lbuttonup| | WParam ==wm_rbuttonup| | WParam = =wm_mbuttonup) {   $                     //get the position of the mouse and make the necessary judgmentsPanax Notoginseng                  -                     //Right-click the mouse button the                 if(mmdlg!=NULL) { +                     if(mmdlg->setingmenu!=null&& mmdlg->bo_back!=1) A                     { the                         //if the mouse is not within the current client location +GetCursorPos (&_mousepoint); - CRect M_crect; $                         //Mmdlg->setingmenu->getclientrect (m_crect); $Mmdlg->setingmenu->GetWindowRect (m_crect); -                         //screentoclient (mmdlg->setingmenu->m_hwnd,&m_crect); -                         if(M_crect.bottom>_mousepoint.y && M_crect.top<_mousepoint.y && theM_crect.left<_mousepoint.x && m_crect.right >_mousepoint.x -                           ){Wuyi;//I'm not doing anything . the                         } -                         Else{ Wu                             Delete[] mmdlg->Setingmenu; -mmdlg->setingmenu=NULL; About                         } $                     } -mmdlg->bo_back=0; -                 } -             } A         } +     default: Break;  the     }   -  $     returnCallNextHookEx (NULL, NCode, WParam, LParam);  the}

        

C + + gets mouse position and global detection mouse behavior

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.