主要是想拦截鼠标左键的点击,在点击三次后释放钩子,如果不在ACTTEST中加入SHOWMESSAGE(‘LLLL’);时就没什么问题,加入后,如果在DELPHI中调试,连DELPHI都会很快的跳出,如果单纯的运行的话好象就跳不出钩子程序。
是不是钩子程序中的操作有什么限制?
unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;type
  PointSite=record
    X:integer;
    Y:integer;
  end;  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
  procedure ActTest;
    { Public declarations }
  end;var
  Form1: TForm1;
  hHook:Integer;
  ClickCount:integer;
  SitePos:^tagMOUSEHOOKSTRUCT;implementation{$R *.dfm}Function MouseC(iCode:Integer;wParam:wParam;lParam:lParam):LRESULT;stdcall;
begin
   Result:=0;
  if iCode<0 then
    Result:=CallNextHookEx(Hhook,iCode,wParam,lParam)
  else
    begin
      if WParam = WM_LButtonDown then begin
        Inc(ClickCount);
        SitePos:=Ptr(LParam);
        Form1.ActTest;      end;
      if ClickCount>2 then begin
        UNHookWindowsHookEx(Hhook);
        Hhook:=0;
        ClickCount:=0;
      end;
    end;
end;procedure TForm1.ActTest;
begin
        Form1.Memo1.Lines.Append('X:='+inttostr(SitePos^.pt.X)+
                   '   '+'Y:='+inttostr(SitePos^.pt.Y));        SHOWMESSAGE('LLLLL');
end;procedure TForm1.Button1Click(Sender: TObject);
begin
   ClickCount:=0;
   //建立一个钩子
   Hhook:=SetWindowsHookEx(WH_Mouse,MouseC,HInstance,0);
end;end.

解决方案 »

  1.   

    HInstance 是什麼東西, 在那裹付值??
      

  2.   

    HInstance: LongWord;          { Handle of this instance }
    在SysInit中声明,表示应用程序的句柄
      

  3.   

    改一下这句!!
    Hhook:=SetWindowsHookEx(WH_JOURNALRECORD,MouseC,HInstance,0);
      

  4.   

    你不能在钩子地回调函数中去释放钩子,当然会出现问题了。如果你一定要在这里释放钩子,你可一给Form发一条消息,用消息来释放钩子就不会有问题了。还有你用showmessage的时候Form会失去焦点钩子就会失去作用。你可一用Dll里去写钩子函数就不会出现这个问题了。