这个帖子说下如何自定义消息,既然我们已经了解了delphi封装的消息,现在来看看,如何自定义一个消息,直接看代码吧。
unit Unit2;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
Vcl.StdCtrls;
const
// WM_USER 是目前最大的一个消息常量了,我们在这之上再加个1000吧,后面的消息可以加1001,1002
WM_MYMSG = WM_USER + 1000;
type
TForm2 = class(TForm)
btn1: TButton;
procedure btn1Click(Sender: TObject);
private
{ Private declarations }
procedure OnMymsg(var Message: TMessage); message WM_MYMSG;
public
{ Public declarations }
end;
var
Form2: TForm2;
implementation
{$R *.dfm}
{ TForm2 }
procedure TForm2.btn1Click(Sender: TObject);
begin
Sendmessage(self.Handle, WM_MYMSG, 0, 0);
end;
procedure TForm2.OnMymsg(var Message: TMessage);
begin
ShowMessage('my msg start....');
end;
end.