unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Memo2: TMemo;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
procedure Memo1Change(Sender: TObject);
private
{ Private declarations }
public
procedure MyCopyDataMsg(Var Msg : TMessage); Message WM_COPYDATA;//定义函数接收过程
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject); //发送消息过程
var
cdds : TCopyDataStruct;//消息体
begin
cdds.dwData := 0; //dwdata初始化
cdds.cbData := length(memo1.Text)+1;//cbData定义消息体的字符长度,+1个长度,是为了存储结束符
cdds.lpData := pchar(memo1.Text);//告诉消息体,LPDATA的指针指向memo1.Text存储位置的指针
SendMessage(Handle,WM_COPYDATA,0,LongWord(@cdds));//发送消息体
end;
procedure TForm1.Memo1Change(Sender: TObject);
var
cdds : TCopyDataStruct;
begin
cdds.dwData := 0;
cdds.cbData := length(memo1.Text)+1;
cdds.lpData := pchar(memo1.Text);
SendMessage(Handle,WM_COPYDATA,0,LongWord(@cdds));
end;
procedure TForm1.MyCopyDataMsg(var Msg: TMessage); //定义接收消息的具体过程
var
cdds : TcopyDataStruct;
begin
if msg.Msg = WM_COPYDATA then
begin
cdds := PcopyDataStruct(Msg.LParam)^;
memo2.Text := (Pchar(cdds.lpData));
end;
end;
end.