实例1
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;
const
WM_ME=WM_USER+100; //自定义消息;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
//第一种消息处理方式; 只能处理常量消息;
procedure wmme(var message:TMessage);message WM_ME; //自定义消息处理过程,专门处理WM_ME消息; private
{ Private declarations }
public
{ Public declarations }
//第二种消息处理方式;可能处理常量或变量消息;
procedure WndProc(var message:TMessage);override; //重载窗口消息过程
//第三种消息处理方式
procedure WMCommand(var Message: TWMCommand); message WM_COMMAND; //命令消息处理过程
procedure WMSysCommand(var Msg:TWMSysCommand);message WM_SYSCOMMAND; //处理系统性消息;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
SendMessage(Handle,WM_ME,0,0); //发送消息WM_ME ; 消息先由WndProc处理,再交给wmme处理;
end;
procedure TForm1.WMSysCommand(var Msg: TWMSysCommand);
begin
//下面代码的作用是,用户如果点击了标题栏上的最小化和关闭按钮,则隐藏窗体。
if (Msg.CmdType=SC_MINIMIZE) or (Msg.CmdType=SC_CLOSE) then
begin
Self.Hide;
end else
DefaultHandler(Msg); //这句的作用是继续处理其它消息;
end;
procedure TForm1.WMCommand(var Message: TWMCommand);
begin //第三种消息处理方式
if Message.NotifyCode = BN_CLICKED then
if FindControl(Message.Ctl) = Button1 then showmessage(‘点击了Button1’);
inherited;
end;
procedure TForm1.wmme(var message: TMessage);
begin
ShowMessage(IntToStr(Handle)+ ‘wmme‘); //第一种消息处理方式
end;
procedure TForm1.WndProc(var message: TMessage);
begin
if message.Msg=WM_ME then //第二种消息处理方式
ShowMessage(IntToStr(Handle)+ ‘WndProc‘);
inherited WndProc(Message); //这里inherited才会触发一次wmme消息;
end;
end.
实例2/////////////////////////////////////////////////////////////消息的广播方式/////////////////////////////////////////////////////////////////////////////
///////消息广播只能将消息传递到接收消息的主程序中,MDIChild窗体不能接收到广播消息;/////////
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, 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 }
procedure WndProc(var message:TMessage);override;
end;
var
Form1: TForm1;
MyMessage: DWORD;
implementation
{$R *.dfm}
{ TForm1 }
procedure TForm1.WndProc(var message: TMessage);
begin
if message.Msg=MyMessage then //第3步:重载Windows消息处理过程,处理接收到的MyMessage消息;
ShowMessage(FloatToStr(Handle)+ ‘MyMessage‘);
inherited WndProc(Message);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
SendMessage(HWND_BROADCAST,MyMessage,0,0); //第2步:广播MyMessage消息;
end;
/////////预先注册好Windows消息;第一种方法////////////////////
procedure TForm1.FormCreate(Sender: TObject);
begin
//同一字符串,如‘MyMessageMe‘在不同的程序中调用RegisterWindowMessage注册消息时,返回的MyMessage结果是一样的。这样为在不同程序之间广播消息提供了可能。即不同程序注册消息时,必须注册相同的注符串。
MyMessage:=RegisterWindowMessage(‘MyMessageMe‘); //第1步:注册Windows消息,重点在于返回的值MyMessage
end;
/////////预先注册好Windows消息;第二种方法////////////////////
{ initialization
MyMessage:=RegisterWindowMessage(‘MyMessageMe‘);
}
//BroadcastSystemMessage,可以在进程之间广播消息;
procedure TForm1.SendSMSMessage;
var
SMSMessage:Cardinal;
recipt:Cardinal;
begin
try
SMSMessage:=RegisterWindowMessage(‘SendSMSMessage‘);
recipt:=BSM_ALLDESKTOPS; //所以桌面程序都可以接收
BroadcastSystemMessage(BSF_POSTMESSAGE,@recipt,SMSMessage,0,0);
except
end;
end;
end.