首页 > 代码库 > 全局消息

全局消息

unit Unit1;
interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;
type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    Msg: LongInt;
    { Private declarations }
  protected
   //重载系统消息监听函数
    procedure WndProc(var Message: TMessage); override;
  public
    { Public declarations }
  end;
var
  Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
//注册一个系统级消息,消息名字最好独一无二,否则不能返回的消息id给msg变量
Msg := RegisterWindowMessage(‘WINDOWS_SYSTEM_TINGHUATONGFANGPC_DIGITALEDU_GQ‘);
end;
procedure TForm1.WndProc(var Message: TMessage);
begin
  if( Message.Msg = Msg) and (Message.WParam=1) then begin
    Caption := ‘‘;
    ShowMessage(‘收到消息了‘);
  end
  else begin
    inherited;
  end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
  M: TMessage;
  B: DWord;
begin
  M.Msg := Msg;
  //向整个PC发送全局消息
  B := BSM_ALLCOMPONENTS;
  //(BSF_POSTMESSAGE表示使用POST方式发送
  BroadcastSystemMessage(BSF_POSTMESSAGE, @B , M.Msg, 1, 0);
end;
end.

全局消息