G
guyigood
Unregistered / Unconfirmed
GUEST, unregistred user!
目的,监控一个程序的一个窗口上面的按钮按下事件。
代码如下,在w2k上可以运行,但时间不能太长,否则报错,在98下面运行提示系统
资源不足,随后死机。
请各位高手指点一下。非常感谢。
unit Unit1;
interface
uses
Windows, Messages,shellapi;
var
hookmenu:thandle;
windows1:wparam;
a:integer;
procSaveExit: Pointer;
function menumsg(iCode: Integer;wParam: WPARAM;lParam: LPARAM): LRESULT; stdcall; export;
function unmenuhook():boolean;stdcall;export;
function setmenuhook():boolean;stdcall;export;
//procedure menumsg(var msg:tmessage);far;
procedure HookExit; far;
implementation
function setmenuhook():boolean;export;
begin
Result := False;
if hookmenu<>0 then Exit;
hookmenu:=SetWindowsHookEx(WH_CallWndProc,menumsg,hinstance,0);
//hookmenu:=SetWindowsHookEx(WH_getmessage,menumsg,hinstance,0);
Result := hookmenu<>0;
end;
function unmenuhook:boolean;export;
begin
if hookmenu<>0 then
begin
UnhookWindowshookEx(hookmenu);
hookmenu:= 0;
end;
Result := hookmenu = 0;
end;
function menumsg(iCode: Integer;wParam: WPARAM;lParam: LPARAM): LRESULT; stdcall; export;
var
//f1:textfile;
pmsgCwpStruct;
g:TCwpStruct;
g1,g2:hwnd;
//spcx:Tspfl;
i:integer;
WName:array[0..254] of char;
//wnamechar;
begin
//pCwpStruct
pmsg:=pCwpStruct(lparam);
g.message:=pmsg^.message;
g.lParam:=pmsg^.lParam;
g.wParam:=pmsg^.wParam;
g.hwnd:=pmsg^.hwnd;
if (g.hwnd=findwindow(nil,'选择一个))and(g.message=273) then
begin
g1:=getwindow(g.hwnd,gw_child);
for i:=1 to 1000 do
begin
g2:=GetWindow(g1,GW_HWNDNEXT);
getwindowtext(g2,wname,254);
if (string(wname)='分类查询') then
break;
g1:=g2;
end;
if g.lParam=g2 then
begin
// spcxnew;
shellexecute(g.hwnd,'open','spflcx.exe','','',sw_showna);
end;
// messagebox(g.hwnd,'fkdsj','dfaldja',sw_show);
end;
//wm_command
if (g.message=287)and(g.hwnd=findwindow(nil,'xxx')) then
begin
//查询菜单id
if (g.wparam=-65536)and(windows1=-2139095014)then
begin
// zk:=tspzkwh.Create(application);
// zk.showmodal;
// zk.free;
shellexecute(g.hwnd,'open','zk.exe','','',sw_showna);
end;
if findwindow(nil,'xxxx')<>0 then
postmessage(findwindow(nil,'xxxx'),wm_close,0,0);
end;
windows1:=g.wparam;
Result:= 1;
end
else
begin
CallNextHookEx(hookmenu,icode,WParam,LParam);
Result := 0;
end;
end;
procedure HookExit;
begin
if findwindow(nil,'xxxx')<>0 then
postmessage(findwindow(nil,'xxxx'),wm_close,0,0);
if hookmenu <>0 then unmenuhook;
ExitProc := procSaveExit;
end;
end.
发现去掉这段代码,可以正常运行,那位大哥给点意见吧,拜托了!
if (g.hwnd=findwindow(nil,'选择一个))and(g.message=273) then
begin
g1:=getwindow(g.hwnd,gw_child);
for i:=1 to 1000 do
begin
g2:=GetWindow(g1,GW_HWNDNEXT);
getwindowtext(g2,wname,254);
if (string(wname)='分类查询') then
break;
g1:=g2;
end;
代码如下,在w2k上可以运行,但时间不能太长,否则报错,在98下面运行提示系统
资源不足,随后死机。
请各位高手指点一下。非常感谢。
unit Unit1;
interface
uses
Windows, Messages,shellapi;
var
hookmenu:thandle;
windows1:wparam;
a:integer;
procSaveExit: Pointer;
function menumsg(iCode: Integer;wParam: WPARAM;lParam: LPARAM): LRESULT; stdcall; export;
function unmenuhook():boolean;stdcall;export;
function setmenuhook():boolean;stdcall;export;
//procedure menumsg(var msg:tmessage);far;
procedure HookExit; far;
implementation
function setmenuhook():boolean;export;
begin
Result := False;
if hookmenu<>0 then Exit;
hookmenu:=SetWindowsHookEx(WH_CallWndProc,menumsg,hinstance,0);
//hookmenu:=SetWindowsHookEx(WH_getmessage,menumsg,hinstance,0);
Result := hookmenu<>0;
end;
function unmenuhook:boolean;export;
begin
if hookmenu<>0 then
begin
UnhookWindowshookEx(hookmenu);
hookmenu:= 0;
end;
Result := hookmenu = 0;
end;
function menumsg(iCode: Integer;wParam: WPARAM;lParam: LPARAM): LRESULT; stdcall; export;
var
//f1:textfile;
pmsgCwpStruct;
g:TCwpStruct;
g1,g2:hwnd;
//spcx:Tspfl;
i:integer;
WName:array[0..254] of char;
//wnamechar;
begin
//pCwpStruct
pmsg:=pCwpStruct(lparam);
g.message:=pmsg^.message;
g.lParam:=pmsg^.lParam;
g.wParam:=pmsg^.wParam;
g.hwnd:=pmsg^.hwnd;
if (g.hwnd=findwindow(nil,'选择一个))and(g.message=273) then
begin
g1:=getwindow(g.hwnd,gw_child);
for i:=1 to 1000 do
begin
g2:=GetWindow(g1,GW_HWNDNEXT);
getwindowtext(g2,wname,254);
if (string(wname)='分类查询') then
break;
g1:=g2;
end;
if g.lParam=g2 then
begin
// spcxnew;
shellexecute(g.hwnd,'open','spflcx.exe','','',sw_showna);
end;
// messagebox(g.hwnd,'fkdsj','dfaldja',sw_show);
end;
//wm_command
if (g.message=287)and(g.hwnd=findwindow(nil,'xxx')) then
begin
//查询菜单id
if (g.wparam=-65536)and(windows1=-2139095014)then
begin
// zk:=tspzkwh.Create(application);
// zk.showmodal;
// zk.free;
shellexecute(g.hwnd,'open','zk.exe','','',sw_showna);
end;
if findwindow(nil,'xxxx')<>0 then
postmessage(findwindow(nil,'xxxx'),wm_close,0,0);
end;
windows1:=g.wparam;
Result:= 1;
end
else
begin
CallNextHookEx(hookmenu,icode,WParam,LParam);
Result := 0;
end;
end;
procedure HookExit;
begin
if findwindow(nil,'xxxx')<>0 then
postmessage(findwindow(nil,'xxxx'),wm_close,0,0);
if hookmenu <>0 then unmenuhook;
ExitProc := procSaveExit;
end;
end.
发现去掉这段代码,可以正常运行,那位大哥给点意见吧,拜托了!
if (g.hwnd=findwindow(nil,'选择一个))and(g.message=273) then
begin
g1:=getwindow(g.hwnd,gw_child);
for i:=1 to 1000 do
begin
g2:=GetWindow(g1,GW_HWNDNEXT);
getwindowtext(g2,wname,254);
if (string(wname)='分类查询') then
break;
g1:=g2;
end;