突然發現 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.