The Message Hook feature of a system message hook is provided in the Windows system, and the various messages in the processing system can be monitored in real time using messages. a lot of
Mouse enhancement software is to use message hooks to block all mouse messages. To set the mouse message hook, you generally create a dynamic connection library (DLL) file that uses the mouse message hook, then you can use this DLL file in other programs.
Handling mouse messages. The following program describes the monitoring of the mouse message by hook through the mouse message, so that the functionality that is similar to the upper and lower rolling of the window is similar to some mouse enhancements.
1. Establish a dynamic connection library selection menu File | New, select DLL to generate a DLL template, save to MHOOK.DPR / / MHOOK.DPR source library MHOOK;
UsessysUtils, classes, hkproc in 'hkproc.pas';
ExportSenableMousehook, Disablemousehook;
BeginhnexthookProc: = 0; procsaveexit: = exitProc; exitproc: = @ HotKeyhooKexit; End.
Select the menu file | New, select Unit to create a PAS file, save it as hkproc.pas // hkproc.pas source program unit hkproc;
InterfaceUseswindows, Messages; Constmove_up = 0;
Move_down = 1; Move_Light = 2; Move_right = 3; VarhnexThookProc: hHOOK; procsaveexit: Pointer; M_Direct: integer; lpoint: tpoint ;owwindow: integer;
Function MouseProc (Icode: WPAR; LPARAM: POINTER): LRESULT; stdcall; export; function enablemousehook (WNDHANDLE: Integer): Bool; Export;
Function; export; function getdirect (fpoint: tpoint; lpoint: tpoint): integer; procedure hotkeyhookexit; far;
IMPLEMentation
The // The GetDirect function determines the direction of the window scrolling according to the movement of the cursor. FUNCTION GETDIRECT (FPOINT: TPOINT; LPOINT: TPOINT): Integer; Variwidth, Iheight: Integer; beginiWidth: = lpoint.x-fpoint.x; ieight: = lpoint.y-fpoint.y; result: = - 1;
IF ((iWidth = 0) or (iheight = 0)).
IF ((iWidth) DIV ABS (Iheight)> = 2) Thenif iWidth <0 THEN // Move To LeftResult: = Move_LEFTELSERESULT: = MOVE_RIGHT
ELSE IF (iHeight) Div Abs (iWidth)> = 2) Thenif Iheight <0 THEN // Move To TopResult: = Move_upelseresult: = Move_down;
END;
Function MouseProc (Icode: WParam; LResult; stdcall; export; varpmouse: ^ mousehookstruct; l: integer; begin // If the user presses the mouse button while the Scroll Lock key is pressed to press the status / / Scroll window. IF ((WPARAM = WM_RBUTTONDOWN) AND BOOLEAN (GetKeyState)) Thenbeginpmouse: = lParam; L: = getdirect (LPOINT, PMOUSE.pt); if L> = 0 THENM_DIRECT: = L; LPOINT: = pmouse.pt; NowWindow: = WINDOWFROMPOINT (LPOINT); if m_direct = Move_up ThensendMessage (nowWindow, WM_VScroll, SB_PageUp, 0) Else if m_direct = Move_Down Then
SendMessage (NowWindow, WM_VSCROLL, SB_PAGEDOWN, 0) else if M_Direct = Move_Left thenSendMessage (NowWindow, WM_HSCROLL, SB_PAGELEFT, 0) else if M_Direct = Move_Right thenSendMessage (NowWindow, WM_HSCROLL, SB_PAGERIGHT, 0); Result: = 1; exit; endelse if ((wParam = WM_RBUTTONUP) and Boolean (GetKeyState (145))) thenResult: = 1elsebeginResult: = 0; if iCode <0 thenbeginResult: = CallNextHookEx (hNextHookProc, iCode, wParam, integer (lParam)); Exit; end; end; END;
function EnableMouseHook (WndHandle: integer): BOOL; export; beginGetCursorPos (lPoint); Result: = False; if hNextHookProc <> 0 thenexit; // Set Mouse hookhNextHookProc: = SetWindowsHookEx (WH_MOUSE, @ MouseProc, Hinstance, 0); Result: = HNEXTHOKPROC <> 0; END;
Function disablemousehook: bool; export; beginif hnexthookProc <> 0 ThenbeginunhookWindowsHookex (HNEXTHOKPROC); HNEXTHOKPROC: = 0; End; Result: = HNEXTHOKPROC = 0;
Procedure HotKeyhooKexit; Beginif HNEXTHOKPROC <> 0 ThendisableMousehook; EXITPROC: = procsaveexit;
End. Select Project | Build MHOOK in the menu to establish a DLL file.
2. Establishing a program call Dynamic Library Here we still use the Delphi creation program, of course, you can also use a dynamic connection library such as VB. Select File | New Application in the menu to build a new program, save the project file as Project1.dpr
Source Project1 of // Project1 ProGram Project1;
Usesforms, Sample1 in 'Sample1.Pas' {FORM1}; {$ r * .res}
BeginApplication.initialize; // Hide Window Application.showMainform: = FALSE
Application.createform (TFORM1, FORM1); Application.Run; End.
Save the source of Form1 into Sample1.PAS
// Form1 source program Unit Sample1;
Interface
Useswindows, Messages, Sysutils, Classes, Graphics, Controls, Forms, Dialogs, Stdctrls, Menus, Imglist, Shellapi, ExtCtrls
Constwm_iconMessage = WM_USER $ 100;
Typetform1 = Class (TFORM)
Procedure formclose (sero: tclosection); procedure formcreate; privateprocedure wmbaricon; message wm_iconMessage;
public
END;
Function EnableMousehook (WNDHANDLE: Integer): Bool; External 'MHOOK.DLL'; Function Disablemousehook: Bool; External'mHOK.DLL ';
Varform1: TFORM1;
IMPLEMentation
{$ R * .dfm}
Procedure TForm1.Wmbaricon (Var Message: TMESSAGE); Begin // User Double-click the taskbar icon.
procedure TForm1.FormClose (Sender: TObject; var Action: TCloseAction); varlpData: PNotifyIconData; begin // delete taskbar icon lpData: = new (PNotifyIconDataA); lpData.cbSize: = 88; // SizeOf (PNotifyIconDataA); lpData. Wnd: = form1.handle; lpdata.hicon: = form1.icon.handle; lpdata.ucallbackMessage: = WM_ICONMESSAGE; lpdata.uid: = 0; lpdata.sztip: = 'mouse demonstration'; lpdata.uflags: = nif_icon or nif_message OR nif_tip; shell_notifyicon (nim_delete, lpdata); Dispose (LPDATA); // Release MOUse hookdisablemousehook;
procedure TForm1.FormCreate (Sender: TObject); varlpData: PNotifyIconData; beginEnableMouseHook (Form1.Handle); Form1.Visible: = False; lpData: = new (PNotifyIconDataA); lpData.cbSize: = 88; // SizeOf (PNotifyIconDataA); LPDATA.Wnd: = form1.handle; lpdata.hicon: = form1.icon.handle; lpdata.uCallbackMessage: = WM_ICONMESSAGE; LPDATA.UID: = 0; lpdata.sztip: = 'mouse demonstration'; lpdata.uflags: = nif_icon OR nif_message or nif_tip; shell_notifyicon (NIM_ADD, LPDATA); Dispose (LPDATA); END;
Run the program, press the scroll lock to make it valid, move the cursor into the text window (such as IE, Word), move the mouse, click the right mouse button, the window can be
The last direction is scrolling. With the above principle, make some changes to the program, you can make, for example, a professional mouse enhancement program, such as zoom windows, running programs, etc. to enhance the effect.
. The above program is running in Windows 98, Delphi5.0.