您正在使用一款已经过时的浏览器!部分功能不能正常使用。
请尝试升级或使用
其他浏览器。
窗体关闭钩子 该捕获哪一个消息呀??? ( 积分: 50 )
暗夜中独舞
Unregistered / Unconfirmed
GUEST, unregistred user!
是窗体关闭,不是窗体销毁。。。销毁是 HCBT_DESTROYWND ,但是我想知道销毁前的关闭的消息。。。。。。。。。。。
redsky.l
Unregistered / Unconfirmed
GUEST, unregistred user!
TWMSysCommand<br>procedure TForm1.WMSysCommand(var message: TWMSysCommand);<br> with message do //消息处理, 当收到系统关闭程序消息时<br> case CmdType of<br> SC_CLOSE:;<br> else inherited;<br> end;<br>end;
lisongmagic
Unregistered / Unconfirmed
GUEST, unregistred user!
暗夜中独舞
Unregistered / Unconfirmed
GUEST, unregistred user!
to redsky.l,:<br>那个窗体不是我自己写的程序,所以你这样的方法也不行呀
暗夜中独舞
Unregistered / Unconfirmed
GUEST, unregistred user!
我刚查了下 好象不能捕捉到窗体关闭的消息<br><br>WH_CBT Hook<br>系统在激活,创建,消毁,最小化,最大化,移动,改变窗体前;在完成一条系统命令前;在从系统消息队列中移去鼠标或键盘事件前;在设置输入焦点前,或同步系统消息队列前,将调用WH_CBT HOOK。你可以在你的HOOK 过程拦截该类HOOK,并返回一个值,告诉系统,是否继续执行上面的操作。
wql
Unregistered / Unconfirmed
GUEST, unregistred user!
嘿嘿!<br>是不是想让某个程序关闭不了!<br>护花使者类的程序!
暗夜中独舞
Unregistered / Unconfirmed
GUEST, unregistred user!
我已经搞定了,我就弄了个鼠标钩子<br>当鼠标左键点下的时候判断鼠标的位置,如果是在右上角,就说明是用户点了关闭,然后就处理一下
ysai
Unregistered / Unconfirmed
GUEST, unregistred user!
WH_CALLWNDPROC 这个可以吧,子类化目标窗口,然后处理WM_CLOSE消息<br>鼠标钩子不能金钩Alt+F4关闭窗口的动作.
暗夜中独舞
Unregistered / Unconfirmed
GUEST, unregistred user!
to ysai:<br> 怎么子类化窗口???<br> 我不大明白你的意思
cqwty
Unregistered / Unconfirmed
GUEST, unregistred user!
暗夜中独舞<br> 窗体子类化技术有啊,你要的话我给你贴点代码,google上也很多。专门有一篇叫做窗体子类化拦截其他窗体消息的文章。<br>你用的鼠标钩子不是一个最好的办法,如果开发人员用了皮肤控件,这个关闭按钮不在右上角呢?
ysai
Unregistered / Unconfirmed
GUEST, unregistred user!
我以前写的一个单元,用的是WH_CALLWNDPROC钩子,把钩子挂上目标窗口后就可以用SetWindowLong子类化窗口了<br>unit WindowExpHook;<br><br>interface<br><br>uses<br> SysUtils, windows, Messages, Graphics, Clipbrd;<br><br>const<br> SMsg_WEH_Hook = '{557F4F7F-2AE2-4902-B8AA-65A609EA5408}';<br> SMsg_WEH_UnHook = '{75CE5BA2-4479-4CEA-AEA7-E61BD2DBDF22}';<br><br>type<br> PWindowHookInfo = ^TWindowHookInfo;<br> TWindowHookInfo = record<br> Handle : HWND;<br> OldProc : Pointer;<br> Separator : Boolean;<br> SubMenu : HMENU;<br> end;<br><br> PWindowExpHookData = ^TWindowExpHookData;<br> TWindowExpHookData = record<br> hWindowHandle : HWND;<br> hHookMessage : Longword;<br> hUnHookMessage : Longword;<br> Hooks : array[0..99] of TWindowHookInfo;<br> end;<br><br>function HookWindowMenu(<br> const AOwner : HWND;<br> const AHandle : HWND<br>  
:Boolean;stdcall;<br>function UnHookAllWindowMenu:Boolean;stdcall;<br><br>var<br> WEHData : PWindowExpHookData;<br><br>implementation<br><br>uses<br> WindowExpRes, ShellAPI;<br><br>const<br> UM_TOPMOST = WM_USER + $0101;<br> UM_PRINTSCREEN = WM_USER + $0102;<br> UM_TRANSPARENT = WM_USER + $0103;<br> UM_ABOUT = WM_USER + $0110;<br><br>function GetHookInfo(<br> const AHandle : HWND<br>  
WindowHookInfo;<br>//取得一个钩子结构<br>var<br> i : Integer;<br>begin<br> Result := nil;<br> for i := Low(WEHData.hooks) to High(WEHData.Hooks) do<br> if WEHData.Hooks
.Handle = AHandle then<br> begin<br> Result := @WEHData.Hooks;<br> break;<br> end;<br>end;<br><br>function GetEmptyHookInfo : PWindowHookInfo;<br>//取得空闲的<br>var<br> i : Integer;<br>begin<br> Result := nil;<br> for i := Low(WEHData.hooks) to High(WEHData.Hooks) do<br> begin<br> if not IsWindow(WEHData.Hooks.Handle) then<br> WEHData.Hooks.Handle := 0;<br> if 0 = WEHData.Hooks.Handle then<br> begin<br> Result := @WEHData.Hooks;<br> break;<br> end;<br> end;<br>end;<br><br>function ShowAbout(const pwhi : PWindowHookInfo): boolean;<br>//显示关于<br>//var<br>// hIcon,hInst:integer;<br>begin<br> //hInst := HINSTANCE;<br> //hInst := GetWindowWord(Application.Handle,GWL_HINSTANCE);<br> //hIcon := ExtractIcon(hInst,PChar(Application.ExeName),0);<br> Result := Boolean(ShellAbout(pwhi.Handle,<br> 'WindowView','WindowView 0.1',0));<br>end;<br><br>procedure WindowProc(<br> Handle : HWND; // handle of window<br> Msg : Longword; // message identifier<br> wParam : wParam; // first message parameter<br> lParam : lParam // second message parameter<br> stdcall;<br>//窗口回调过程<br>var<br> pwhi : PWindowHookInfo;<br>begin<br> pwhi := GetHookInfo(Handle);<br> if Assigned(pwhi) then<br> begin<br> case Msg of<br> WM_SYSCOMMAND :<br> begin<br> case wParam of<br> UM_TOPMOST : ;//UpdateTopMost(pwhi);<br> UM_PRINTSCREEN : ;//DoPrintScreen(pwhi);<br> UM_TRANSPARENT : ;//DoTransparent(pwhi);<br> UM_ABOUT : ;//ShowAbout(pwhi);<br> end;<br> end;<br> WM_CLOSE :<br> begin<br> //空过程,不处理WM_CLOSE消息就关不掉窗口了<br> end; //}<br> else<br> CallWindowProc(pwhi.OldProc, Handle, Msg, wParam, lParam);<br> end;<br> end;<br>end;<br><br>procedure HookProc(nCode, wParam, lParam: LongWORD);stdcall;<br>//HOOK回调过程<br><br> procedure InsertMenus(const AWhi : PWindowHookInfo);<br> //插入菜单<br> var<br> SysMenu : HMenu;<br> i : integer;<br> s : array[0..225] of char;<br> begin<br> SysMenu := GetSystemMenu(AWhi.Handle, False);<br> i := GetMenuItemCount(SysMenu)-1;<br> InsertMenu(SysMenu, i, MF_BYPOSITION + MF_SEPARATOR, 0, nil);<br> AWhi.SubMenu := CreateMenu;<br> InsertMenu(AWhi.SubMenu, 0, MF_BYPOSITION, UM_ABOUT, PChar(SAbout));<br> InsertMenu(AWhi.SubMenu, 0, MF_BYPOSITION + MF_SEPARATOR, 0, nil);<br> InsertMenu(AWhi.SubMenu, 0, MF_BYPOSITION or MF_UNCHECKED, UM_TRANSPARENT, PChar(STransparent));<br> InsertMenu(AWhi.SubMenu, 0, MF_BYPOSITION or MF_UNCHECKED, UM_PRINTSCREEN, PChar(SPrintScreen));<br> InsertMenu(AWhi.SubMenu, 0, MF_BYPOSITION or MF_UNCHECKED, UM_TOPMOST, PChar(STopMost));<br><br> InsertMenu(SysMenu, i, MF_BYPOSITION or MF_POPUP, AWhi.SubMenu, PChar(SExpand));<br> GetMenuString(SysMenu, i - 1, s, 255, MF_BYPOSITION);<br> AWhi.Separator := s[0] <> #0;<br> if AWhi.Separator then<br> InsertMenu(SysMenu, i, MF_BYPOSITION + MF_SEPARATOR, 0, nil);<br> end;<br><br> procedure DeleteMenus(const AWhi : PWindowHookInfo);<br> //删除菜单<br> var<br> SysMenu : HMenu;<br> i : Integer;<br> begin<br> SysMenu := GetSystemMenu(AWhi.Handle, False);<br> i := GetMenuItemCount(SysMenu) - 3;<br> DeleteMenu(SysMenu, i, MF_BYPOSITION);<br> DeleteMenu(SysMenu, i, MF_BYPOSITION);<br> if AWhi.Separator then<br> DeleteMenu(SysMenu, i - 1, MF_BYPOSITION);<br> end;<br><br> procedure DoHook(AHandle : HWND);<br> //挂钩<br> var<br> whi : PWindowHookInfo;<br> begin<br> whi := GetHookInfo(AHandle);<br> if Assigned(whi) then Exit;<br> whi := GetEmptyHookInfo;<br> if not Assigned(whi) then Exit;<br><br> whi.Handle := AHandle;<br> InsertMenus(whi);<br> whi.OldProc :=<br> Pointer(GetWindowLong(AHandle, GWL_WNDPROC));<br> SetWindowLong(AHandle, GWL_WNDPROC, Longint(@WindowProc));<br> end;<br><br> procedure DoUnHook(AHandle : HWND);<br> //脱钩<br> var<br> whi : PWindowHookInfo;<br> begin<br> whi := GetHookInfo(AHandle);<br> if Assigned(whi) then<br> begin<br> DeleteMenus(whi);<br> SetWindowLong(AHandle, GWL_WNDPROC, Longint(whi.OldProc));<br> whi.Handle := 0;<br> end;<br> end;<br><br>type<br> PWMSysCommand = ^TWMSysCommand;<br>begin<br> if PCWPStruct(lParam).Message = WEHData.hHookMessage then<br> DoHook(PCWPStruct(lParam).hwnd)<br> else if PCWPStruct(lParam).Message = WEHData.hUnHookMessage then<br> DoUnHook(PCWPStruct(lParam).hwnd)<br> else<br> CallNextHookEx(0, nCode, wParam, lParam);<br>end;<br><br>function HookWindowMenu(<br> const AOwner : HWND;<br> const AHandle : HWND<br>  :Boolean;stdcall;<br>//导出函数/挂钩<br>var<br> ThreadID : Longword;<br> ProID : Integer;<br>begin<br> Result := False;<br> if (GetSystemMenu(AHandle, False) > 0)<br> and (AOwner <> AHandle)<br> and not Assigned(GetHookInfo(AHandle)) then //}<br> begin<br> ThreadID := GetWindowThreadProcessId(AHandle, @ProID);<br> Result := SetWindowsHookEx(<br> WH_CALLWNDPROC, //WH_GETMESSAGE,WH_CALLWNDPROC,WH_CALLWNDPROCRET<br> @HookProc,<br> Hinstance,<br> ThreadID<br>   > 0;<br> if Result then<br> begin<br> WEHData.hWindowHandle := AOwner; <br> SendMessage(AHandle, WEHData.hHookMessage, 0, 0);<br> end;<br> end;<br>end;<br><br>function UnHookAllWindowMenu:Boolean;stdcall;<br>//导出函数/脱钩所有<br>var<br> i : Integer;<br>begin<br> Result := False;<br> for i := Low(WEHData.Hooks) to High(WEHData.Hooks) do<br> if WEHData.Hooks.Handle > 0 then<br> SendMessage(WEHData.Hooks.Handle, WEHData.hUnHookMessage, 0, 0);<br>end;<br><br>end.
暗夜中独舞
Unregistered / Unconfirmed
GUEST, unregistred user!
头都看晕了 <br>to cqwty,你说的那篇文章能不能贴出来 或者告诉我地址,我找不到呀
暗夜中独舞
Unregistered / Unconfirmed
GUEST, unregistred user!