http://blog.9cbs.net/yiruoyun/archive/2004/10/18/141530.aspx
Hook, is a platform for the Windows message handling mechanism, which can be set up on top to monitor some of the messages for the specified window, and the monitored window can be created by other processes. When the message arrives, handle it before the target window handles the function. The hook mechanism allows applications to intercept the Window message or a specific event.
Detailed introduction to hook, in Microsoft's MSDN, http://www.microsoft.com/china/community/program/originalarticles/techdoc/hook.mspx
Here is I am in c # to apply hook:
Effect:
When the user enters B in TextBox, TextBox always displays a
Implementation process:
1. Newly built a C # WindowsApplication
2, in Form1, add some variables below:
Internal enum hooktype // enumeration, hook type
{
// msgfilter = -1,
// jensionrecord = 0,
// journalplayback = 1,
KeyBoard = 2,
// getMessage = 3,
// CallWndProc = 4,
// CBT = 5,
// sysmsgfilter = 6,
// mouse = 7,
// Hardware = 8,
// debug = 9,
// shell = 10,
// foregroundIdle = 11,
// CallWndProcret = 12,
// KeyBoardll = 13,
// mousell = 14,
}
INTPTR _NEXTHOKPTR; // Record the hook number
3. Introduce the necessary API in Form1
[DLLIMPORT ("kernel32.dll")] static extern int getCurrentThreadId (); // get the current thread number API
[DLLIMPORT ("User32.dll")]]]]]
INTERNAL EXTERN Static Void UnhookWindowsHookex (INTPTR HANDLE); // Cancel Hook API
[DLLIMPORT ("User32.dll")]]]]]
INTERNAL EXTERN Static INTPTR SETWINDOWSHOKEX (Int IDHOK, [Marshalas (UnmanagedType.FunctionPtr)] HookProc LPFN, INTPTR HINSTANCE, INT THREADID; // Setting HOOK API
[DLLIMPORT ("User32.dll")]]]]]
INTERNAL EXTERN Static INTPTR CallNexthookex (INTPTR HANDLE, INT CODE, INTPTR WPARAM, INTPTR LPARAM); // Get the next HOOK API
4. Declare an implementation of the commission of INTERNAL DELEGATE INTPTR HOOKPROC (int Code, INTPTR WPARAM, INTPTR LPARAM);
5, add your own HOOK processing
INTPTR MYHOOKPROC (int code, intptr wparam, int code lparam) {
IF (Code <0) Return CallNexthookex (_nexThookPtr, Code, WPARAM, LPARAM); // Returns, let the subsequent program processing the message
IF (wParam.Toint32 () == 98 || WPARAM.TOINT32 () == 66) // If the user enters B
{
THIS.TEXTBOX1.TEXT = "a";
Return (INTPTR) 1; / / directly returned, the message ended
}
Else
{
Return INTPTR.ZERO; / / Return, let the rear program process the message
}
}
6, add a function that is added to the HOOK chain and cancel from the hook chain
Public void setook ()
{
IF (_nexthookptr! = INTPTR.ZERO) // has already been hooked
Return;
HookProc MyHookProc = New HookProc (myhookProc); // Declare a delegation object of your own Hook implementation function
_nexthookptr = setWindowshookex ((int) hooktype.keyboard, myhookproc, intptr.zero, getcurrentthreadid ()); // Add to HOOK chain
}
Public void unhook ()
{
IF (_NexThookPtr! = INTPTR.ZERO)
{
UnHookWindowsHookex (_nexthookptr); // Cancel from the hook chain
_nexthookptr = intptr.zero;
}
}
7. Add setHook () in Form1's LOAD event, add unHook () in the forming event of Form1
Private Void Form1_Load (Object Sender, System.EventArgs E)
{
Sthook ();
}
Private void form1_closing (Object Sender, System.comPonentmodel.canceleventargs E)
{
UnHook ();
}
8, run the input B, discover the display in TextBox is A!