Hook Technology 1: thread hook

Source: Internet
Author: User
Tags textout

// 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 (
Int
Idhook,// 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.

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.