关于HOOK的,已有点眉目了,进来看看,不会也UP下! 能解决的再加100大元 (40分)

  • 主题发起人 主题发起人 lfpsoft
  • 开始时间 开始时间
想到用MOUSEHOOK做,但对于HOOK研究不深,找了些例子看了。
现在用HOOK已载到按下右键的消息。但由于水平问题,
全部的按右键的消息都“吃”掉了。哪位高手帮在忙看看,应该如何得出是在
任务栏上的自己程序上按了右键,然后又如何弹出自己的菜单呢?
我想到用屏幕取词,如果取得窗口标题为程序标题,则就弹出自己的菜单。
但屏幕取词好像很复杂呀。
大家想想办法吧!
下面给出我的HOOK,用的是DLL HOOK;请大家指教指教!
//DLL 工程代码
library HKTest;

uses
HKProc in 'HKProc.pas';

exports
EnableMouseHook,
DisableMouseHook;

begin
hNextHookProc := 0;
procSaveExit := ExitProc;
ExitProc := @MouseHookExit;
end.

//HKProc.pas
unit HKProc;

interface

uses
Windows, Messages;

var
hNextHookProc: HHook;
procSaveExit: Pointer;


function MouseHookHandler(iCode: Integer;
wParam: WPARAM;
lParam: LPARAM): LRESULT; stdcall; export;
function EnableMouseHook: BOOL; export;
function DisableMouseHook: BOOL; export;
procedure MouseHookExit; far;

implementation


function MouseHookHandler(iCode: Integer;
wParam: WPARAM;
lParam: LPARAM): LRESULT; stdcall; export;
begin
Result := 0;
If iCode < 0 Then
begin
Result := CallNextHookEx(hNextHookProc, iCode, wParam, lParam);
Exit;
end;
if (wParam = WM_RBUTTONUP) then
begin
Result := 1;
MessageBeep(0);
// DoMenu;//如何在这里加入判断以弹出自己的菜单呢?
end;
end;

function EnableMouseHook: BOOL; export;
begin
Result := False;
if hNextHookProc <> 0 then Exit;
hNextHookProc := SetWindowsHookEx(WH_MOUSE,
MouseHookHandler,
HInstance,
0);
Result := hNextHookProc <> 0;
end;

function DisableMouseHook: BOOL; export;
begin
if hNextHookProc <> 0 then
begin
UnhookWindowsHookEx(hNextHookProc);
hNextHookProc := 0;
MessageBeep(0);
end;
Result := hNextHookProc = 0;
end;

procedure MouseHookExit;
begin
if hNextHookProc <> 0 then DisableMouseHook;
ExitProc := procSaveExit;
end;

end.


//调用
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;

type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }

public
{ Public declarations }
end;

function EnableMouseHook:BOOL;stdcall;external 'HKTest.dll';
function DisableMouseHook:BOOL;stdcall;external 'HKTest.dll';

var
Form1: TForm1;
hook,mm:integer;

implementation

{$R *.dfm}



procedure TForm1.FormCreate(Sender: TObject);
begin
EnableMouseHook;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin

DisableMouseHook;
end;

end.
 
lfpsoft:
你是不是用winamp的dll
做了个类似的

能说一下是什么原理么
你怎么利用winamp的dll的
好象知道导出函数也没用呀
 
我没有用到WINAMP的DLL,DLL也是自己写的,上面有代码,你看下就知道了.
我已有注明的.
各位HOOK的高手,帮帮忙吧!
 
lfpsoft:你的代码中有几个小问题,所以拦截的结果不好;
1. function EnableMouseHook: BOOL; export;
begin
Result := False;
if hNextHookProc <> 0 then Exit;
hNextHookProc := SetWindowsHookEx(WH_MOUSE,
MouseHookHandler,
HInstance,
0);//注意这里不要用0,否则将监视所有进程,你可以将你自己程序的进程ID传入,按你的意思:应该是任务栏的ID。
Result := hNextHookProc <> 0;
end;
2. function MouseHookHandler(iCode: Integer;
wParam: WPARAM;
lParam: LPARAM): LRESULT; stdcall; export;
begin
Result := 0;
If iCode < 0 Then
begin
Result := CallNextHookEx(hNextHookProc, iCode, wParam, lParam);
Exit;
end;
if (wParam = WM_RBUTTONUP) then
begin
// DoMenu;//如何在这里加入判断以弹出自己的菜单呢?
//以下是我的设想,还没有做类似的程序
//应该用判断,鼠标消息的发送者handle和坐标(x,y),如果是“本程序”handle再处理。显示菜单是很简单的!
end;
end;
泛泛而谈,希望对你有用,有空我帮你试一下啊。
 
你可以先用软件工具分析一下WINAMP的程序和它的运行情况(如加载了哪些DLL),再分析
实现的过程。不过这到是一个很好的话题,好好研究一下,写篇文章出来是个好注意,还可
以骗点稿费,呵呵,这两天去丈母娘家,回来帮你做一个,你的邮件地址告诉我。我的邮件
地址是:tufeiping@sina.com
 
我想问一下HOOK是什么东西啊?
 
希望晶晶有时间就帮我做一做吧,我对HOOK的研究不深,还在入门阶段。
但是我真的很有兴趣研究研究这个问题。
我已用增加一个设置进程ID的函数:
var ProcessId :DWord;

