一:发送方
View Code
var
hwnd:THandle;
cpStruct:COPYDATASTRUCT;
begin
hwnd:=FindWindow(nil,PChar(FORMNAME));
if hwnd<>0 then
begin
cpStruct.dwData:=0;
cpStruct.cbData:=length('test')+1;
cpStruct.lpData:=pchar('test');
SendMessage(Handle,WM_COPYDATA,0,Cardinal(@cpStruct));//lParam和wParam的顺序不能交换,具体原因还未找到
end;
end;
hwnd:THandle;
cpStruct:COPYDATASTRUCT;
begin
hwnd:=FindWindow(nil,PChar(FORMNAME));
if hwnd<>0 then
begin
cpStruct.dwData:=0;
cpStruct.cbData:=length('test')+1;
cpStruct.lpData:=pchar('test');
SendMessage(Handle,WM_COPYDATA,0,Cardinal(@cpStruct));//lParam和wParam的顺序不能交换,具体原因还未找到
end;
end;
二:接收方
View Code
procedure WMCopyData(var Message: TWMCopyData); message WM_COPYDATA;
procedure TForm1.WMCopyData(var Message: TWMCopyData);
begin
if message.Msg=WM_COPYDATA then
Self.Text:=PChar(message.CopyDataStruct.lpData)
else
ShowMessage('方法不对');
end;
procedure TForm1.WMCopyData(var Message: TWMCopyData);
begin
if message.Msg=WM_COPYDATA then
Self.Text:=PChar(message.CopyDataStruct.lpData)
else
ShowMessage('方法不对');
end;