我写了一个鼠标钩子的DLL,但是只有在主程序的窗口上移动鼠标才有响应,离开主程序窗口就没有反应了,这是为什么啊?
我把我写的DLL得一部分代码写出来,哪位高手给我看看阿。
function EnableMouseHook(const wnd:integer):BOOL;stdcall;export;
begin
result:=false;
OwnerHwnd:=wnd;
if hNextHookProc<>0 then exit;
hNextHookProc:=SetWindowsHookEx(WH_MOUSE,MouesProc,hInstance,0);
result:=hNextHookProc<>0;
end;
//OwnerHwnd保存得主窗口的句柄
function MouesProc(iCode : Integer;wParam : WPARAM;lParam : LPARAM):LRESULT;stdcall;export;
var
MouseHookStruct: ^TMOUSEHOOKSTRUCT;
str:array[0..254] of char;
Wnd:integer;
currPoint:TPoint;
begin
MouseHookStruct:=pointer(lParam);
if iCode>=0 then
begin
Wnd:=MouseHookStruct.hwnd;
GetWindowText(Wnd,str,255);
sendmessage(OwnerHwnd,WM_SETTEXT,length(string(str)),longint(pchar(string(str))));
end;
result:=CallNextHookEx (hNextHookProc,iCode,wParam,lParam);
end;
我把我写的DLL得一部分代码写出来,哪位高手给我看看阿。
function EnableMouseHook(const wnd:integer):BOOL;stdcall;export;
begin
result:=false;
OwnerHwnd:=wnd;
if hNextHookProc<>0 then exit;
hNextHookProc:=SetWindowsHookEx(WH_MOUSE,MouesProc,hInstance,0);
result:=hNextHookProc<>0;
end;
//OwnerHwnd保存得主窗口的句柄
function MouesProc(iCode : Integer;wParam : WPARAM;lParam : LPARAM):LRESULT;stdcall;export;
var
MouseHookStruct: ^TMOUSEHOOKSTRUCT;
str:array[0..254] of char;
Wnd:integer;
currPoint:TPoint;
begin
MouseHookStruct:=pointer(lParam);
if iCode>=0 then
begin
Wnd:=MouseHookStruct.hwnd;
GetWindowText(Wnd,str,255);
sendmessage(OwnerHwnd,WM_SETTEXT,length(string(str)),longint(pchar(string(str))));
end;
result:=CallNextHookEx (hNextHookProc,iCode,wParam,lParam);
end;
VC在调试状态下是这样的
你直接点击exe运行试试看
你是在给主窗体发消息吧
还有
Wnd:integer;
我觉得应改为
Wnd:Handle;
sendmessage(Wnd,WM_SETTEXT,length(string(str)),longint(pchar(string(str))));
我也觉得是OwnerHwnd的问题,因为我向鼠标坐标所在窗口发消息是对的
sendmessage(MouseHookStruct.hwnd,WM_SETTEXT,length(string(str)),longint(pchar(string(str))));
在vc的例子中是用
#pragma data_seg("mydata") 关键字定义的句柄变量,
请问,在delphi中怎样解决呢?
确如你所说,OwnerHwnd变为0了,但是为什么呢?
帮帮忙吧,谢谢了
事先声明,我不会
我刚放上了各种钩子的实现源码