2、在要实现的unit中定义一个私有方法
procedure doMyMessage(var msg:TMessage);message WM_MyMessage;
3、实现这个私有方法
procedure TForm1.doMyMessage(var msg:TMessage);
begin
//
if msg.Msg= WM_MyMessage then
showmessage('好啊')
else
showmessage('不好');
end;
4、最重要 把这个消息广播出去 Form1.Perform(WM_MyMessage,0,0);
下面是实现的代码
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
const WM_MyMessage=WM_USER+$200;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
procedure doMyMessage(var msg:TMessage);message WM_MyMessage;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.doMyMessage(var msg:TMessage);
begin
//
if msg.Msg= WM_MyMessage then
showmessage('好啊')
else
showmessage('不好');
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Form1.Perform(WM_MyMessage,0,0);
end;
end.