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 );}