// Childview. cpp: Implementation of the cchildview class
//
# Include "stdafx. H"
# Include "mousehook. H"
# Include "childview. H"
# Ifdef _ debug
# Define new debug_new
# UNDEF this_file
Static char this_file [] = _ file __;
# Endif
Hhook;
Cpoint point;
Cchildview * pview;
Char gstr [256] = "123456 ";
Lresult callback mouseproc (INT ncode, wparam, lparam)
{
// Move the message with the mouse
If (wparam = wm_mousemove | wparam = wm_ncmousemove)
{
Point = (mousehookstruct *) lparam)-> pt;
Pview-> invalidate ();
}
If (wparam = wm_lbuttondown)
{
Sprintf (gstr, "Fuck ");
}
Return callnexthookex (hhook, ncode, wparam, lparam );
}
//////////////////////////////////////// /////////////////////////////////////
// Cchildview
Cchildview: cchildview ()
{
Pview = this;
Hhook = setwindowshookex (wh_mouse, mouseproc, 0, getcurrentthreadid ());
}
Cchildview ::~ Cchildview ()
{
If (hhook)
Unhookwindowshookex (hhook );
}
Begin_message_map (cchildview, cwnd)
// {Afx_msg_map (cchildview)
On_wm_paint ()
//} Afx_msg_map
End_message_map ()
//////////////////////////////////////// /////////////////////////////////////
// Cchildview message handlers
Bool cchildview: precreatewindow (createstruct & CS)
{
If (! Cwnd: precreatewindow (CS ))
Return false;
CS. dwexstyle | = ws_ex_clientedge;
CS. Style & = ~ Ws_border;
CS. lpszclass = afxregisterwndclass (cs_hredraw | cs_vredraw | cs_dblclks,
: Loadcursor (null, idc_arrow), hbrush (color_window + 1), null );
Return true;
}
Void cchildview: onpaint ()
{
Cpaintdc DC (this); // device context for painting
Char STR [256];
Sprintf (STR, "x = % d, y = % d", point. X, point. y );
DC. textout (0, 0, STR );
DC. textout (50, 50, gstr );
// Todo: add your message handler code here
// Do not call cwnd: onpaint () for painting messages
}
1. Write your own message processing function first, and use the mouseproc above to process the messages you are interested in (wm_mousemove, wm_ncmousemove, and wm_lbuttondown (I am interested in moving the mouse and clicking the left button ))
Note: It is best to call callnexthookex () at the end of your function to keep the message going down. (Of course, if you want to block it, use it :))
2. Use setwindowshookex () to put your function in the first line of the linked list of message processing functions.
Hhook setwindowshookex (
IntIdhook,// Type of hook to install hook
HookprocLpfn,// Address of hook procedure processing function pointer, here is the above mouseproc
HinstanceHmod,// Handle to application instance handle, thread hook is 0
DWORDDwthreadid// Identity of thread to install hook for the purebred ID to be monitored
);
The above completes the compilation and serving of hook functions.
3. Don't remember to release it later ..
Use unhookwindowshookex (hhook); its parameter is when the hook function is servingThe return handle of setwindowshookex.