P
phyyun
Unregistered / Unconfirmed
GUEST, unregistred user!
原问题如下(新问题见回帖):<br>键盘钩子,HOOK总是断掉,请问如何解决?<br><br>这个程序是用来隐藏窗口的。包括一个EXE,一个DLL。<br>在列表框里加入想要隐藏的窗口标题,按 ALT+2,就会隐藏或显示设定的窗口。<br>我是在DLL中使用KeyboardHook来实现的。<br>每次在DLL里遍历窗口,然后和列表框里的比较,如果符合条件,就隐藏或显示。<br>但是现在程序有个问题,我怎么调试都找不到答案。就是每次启动程序,只有开始两次按ALT+2有效,后来就没响应了,不触发KeyboardHookHandler,我晕。<br>第一次使用HOOK,请教。源代码我贴下面。<br>*****************************<br>DLL PROJECT'HKTest.dpr'<br>*****************************<br>library HKTest;<br>uses<br> Sharemem,<br> forms,<br> HKProc in 'HKProc.PAS';<br>exports<br>EnableHotKeyHook,<br>DisableHotKeyHook,<br>Setform;<br><br>begin<br>hNextHookProc := 0;<br>HotKeyEnable := false;<br>procSaveExit := ExitProc;<br>ExitProc := @HotKeyHookExit;<br>end.<br>***********************<br>HKProc.PAS<br>***********************<br>unit HKProc;<br><br>interface<br><br>uses<br>Windows, dialogs, Messages, Classes, Forms, SysUtils;<br><br>var<br>hNextHookProc: HHook;<br>procSaveExit: Pointer;<br>FormStrings:TStringList;<br>FormStrings2:TStringList;<br>CustomStrings:TStringList;<br>HotKeyEnable: Boolean;<br><br>function KeyboardHookHandler(iCode: Integer;<br>wParam: WPARAM;<br>lParam: LPARAM): LRESULT; stdcall; export;<br>function EnableHotKeyHook: BOOL; export;<br>function DisableHotKeyHook: BOOL; export;<br>procedure HotKeyHookExit; far;<br>procedure Setform(formname, action: pchar; id: integer); export;<br><br>implementation<br><br>Function My_RefreshForm(MyStringList:TStringList; MyStringList2:TStringList):Boolean;<br>var<br>hCurrentWindow: HWnd;<br>szText: array[0..254] of char;<br>begin<br>MyStringList.Clear;<br>MyStringList2.Clear;<br>//hCurrentWindow := GetWindow(application.Handle, GW_HWNDFIRST);<br>hCurrentWindow := GetWindow(GetActiveWindow, GW_HWNDFIRST);<br>while hCurrentWindow <> 0 do<br>begin<br>if GetWindowText(hCurrentWindow, @szText, 255) > 0 then<br>begin<br> MyStringList.Add(StrPas(@szText));<br> MyStringList2.Add(inttostr(hCurrentWindow));<br>end;<br>hCurrentWindow := GetWindow(hCurrentWindow, GW_HWNDNEXT);<br>end;<br>Result:=true;<br>end;<br><br>function KeyboardHookHandler(iCode: Integer;<br>wParam: WPARAM;<br>lParam: LPARAM): LRESULT; stdcall; export;<br>const<br>_KeyPressMask = $80000000;<br>var<br> i,j :integer;<br> ExeHandle : HWnd;<br>begin<br>Result := 0;<br>If iCode < 0 Then<br>begin<br>Result := CallNextHookEx(hNextHookProc, iCode, wParam, lParam);<br>Exit;<br>end;<br>// 侦测 ALT + 2 组合键<br>if ((lParam and _KeyPressMask) = 0) and<br> (GetKeyState(VK_MENU) < 0) and (wParam = Ord('2')) then<br>begin<br> Result := 1;<br> HotKeyEnable := not HotKeyEnable;<br> My_RefreshForm(FormStrings, FormStrings2);<br> for i:=0 to CustomStrings.count-1 do<br> begin<br> for j:=0 to FormStrings.count-1 do<br> if pos(CustomStrings, FormStrings[j])>0 then<br> begin<br> ExeHandle := strtoint(FormStrings2[j]);<br> if ExeHandle <> 0 then<br> begin<br> if HotKeyEnable then<br> ShowWindow(ExeHandle,SW_hide)<br> else<br> ShowWindow(ExeHandle,SW_show);<br> end;<br> end;<br> end;<br>end;<br>end;<br><br>function EnableHotKeyHook: BOOL; export;<br>begin<br>Result := False;<br>if hNextHookProc <> 0 then Exit;<br>FormStrings := TStringList.Create;<br>FormStrings2 := TStringList.Create;<br>CustomStrings := TStringList.Create;<br>CustomStrings.clear;<br>// 挂上 WH_KEYBOARD 这型的 HOOK, 同时, 传回值必须保留下<br>// 来, 免得 HOOK 呼叫链结断掉<br>hNextHookProc := SetWindowsHookEx(WH_KEYBOARD,<br>KeyboardHookHandler,<br>HInstance,<br>0);<br>Result := hNextHookProc <> 0;<br>end;<br><br>function DisableHotKeyHook: BOOL; export;<br>begin<br>if hNextHookProc <> 0 then<br>begin<br>UnhookWindowshookEx(hNextHookProc); // 解除 Keyboard Hook<br>hNextHookProc := 0;<br>FormStrings.Free;<br>FormStrings2.Free;<br>CustomStrings.Free;<br>end;<br>Result := hNextHookProc = 0;<br>end;<br><br>procedure HotKeyHookExit;<br>begin<br>// 如果忘了解除 HOOK, 自动代理解除的动作<br>if hNextHookProc <> 0 then DisableHotKeyHook;<br>ExitProc := procSaveExit;<br>end;<br><br>procedure Setform(formname, action: pchar; id: integer); export;<br>var<br> name : string;<br> Saction : string;<br>begin<br> name := strpas(formname);<br> Saction := strpas(action);<br> if Saction = 'add' then<br> customstrings.add(name)<br> else<br> customstrings.delete(id);<br>end;<br>end.<br><br>*******************************<br>****************<br>Project1.dpr<br>****************<br>program Project1;<br><br>uses<br> Sharemem,<br> Forms,<br> Unit1 in 'Unit1.pas' {Form1};<br><br>{$R *.RES}<br><br>begin<br> Application.Initialize;<br> Application.CreateForm(TForm1, Form1);<br> Application.Run;<br>end.<br><br>********************<br>Unit1.pas<br>********************<br>unit Unit1;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br> StdCtrls, ComCtrls;<br><br>type<br> TForm1 = class(TForm)<br> Button3: TButton;<br> Button4: TButton;<br> Label1: TLabel;<br> ListBox1: TListBox;<br> Edit1: TEdit;<br> GroupBox1: TGroupBox;<br> Label2: TLabel;<br> Label3: TLabel;<br> procedure FormCreate(Sender: TObject);<br> procedure WMNCHitTest(var M:TWMNCHitTest);message wm_NCHitTest;<br> procedure ListBox1DblClick(Sender: TObject);<br> procedure Button3Click(Sender: TObject);<br> procedure Button4Click(Sender: TObject);<br> procedure FormDestroy(Sender: TObject);<br> private<br> { Private declarations }<br> public<br> { Public declarations }<br> end;<br><br>var<br> Form1: TForm1;<br><br>implementation<br><br>function EnableHotKeyHook: BOOL; external 'HKTEST.DLL';<br>function DisableHotKeyHook: BOOL; external 'HKTEST.DLL';<br>procedure Setform(formname, action: pchar; id: integer); external 'HKTEST.DLL';<br><br>{$R *.DFM}<br><br><br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br> //总是置为最前<br> setWindowPos(Application.Handle, HWND_TOPMOST,<br> 0, 0, 0, 0, SWP_NOSIZE or SWP_NOMOVE or SWP_NOACTIVATE);<br>// ShowWindowAsync(application.Handle,sw_hide); {使用Ctrl+Del+Alt看不到此程序}<br><br> EnableHotKeyHook;<br><br>end;<br><br>procedure TForm1.WMNCHitTest(var M: TWMNCHitTest);<br>begin //可在窗口任意部分移動窗口<br> inherited;<br> if M.Result=htClient then<br> M.Result:=htCaption;<br>end;<br><br>procedure TForm1.ListBox1DblClick(Sender: TObject);<br>begin<br> ListBox1.DeleteSelected;<br>end;<br><br>procedure TForm1.Button3Click(Sender: TObject);<br>begin<br> if trim(edit1.text)<>'' then<br> begin<br> Setform(Pchar(trim(edit1.Text)), Pchar('add'), 0);<br> listbox1.Items.Add(trim(edit1.Text));<br> end;<br>end;<br><br>procedure TForm1.Button4Click(Sender: TObject);<br>begin<br> if (ListBox1.Count > 0) and (ListBox1.ItemIndex >= 0 ) then<br> begin<br> Setform(Pchar(ListBox1.Items.strings[ListBox1.ItemIndex]), Pchar('delete'), ListBox1.ItemIndex);<br> listbox1.DeleteSelected;<br> end;<br>end;<br><br>procedure TForm1.FormDestroy(Sender: TObject);<br>begin<br> DisableHotKeyHook;<br>end;<br><br>end.<br><br>unit1的FORM1是主窗口,很简单的,一个小FORM,窗口图片网址如下:http://www.djypanda.com/forum/UploadFile/20039316475963853.jpg<br>如果是DELPHI7的话,可以直接引用下面这个DFM文件:<br>object Form1: TForm1<br> Left = 257<br> Top = 194<br> Width = 369<br> Height = 289<br> Caption = #38544#34255#19987#23478#65288'phyyun'#65289<br> Color = clBtnFace<br> Font.Charset = DEFAULT_CHARSET<br> Font.Color = clWindowText<br> Font.Height = -11<br> Font.Name = 'MS Sans Serif'<br> Font.Style = []<br> OldCreateOrder = False<br> Position = poDesktopCenter<br> OnCreate = FormCreate<br> OnDestroy = FormDestroy<br> PixelsPerInch = 96<br> TextHeight = 13<br> object Label1: TLabel<br> Left = 8<br> Top = 8<br> Width = 329<br> Height = 13<br> AutoSize = False<br> Caption = #35831#36755#20837#35201#38544#34255#30340#31383#21475#26631#39064#25152#21253#21547#30340#25991#23383'('#27880#24847#22823#23567#20889')'<br> end<br> object Button3: TButton<br> Left = 224<br> Top = 64<br> Width = 57<br> Height = 25<br> Caption = #28155#21152<br> TabOrder = 0<br> OnClick = Button3Click<br> end<br> object Button4: TButton<br> Left = 224<br> Top = 112<br> Width = 57<br> Height = 25<br> Caption = #21024#38500<br> TabOrder = 1<br> OnClick = Button4Click<br> end<br> object ListBox1: TListBox<br> Left = 8<br> Top = 56<br> Width = 193<br> Height = 193<br> ItemHeight = 13<br> TabOrder = 2<br> OnDblClick = ListBox1DblClick<br> end<br> object Edit1: TEdit<br> Left = 8<br> Top = 32<br> Width = 193<br> Height = 21<br> TabOrder = 3<br> Text = #22823#23500#32705<br> end<br> object GroupBox1: TGroupBox<br> Left = 208<br> Top = 176<br> Width = 145<br> Height = 73<br> Caption = #28909#38190<br> TabOrder = 4<br> object Label2: TLabel<br> Left = 8<br> Top = 16<br> Width = 129<br> Height = 13<br> AutoSize = False<br> Caption = #38544#34255#25110#26174#31034#35774#23450#31383#21475<br> end<br> object Label3: TLabel<br> Left = 8<br> Top = 32<br> Width = 32<br> Height = 13<br> AutoSize = False<br> Caption = 'ALT+2'<br> end<br> end<br>end<br>