本人初次接触 钩子和消息 还望大虾耐心指教:
以下代码简单 本人照书搬下   可是编译通不过 请大虾耐心指教了, 多谢!
unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;Const
    WM_TestMessage = WM_User + 2000;type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  Form1: TForm1;implementation{$R *.dfm}var
   HookHandle : HHook;//HOOK处理函数
Function TestHookProc(Code:Integer;WParam:LongInt):LongInt;StdCall;
begin
  if Code = HC_Action then
     if TMsg(Msg)^.message= WM_TestMessage then
     begin
       showmessage('已经截获该消息');
     end;
  //挂钩下一个函数
  Result := CallNextHookEx(HookHandle,Code,WParam,LongInt(@Msg));
end;procedure TForm1.FormCreate(Sender: TObject);
begin //安装HOOK
  HookHandle := SetWindowsHookEx(WH_GetMessage,TestHookProc,0,GetCurrentThreadID);
end;procedure TForm1.Button1Click(Sender: TObject);
begin //发特定消息
  PostMessage(self.Handle,WM_TestMessage,0,0);
end;end.

解决方案 »

  1.   

    我调试通过^_^//HOOK处理函数
    Function TestHookProcfunction(Code: Integer; WParam: Longint; var Msg: TMsg): Longint; stdcall;begin
      if Code = HC_Action then
         if Msg.message = WM_TestMessage then
         begin
           showmessage('已经截获该消息');
         end;
      //挂钩下一个函数
      Result := CallNextHookEx(HookHandle,Code,WParam,Longint(@Msg));
    end;procedure TForm1.FormCreate(Sender: TObject);
    begin //安装HOOK
      HookHandle := SetWindowsHookEx(WH_GetMessage,@TestHookProcfunction,0,GetCurrentThreadID);end;procedure TForm1.Button1Click(Sender: TObject);
    begin //发特定消息
      PostMessage(self.Handle,WM_TestMessage,0,0);
    end;
      

  2.   

    谢谢楼上的大哥 
    不过我还没有达到我的目的啊 
    如果我点击Button1时应该发送消息啊 
    然后调用 函数TestHookProc(Code:Integer;WParam:LongInt;Var Msg:TMsg):LongInt;StdCall;
    弹出消息对话框 showmessage('已经截获该消息');  怎么不能啊 
      

  3.   

    你试试用sendmessage来试试呢,好像postmessage的处理和一般的不同。
    你也可以用delphi包装的钩子函数Application.HookMainWindow(WindowHook);
      

  4.   

    楼主,我测试通过, showmessage('已经截获该消息'); 
    弹出来了