使用RegisterWindowsMessage注册一个系统消息,该如何使用呢

解决方案 »

  1.   

    这是《Delphi Win32核心API参考》第三章的一个例子,分为两个程序,一个Broadcast消息,一个接收消息;
    unit BroadcastSystemMessageU;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;
      UserMessage: UINT;implementation{$R *.DFM}procedure TForm1.Button1Click(Sender: TObject);
    var
      Recipients: DWORD;  // holds the recipient flags
    begin
      {set the recipients to all applications}
      Recipients := BSM_APPLICATIONS;  {send the user defined message to all applications on the system by
       posting it to their message queues}
      BroadcastSystemMessage(BSF_IGNORECURRENTTASK or BSF_POSTMESSAGE, @Recipients,
                             UserMessage, 0, 0);
    end;procedure TForm1.FormCreate(Sender: TObject);
    begin
      {register a user defined message}
      UserMessage := RegisterWindowMessage('CallWindowProc Test Message');
    end;end.
    接收:
    unit CallWindowProcU;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls, ComCtrls;type
      TForm1 = class(TForm)
        Label1: TLabel;
        ProgressBar1: TProgressBar;
        Label2: TLabel;
        procedure FormCreate(Sender: TObject);
        procedure FormDestroy(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;  {the prototype for the new window procedure}
      function NewWindowProc(TheWindow: HWND; Msg: Integer; wParam: WPARAM;
                             lParam: LPARAM): Longint; stdcall;var
      Form1: TForm1;
      UserMessage: UINT;         // holds a user defined message identifier
      OldWindowProc: TFNWndProc; // holds a pointer to the previous window procedureimplementation{$R *.DFM}function NewWindowProc(TheWindow: HWND; Msg: Integer; wParam: WPARAM; lParam: LPARAM): Longint;
    var
      iLoop: Integer;         // a general loop counter
    begin
      {if the user defined message has been received...}
      if Msg=UserMessage then
      begin
        {...turn on some user interface elements}
        Form1.ProgressBar1.Visible := TRUE;
        Form1.Label2.Visible := TRUE;
        Form1.Repaint;    {animate the progress bar for a short period of time}
        for iLoop := 0 to 100 do
        begin
          Form1.ProgressBar1.Position := iLoop;
          Sleep(10);
        end;    {turn off the user interface elements}
        Form1.ProgressBar1.Visible := FALSE;
        Form1.Label2.Visible := FALSE;    {the message was handled, so return a one}
        Result := 1;
      end
      else
        {any other message must be passed to the previous window procedure}
        Result := CallWindowProc(OldWindowProc, TheWindow, Msg, wParam, lParam);
    end;procedure TForm1.FormCreate(Sender: TObject);
    begin
      {register a user defined message}
      UserMessage := RegisterWindowMessage('CallWindowProc Test Message');  {subclass this window.  replace the window procedure with one of
       ours.  this window procedure will receive messages before the
       previous one, allowing us to intercept and process any message
       before the rest of the application ever sees it.}
      OldWindowProc := TFNWndProc(SetWindowLong(Form1.Handle, GWL_WNDPROC,
                                  Longint(@NewWindowProc)));
    end;procedure TForm1.FormDestroy(Sender: TObject);
    begin
      {reset the window procedure to the previous one}
      SetWindowLong(Form1.Handle, GWL_WNDPROC, Longint(OldWindowProc));
    end;end.
      

  2.   

    发送端:
    var
      HOOK_EVENT: Integer;HOOK_EVENT:=RegisterWindowMessage(PChar('HOOK_EVENT'));
    sendmessage(findwindow(nil,'form2'),HOOK_EVENT,0,0);接收端:
        procedure WndProc(var Mess: TMessage); override;var
      HOOK_EVENT: Integer;
    procedure TForm2.FormCreate(Sender: TObject);
    begin
    HOOK_EVENT:=RegisterWindowMessage(PChar('HOOK_EVENT'));
    end;procedure OnHookEvent(wParam,LParam: Longint);
    begin
    {
    处理代码
    }
    end;procedure TForm2.WndProc(var Mess: TMessage);
    begin
      if mess.Msg = HOOK_EVENT then
        OnHookEvent(Mess.WParam, Mess.LParam)
      else
        inherited;
    end;