王
王靖
Unregistered / Unconfirmed
GUEST, unregistred user!
我想截取自己当前窗体的关闭消息,并阻止窗体关闭
看了钱达智的keyboard例子,我想用WH_Shell实现,里面有钩子码
HSHELL_WINDOWDESTROYED表示窗体被关
wParam :Handle to the destroyed window.
我是把钩子在自己进程实现的,没有放到dll里.但关闭本身窗体并不能触发
icode=HSHELL_WINDOWDESTROYED.
由于第一次编hook的程序,不知是设置有问题,还是设置的钩子不对
望各位大虾指出错误给出解决方法
下面是我的程序
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
function SHellHookHandler(iCode: Integer;
wParam: WPARAM;
lParam: LPARAM): LRESULT;
function EnableHotKeyHook: BOOL;
function DisableHotKeyHook: BOOL;
var
Form1: TForm1;
hNextHookProc: HHook;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
begin
if EnableHotKeyHook then
ShowMessage('shellhooking...');
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
if DisableHotKeyHook then
ShowMessage('releasing hook..., DONE!!');
end;
function ShellHookHandler(iCode: Integer;
wParam: WPARAM;
lParam: LPARAM): LRESULT;
const
_KeyPressMask = $80000000;
begin
Result := 0;
If iCode < 0 Then
begin
Result := CallNextHookEx(hNextHookProc, iCode, wParam, lParam);
Exit;
end;
// 侦测Form1窗口被关
if (iCode = HSHELL_WINDOWDESTROYED) then
// and (Findwindow('TForm1','Form1') = wParam) then
begin
if MessageDlg('关闭本程序吗?', mtConfirmation, mbYesNoCancel, 0) = mrYes
then Result := CallNextHookEx(hNextHookProc, iCode, wParam, lParam)
// 允许Windows关闭
else result:=1; // 阻止Windows关闭
end
else begin
Result := CallNextHookEx(hNextHookProc, iCode, wParam, lParam);
end;
showmessage(inttostr(icode));
end;
function EnableHotKeyHook: BOOL;
begin
Result := False;
if hNextHookProc <> 0 then Exit;
hNextHookProc := SetWindowsHookEx(WH_SHELL,@SHellHookHandler,0, GetCurrentThreadID);
Result := hNextHookProc <> 0;
end;
function DisableHotKeyHook: BOOL; export;
begin
if hNextHookProc <> 0 then
begin
UnhookWindowshookEx(hNextHookProc); // 秆埃 Keyboard Hook
hNextHookProc := 0;
MessageBeep(0);
MessageBeep(0);
end;
Result := hNextHookProc = 0;
end;
end.
看了钱达智的keyboard例子,我想用WH_Shell实现,里面有钩子码
HSHELL_WINDOWDESTROYED表示窗体被关
wParam :Handle to the destroyed window.
我是把钩子在自己进程实现的,没有放到dll里.但关闭本身窗体并不能触发
icode=HSHELL_WINDOWDESTROYED.
由于第一次编hook的程序,不知是设置有问题,还是设置的钩子不对
望各位大虾指出错误给出解决方法
下面是我的程序
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
function SHellHookHandler(iCode: Integer;
wParam: WPARAM;
lParam: LPARAM): LRESULT;
function EnableHotKeyHook: BOOL;
function DisableHotKeyHook: BOOL;
var
Form1: TForm1;
hNextHookProc: HHook;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
begin
if EnableHotKeyHook then
ShowMessage('shellhooking...');
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
if DisableHotKeyHook then
ShowMessage('releasing hook..., DONE!!');
end;
function ShellHookHandler(iCode: Integer;
wParam: WPARAM;
lParam: LPARAM): LRESULT;
const
_KeyPressMask = $80000000;
begin
Result := 0;
If iCode < 0 Then
begin
Result := CallNextHookEx(hNextHookProc, iCode, wParam, lParam);
Exit;
end;
// 侦测Form1窗口被关
if (iCode = HSHELL_WINDOWDESTROYED) then
// and (Findwindow('TForm1','Form1') = wParam) then
begin
if MessageDlg('关闭本程序吗?', mtConfirmation, mbYesNoCancel, 0) = mrYes
then Result := CallNextHookEx(hNextHookProc, iCode, wParam, lParam)
// 允许Windows关闭
else result:=1; // 阻止Windows关闭
end
else begin
Result := CallNextHookEx(hNextHookProc, iCode, wParam, lParam);
end;
showmessage(inttostr(icode));
end;
function EnableHotKeyHook: BOOL;
begin
Result := False;
if hNextHookProc <> 0 then Exit;
hNextHookProc := SetWindowsHookEx(WH_SHELL,@SHellHookHandler,0, GetCurrentThreadID);
Result := hNextHookProc <> 0;
end;
function DisableHotKeyHook: BOOL; export;
begin
if hNextHookProc <> 0 then
begin
UnhookWindowshookEx(hNextHookProc); // 秆埃 Keyboard Hook
hNextHookProc := 0;
MessageBeep(0);
MessageBeep(0);
end;
Result := hNextHookProc = 0;
end;
end.