procedure SetProcessId( ID: DWord );stdcall; export;
begin
ProcessId := ID;
end;
将hNextHookProc := SetWindowsHookEx(WH_MOUSE,
MouseHookHandler,
HInstance,
0);//
改为hNextHookProc := SetWindowsHookEx(WH_MOUSE,
MouseHookHandler,
HInstance,
ProcessId);//

在调用时这样写:
var ID:DWord;
MyHWnd:HWnd;

{不知下面这个方法用得对不对,但是这样的话好像监视不起作用了。}
MyHWnd:= FindWindow('Shell_TrayWnd',nil);//得到任务栏的句柄
GetWindowThreadProcessID(MyHWnd, ID);//得到进程ID

SetProcessId(ID);
EnableMouseHook;


另外在这里:
if (wParam = WM_RBUTTONUP) then
begin
//如何判断是“本程序”handle呢?
end;

高手们发表下意见吧!
 
<<delphi深度历险>>里有你要的功能,不用hook.
关于它的源码,你可以去http://www.developres.com/dispdoc.asp?id=144下载.
它的那个"桌面随意帖软件"中有介绍的.
 
TO:jbas
我去下载了,但是怎么找也找不到呀。
里面有CHAP02到CHAP10几个目录,你能告诉我具体在哪个目录吗?
 
chap05中的DESKPAD这个目录下,你打开看它的源代码,里面有!祝好运.
 
感谢jbas!
但里面的是使用InsertMenuItem的,并不是我想要的那种方法
 
//以下方法已经能钩住任务栏的消息了,但是如何知道鼠标是在自己的程序上呢?
library Hook;

uses
SysUtils,
Classes,
HookProc in 'HookProc.pas';

{$R *.RES}

exports
SetHook,
EndHook,
SetAppHandled,
SetHandled;

begin
IntoShare;
end.

//HookProc.pas
unit HookProc;

interface

uses
SysUtils,
Windows,
Dialogs,
Messages;

function MyCBTHookProc(nCode: Integer; wp: wParam; lp: lParam): LResult; stdcall;
procedure SetHook;export;
procedure EndHook;export;
procedure IntoShare; stdcall;export;
procedure SetHandled( H: THandle );stdcall; export;
procedure SetAppHandled( H: THandle);stdcall; export;

implementation

type
TGoData = record
TargetWnd : Hwnd;
AppHwnd : THandle;
ghHook : HHook;
end;
PGoData = ^TGoData;


var
GoData : PGoData;
MemFile : THandle;
aStr:String;
procedure SetHandled( H: THandle );stdcall; export;
begin
GoData^.TargetWnd := H;
end;

procedure SetAppHandled( H: THandle);stdcall; export;
begin
GoData^.AppHwnd := H;

end;


function MyCBTHookProc(nCode: Integer; wp: wParam; lp: lParam): LResult; stdcall;
begin
if wP = WM_RBUTTONUP then
if (PMouseHookStruct(lp)^.hwnd =GoData^.TargetWnd) then
begin
MessageBeep(0);
// GetWindowText(GoData^.AppHwnd,@aStr[1],255);
showmessage(inttostr(lp)+' '+inttostr(GoData^.AppHwnd)+' '+inttostr(PMouseHookStruct(lp)^.dwExtraInfo));
result :=1;
exit;
end;
Result := CallNextHookEx( GoData^.ghHook, nCode, wP, lP);
end;


procedure SetHook;export;
begin
GoData^.ghHook := SetWindowsHookEx(WH_MOUSE, @MyCBTHookProc, hInstance, 0);
end;

procedure EndHook;export;
begin
UnhookWindowsHookEx( GoData^.ghHook );
end;

procedure IntoShare; stdcall;export;
begin
MemFile := OpenFileMapping( FILE_MAP_WRITE, False, 'JJYY' );
if MemFile = 0 then
MemFile:=CreateFileMapping( $FFFFFFFF, nil,
PAGE_READWRITE, 0, SizeOf( TGoData ), 'JJYY');
GoData := MapViewOfFile( MemFile, FILE_MAP_WRITE, 0, 0, 0 );
if MemFile = 0 then
FillChar( GoData^, SizeOf( TGoData ),0);
SetLength(aStr,255);
end;

end.

//测试
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
Ttest = class(TForm)
procedure FormDestroy(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
procedure SetHandled( H : THandle ); stdcall;external 'Hook.dll';

procedure SetAppHandled( H : THandle); stdcall;external 'Hook.dll';

procedure SetHook; stdcall;external 'Hook.dll';

procedure EndHook; stdcall;external 'Hook.dll';

var
test: Ttest;

implementation

{$R *.dfm}

procedure Ttest.FormDestroy(Sender: TObject);
begin
EndHook;
end;

procedure Ttest.FormCreate(Sender: TObject);
var
H : THandle;
begin
SetAppHandled(Handle);
H:= FindWindow('Shell_TrayWnd',nil);
H :=Findwindowex(H,0,'ReBarWindow32',nil);//得到任务栏的句柄
H :=Findwindowex(H,0,'MSTaskSwWClass',nil);//得到任务栏的句柄
H :=Findwindowex(H,0,'ToolbarWindow32',nil);//得到任务栏的句柄
SetHandled(H);
SetHook;

end;

end.

 
问题得到解决,散分!
 
后退
顶部