此代码曾经与 Delphi 5 一起工作,但与 delphi XE2 并没有按预期工作。使用 wm_copydata 传递的字符串将被剪切。
procedure SendAppParameters(aMsgStr: string);
var
hwnd: THandle;
cds: CopyDataStruct;
begin
hwnd := FindWindow('TMyAppFormMain', nil); //<-- Substitute window classname with your own
if hwnd <> 0 then
begin
// showmessage(aMsgStr);
// prepare the data to copy
cds.dwData := 0;
cds.cbData := length(AMsgStr);
cds.lpData := PChar(AMsgStr);
// activate the destination window
SetForegroundWindow(hwnd);
// send the data to the first instance using a wm_CopyData message
SendMessage(hwnd, wm_CopyData, Application.Handle, integer(@cds));
end
end;
在主窗体中,我有:
procedure TMyAppFormMain.GotMessage_CopyData(var Msg: TWmCopyData);
var
MsgString: string;
I: Integer;
begin
MsgString := PChar(Msg.CopyDataStruct.lpData);
showmessage(MsgString);
end;