还是关于钩子的,对于目标窗口关闭消息的响应(100分)

P

pomb

Unregistered / Unconfirmed
GUEST, unregistred user!
还是钩子,就是获取指定的窗口关闭的消息,现在获取窗口绝对没有问题.<br>在Hook dll中偶指定了SetShellHook的参数为调用该钩子的窗口的<br>句柄CallWin以及需要捕获关闭消息的那个窗口的句柄TargetWin.<br>对关闭消息的处理是向CallWin发送一个自定义消息以通知其监视的<br>窗口已经关闭.<br>可是在处理的时候发现,根本没有接受到HShellDestroyed消息,但是<br>通过对参数的显示又表明CallWin和TargetWin的句柄确实传给了dll,<br>不知道错在哪里?还有检查wparam怎么没有跟关闭的窗口句柄相符的?<br>以前没怎么搞过钩子,不要见笑,100分吧!毕竟有人问过同样问题了.
 
http://www.gislab.ecnu.edu.cn/delphibbs/DispQ.asp?LID=198085<br>http://www.gislab.ecnu.edu.cn/delphibbs/DispQ.asp?LID=208187<br>http://www.gislab.ecnu.edu.cn/delphibbs/DispQ.asp?LID=202446<br><br>
 
http://www.csdn.net有DEMO
 
主要就是要理解: dll的“全局变量”,在不同的进程之间其实是不同的东西了
 
To 前卫兄,偶当然把所有有关问题都看了才敢再发,您所说的那几篇<br>偶都看了好几遍,现在再把偶的问题具体说一下。<br>偶的钩子是钩住WH_SHELL,当被监视窗口关闭时应该得到<br>HShellDestroyed消息。可是偶的钩子可以得到其他消息偏偏<br>就是没有这个表示窗口Destroyed的消息。偶看过那些文章,知道对于<br>全局变量要放入共享区,要先MappFile,可是偶想再怎样至少说这个<br>Destroyed消息总能得到吧?偶的iCode总是不等于它:&lt;<br>另外请教一下,当收到HShellDestroyed消息时,是否wparam即被<br>Destroy的Window句柄?<br>谢谢先。<br><br>To pipi:<br>谢谢,这个偶理解了的说,偶现在的问题是得不到需要的消息呀,救命<br>大虾:)<br>
 
WH_SHELL只能得到 top-level window 的destroy,如果你要看的窗口<br>的parent不是0,就没戏了。<br><br>可以考虑WH_CBT钩子
 
近日外出有事没来看,偶要看的Window正是Parent为零的窗口,不是子窗口.<br>而且偶在钩子中还没有比较窗口句柄就已经得不到相应的Destroy的消息了.<br>另外WH_CBT钩子偶没有仔细看过,可以给个详细点的说明吗?谢了先<br>
 
呵呵,我已经作成功了,并不是使用WH_CBT.
 
hehe,沈兄如何搞定?拜托源码贴出来瞧瞧好吗?可以的话75分归您了:)<br>呵呵,谢了谢了!
 
