突然发现 Message 单元的最后给提供了几个方便的消息发送函数:
SendTextMessage、SendStructMessage、SendGetStructMessage、SendGetIntMessage
虽然函数实现的很简单, 但用他们发送文本、结构和整数, 的确方便了不少; 下面仅就 SendTextMessage 举例:
//例子是通过发送消息的方法给 Edit 赋值: unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Edit1: TEdit; Button1: TButton; Button2: TButton; Button3: TButton; Button4: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); procedure Button4Click(Sender: TObject); end; var Form1: TForm1; implementation {$R *.dfm} //使用传统的 SendMessage procedure TForm1.Button1Click(Sender: TObject); var str: string; begin str := '万一1'; SendMessage(Edit1.Handle, WM_SETTEXT, 0, Integer(PChar(str))); end; //使用 VCL 的 Perform procedure TForm1.Button2Click(Sender: TObject); var str: string; begin str := '万一2'; Edit1.Perform(WM_SETTEXT, 0, Integer(PChar(str))); end; //使用 SendTextMessage 的确方便 procedure TForm1.Button3Click(Sender: TObject); var str: string; begin str := '万一3'; SendTextMessage(Edit1.Handle, WM_SETTEXT, 0, str); end; //SendTextMessage 也可接受字符串常数 procedure TForm1.Button4Click(Sender: TObject); begin SendTextMessage(Edit1.Handle, WM_SETTEXT, 0, '万一4'); end; end.