这里采用自定义消息的方法。
注意:delphi中自定义消息值范围为:WM_USER到$7FFF之间。
1.应用程序中的消息(在同一个应用程序的不同Form间进行消息处理)示例:
Form1:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
const
SX_MY_MESSAGE = WM_USER + 100;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses Unit2;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
Form2.Show
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
//Form2.Perform(SX_MY_MESSAGE, 0, 0);
//SendMessage(Form2.Handle, SX_MY_MESSAGE, 0, 0);
PostMessage(Form2.Handle, SX_MY_MESSAGE, 0, 0);
end;
end.
Form2:
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;
const
SX_MY_MESSAGE = WM_USER + 100;
type
TForm2 = class(TForm)
private
{ Private declarations }
procedure SXMyMessage(var Msg : TMessage); message SX_MY_MESSAGE;
public
{ Public declarations }
end;
var
Form2: TForm2;
implementation
{$R *.dfm}
procedure TForm2.SXMyMessage(var Msg : TMessage);
begin
MessageDlg('Receive My Message!', mtInformation, [mbOK], 0);
end;
end.
2.应用程序间的消息
利用RegisterWindowMessage在不同程序窗体间传递消息
转自:http://blog.youkuaiyun.com/xiaowenjie/archive/2007/03/10/1525870.aspx
3.广播消息
TWinControl的派生类可以广播一条消息记录给他拥有的控件,方法名为Broadcast()。
例如:
var
M : TMessage;
begin
with M do
begin
Message := 0;
wParam := 0;
lParam := 0;
Result := 0;
end;
Panel1.Breadcast(M);
end;