呵呵,只给我75分?<br><br>//程序教乱,没时间整理。凑和作看看吧。<br><br>library capwnd;<br><br>uses<br>&nbsp; SysUtils,<br>&nbsp; Classes,<br>&nbsp; Dialogs,<br>&nbsp; windows,<br>&nbsp; messages;<br><br>type TCommonData = record<br>&nbsp; &nbsp; &nbsp; &nbsp;HookID:HHook;<br>&nbsp; &nbsp; &nbsp; &nbsp;CallBackHandle:HWnd;<br>&nbsp; &nbsp; &nbsp;end;<br>var<br>&nbsp; hNextHookProc: HHook;<br>&nbsp; procSaveExit: Pointer;<br>&nbsp; receiver,msg_id:integer;<br>var &nbsp;HMapFile:THandle;<br>&nbsp; &nbsp; &nbsp;CommonData:^TCommonData;<br><br>const STR_MSGMOUSEPOS:pchar='WM_MOUSEPOS';<br><br>function MousePosHookHandler(iCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;<br>var h:pChar;<br>begin<br>&nbsp; if iCode &lt; 0 then<br>&nbsp; begin<br>&nbsp; &nbsp; Result := CallNextHookEx(hNextHookProc, iCode, wParam, lParam);<br>&nbsp; end<br>&nbsp; else<br>&nbsp; begin<br>&nbsp; &nbsp; if iCode = HSHELL_WINDOWDESTROYED &nbsp;then<br>&nbsp; &nbsp; begin<br>// &nbsp; &nbsp; &nbsp;ShowMessage('Ok, HSHELL_WINDOWDESTROYED');<br>// &nbsp; &nbsp; &nbsp;h:=windowfrompoint(PMouseHookStruct(lParam).pt);<br>&nbsp; &nbsp; &nbsp; Sendmessage(CommonData.CallBackHandle,msg_id,wParam,0);<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; Result := 0;<br>&nbsp; end;<br>end;<br><br>function EnableMouseHook(hld:hwnd): BOOL; export;<br>begin<br>&nbsp; Result := False;<br>&nbsp; CommonData.CallBackHandle:=hld;<br>&nbsp; if hNextHookProc &lt;&gt; 0 then Exit;<br>&nbsp; hNextHookProc := SetWindowsHookEx(WH_SHELL, MousePosHookHandler,Hinstance, 0);<br>&nbsp; Result :=hNextHookProc &lt;&gt; 0 ;<br>end;<br><br>function DisableMouseHook: BOOL; export;<br>begin<br>&nbsp; if hNextHookProc &lt;&gt; 0 then<br>&nbsp; begin<br>&nbsp; &nbsp; UnhookWindowshookEx(hNextHookProc);<br>&nbsp; &nbsp; hNextHookProc := 0;<br>&nbsp; end;<br>&nbsp; Result := hNextHookProc = 0;<br>end;<br><br>procedure MouseHookExit;<br>begin<br>&nbsp; if hNextHookProc &lt;&gt; 0 then DisableMouseHook;<br>&nbsp; ExitProc := procSaveExit;<br>end;<br><br>procedure MapCommonData;<br>var FirstCall: Boolean;<br>begin<br>&nbsp; HMapFile:=OpenFileMapping(FILE_MAP_WRITE, False, 'sjhdfasdfasdfasd');<br>&nbsp; FirstCall:=(HMapFile = 0);<br>&nbsp; if FirstCall then<br>&nbsp; &nbsp; HMapFile:=CreateFileMapping($FFFFFFFF,nil,PAGE_READWRITE,0,SizeOf(TCommonData),'sjhdfasdfasdfasd');<br>&nbsp; CommonData:= MapViewOfFile(HMapFile, FILE_MAP_WRITE, 0, 0, 0);<br>&nbsp; if FirstCall then FillChar(CommonData^, SizeOf(TCommonData), 0);<br>end;<br><br><br>procedure IntoDll; stdcall;<br>begin<br>&nbsp; msg_id:= RegisterWindowMessage(STR_MSGMOUSEPOS);<br>&nbsp; receiver:=0;<br>end;<br><br>exports<br>&nbsp; EnableMouseHook,<br>&nbsp; DisableMouseHook;<br>begin<br>&nbsp; Intodll;<br>&nbsp; MapCommonData;<br>&nbsp; hNextHookProc := 0;<br>&nbsp; procSaveExit := ExitProc;<br>&nbsp; ExitProc := @MouseHookExit;<br>end.<br><br><br><br><br>unit unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br>&nbsp; StdCtrls;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Edit1: TEdit;<br>&nbsp; &nbsp; procedure FormCreate(Sender: TObject);<br>&nbsp; &nbsp; procedure FormClose(Sender: TObject; var Action: TCloseAction);<br>&nbsp; &nbsp; procedure WndProc(var Mess: TMessage); override;<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br>&nbsp; msg_id:integer;<br>const STR_MSGMOUSEPOS:pchar='WM_MOUSEPOS';<br><br>implementation<br><br>function EnableMouseHook(hld:hwnd): BOOL; external 'capwnd.DLL';<br>function DisableMouseHook: BOOL; external 'capwnd.DLL';<br><br>{$R *.DFM}<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br>&nbsp;msg_id:=RegisterWindowMessage(STR_MSGMOUSEPOS);<br>&nbsp;EnableMouseHook(handle);<br>end;<br><br>procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);<br>begin<br>&nbsp; DisableMouseHook;<br>end;<br>procedure TForm1.WndProc(var Mess: TMessage);<br>var<br>&nbsp; Str:array [0..255] of char;<br>begin<br>&nbsp; if (mess.msg=msg_id) then<br>&nbsp; begin<br>&nbsp; &nbsp; &nbsp;GetWindowText(mess.WParam,Str,256);<br>&nbsp; &nbsp; &nbsp;Edit1.Text:=str;<br>&nbsp; end;<br>&nbsp; inherited;<br>end;<br><br>end.
 
hehe,谢谢沈兄,统共就100,分你80,呵呵,知道你不在乎这点,表示一下谢意嘛!暂时接受答案吧,虽然偶的程序跟沈兄的差不多,就是老截获不到,呵呵,<br>以后再说了:)
 
顶部