首页/技术开发/内容

运用HOOK创建鼠标增强程序

技术开发2022-10-23 阅读()
New Application建立一个新程序,将工程文件保存为Project1.dpr

//project1的源程序
program Project1;

uses
Forms,
Sample1 in 'Sample1.pas' {Form1};

{$R *.RES}

begin
Application.Initialize;
//隐藏窗口
Application.ShowMainForm := False;

Application.CreateForm(TForm1, Form1);
Application.Run;
end.

将Form1的源程序文件保存成Sample1.pas

//Form1的源程序
unit Sample1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Menus, ImgList,ShellApi, ExtCtrls;

const
WM_ICONMESSAGE=WM_USER+$100;


type
TForm1 = class(TForm)

procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormCreate(Sender: TObject);
private
procedure WMBarIcon(var Message:TMessage);message WM_ICONMESSAGE;

public

end;

function EnableMouseHook(WndHandle:integer):BOOL;external 'MHook.DLL';
function DisableMouseHook:BOOL;external'MHook.DLL';

var
Form1: TForm1;

implementation

{$R *.DFM}


procedure TForm1.WMBarIcon (var Message:TMessage);
begin
//用户双击任务栏图标则关闭程序
if Message.LParam = WM_LBUTTONDBLCLK then
close;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
var
lpData:PNotifyIconData;
begin
//删除任务栏图标
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 := '鼠标演示';
lpData.uFlags := NIF_ICON or NIF_MESSAGE or NIF_TIP;
Shell_NotifyIcon(NIM_DELETE,lpData);
dispose(lpData);
//解除Mouse hook
DisableMouseHook;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
lpData:PNotifyIconData;
begin
EnableMouseHook(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 := '鼠标演示';
lpData.uFlags := NIF_ICON or NIF_MESSAGE or NIF_TIP;
Shell_NotifyIcon(NIM_ADD,lpData);
dispose(lpData);
end;

end.

运行程序,按下Scroll Lock键使其有效,将光标移动到文本窗口中(如IE、Word),移动鼠标,点击鼠标右键,窗口就可以依上一次移动的方向滚动。
利用上面的原理,将程序做一些改动,就可以象专业的鼠标增强程序一样做出例如缩放窗口,运行程序等很多鼠标增强效果来。
上面的程序在Windows95,Delphi4.0下运行通过。

第1页  第2页  第3页  第4页  第5页 

……

相关阅读