Hook under VC

Source: Internet
Author: User

Recently used in a single documentProgramWhen the non-customer area of the view implements a mouse click function, it finds a strange phenomenon: On win7, non-customer areas will respond to the wm_nclbuttondown message, while on WINXP, but the message should not be sent, because it is unclear, but there is still a way to implement such a function, that is, the mouse hook.

The following hooks generally take three steps: 1. Install hooks; 2. Implement hook functions; 3. Uninstall hooks. Next we will combineCodeTo describe in detail.

Step 1: Install the hook

Before installing a hook, we need to define a global hook handle hhook, for example:

 
Hhook g_mousehook = NULL;

Then, when appropriate, in the solution to this problem, I will install the hook in the ctestview constructor.

 
If(Null =G_hmousehook ){//For the setwindowshookex function, you can view msdn. The first parameter indicates the hook type. Here we use wh_mouse to indicate the mouse hook. The second parameter is the hook function to be implemented.G_hmousehook= Setwindowshookex (wh_mouse, mouseproc, afxgetapp ()->M_hinstance, getcurrentthreadid ());}

 

Step 2: implement the hook function mouseproc. we implement a global function mouseproc, such:

Lresult callback mouseproc ( Int  Ncode, wparam, lparam ){  //  In this function, wparam indicates the message type. lparam indicates different types of structs Based on the hook type. In the mouse hook, It is mousehookstruct. There are many other types of hooks. For details, refer to msdn.
Bool Bret = False; If (Wparam = Wm_nclbuttondown) {mousehookstruct * Pmousehook = (mousehookstruct * ) Lparam; If (Pmousehook-> hwnd = g_testview-> M_hwnd ){ // Determine whether the current message is sent to the expected window. If you do not want the message to continue, return }} // If it is not the message you want to intercept, or the message is not sent to the View window, let the message continue to pass Return Callnexthookex (g_hmousehook, ncode, wparam, lparam );}

 

Step 3: uninstall the hook and uninstall it when appropriate. In this example, we uninstall the hook in the destructor of testview.

 
 If(Null! =G_hmousehook) {unhookwindowshookex (g_hmousehook );}

 

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.