WM_COPYDATA
Sender:
代码
Receiver
unit SenderMain;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TCopyDataType = (cdtString = 0, cdtImage = 1, cdtRecord = 2);
TSampleRecord = packed record
s : string[50];
i : integer;
d : TDateTime;
end;
TSenderMainForm = class(TForm)
SendDataButton: TButton;
rgCopyOptions: TRadioGroup;
procedure SendDataButtonClick(Sender: TObject);
private
procedure SendData(copyDataStruct : TCopyDataStruct);
procedure SendString();
procedure SendImage();
procedure SendRecord();
public
{ Public declarations }
end;
var
SenderMainForm: TSenderMainForm;
implementation
{$R *.dfm}
procedure TSenderMainForm.SendData(
copyDataStruct: TCopyDataStruct);
var
receiverHandle : THandle;
res : integer;
begin
receiverHandle := FindWindow(PChar('TReceiverMainForm'),PChar('ReceiverMainForm'));
if receiverHandle = 0 then
begin
ShowMessage('CopyData Receiver NOT found!');
Exit;
end;
res := SendMessage(receiverHandle, WM_COPYDATA, Integer(Handle), Integer(@copyDataStruct));
if res > 0 then ShowMessage(Format('Receiver has %d lines in Memo!',[res]));
end;
procedure TSenderMainForm.SendDataButtonClick(Sender: TObject);
begin
if rgCopyOptions.ItemIndex = -1 then
begin
ShowMessage('Nothing selected!');
Exit;
end;
case rgCopyOptions.ItemIndex of
0 : SendString;
1 : SendImage;
2 : SendRecord;
end;
end;
procedure TSenderMainForm.SendImage();
var
ms : TMemoryStream;
bmp : TBitmap;
copyDataStruct : TCopyDataStruct;
begin
ms := TMemoryStream.Create;
try
bmp := self.GetFormImage;
try
bmp.SaveToStream(ms);
finally
bmp.FreeImage;
end;
copyDataStruct.dwData := Integer(cdtImage); //use it to identify the message contents
copyDataStruct.cbData := ms.Size;
copyDataStruct.lpData := ms.Memory;
SendData(copyDataStruct);
finally
ms.Free;
end;
end;
procedure TSenderMainForm.SendRecord();
var
sampleRecord : TSampleRecord;
copyDataStruct : TCopyDataStruct;
begin
sampleRecord.s := 'Hello Receiver';
sampleRecord.i := 1982;
sampleRecord.d := Now;
copyDataStruct.dwData := Integer(cdtRecord); //use it to identify the message contents
copyDataStruct.cbData := SizeOf(sampleRecord);
copyDataStruct.lpData := @sampleRecord;
SendData(copyDataStruct);
end;
procedure TSenderMainForm.SendString();
var
stringToSend : string;
copyDataStruct : TCopyDataStruct;
begin
stringToSend := 'About Delphi Programming';
copyDataStruct.dwData := Integer(cdtString); //use it to identify the message contents
copyDataStruct.cbData := 1 + Length(stringToSend);
copyDataStruct.lpData := PChar(stringToSend);
SendData(copyDataStruct);
end;
end.
代码
unit ReceiverMain;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TCopyDataType = (cdtString = 0, cdtImage = 1, cdtRecord = 2);
TSampleRecord = packed record
s : string[50];
i : integer;
d : TDateTime;
end;
TReceiverMainForm = class(TForm)
cdMemo: TMemo;
receivedImage: TImage;
procedure FormCreate(Sender: TObject);
private
procedure WMCopyData(var Msg : TWMCopyData); message WM_COPYDATA;
procedure HandleCopyDataString(copyDataStruct : PCopyDataStruct);
procedure HandleCopyDataImage(copyDataStruct : PCopyDataStruct);
procedure HandleCopyDataRecord(copyDataStruct : PCopyDataStruct);
public
{ Public declarations }
end;
var
ReceiverMainForm: TReceiverMainForm;
implementation
{$R *.dfm}
{ TReceiverMainForm }
procedure TReceiverMainForm.FormCreate(Sender: TObject);
begin
cdMemo.Clear;
end;
procedure TReceiverMainForm.HandleCopyDataImage(
copyDataStruct: PCopyDataStruct);
var
ms: TMemoryStream;
begin
ms := TMemoryStream.Create;
try
ms.Write(copyDataStruct.lpData^, copyDataStruct.cbData);
ms.Position := 0;
receivedImage.Picture.Bitmap.LoadFromStream(ms);
finally
ms.Free;
end;
cdMemo.Lines.Add(Format('Received image at %s',[DateToStr(Now)]));
end;
procedure TReceiverMainForm.HandleCopyDataRecord(
copyDataStruct: PCopyDataStruct);
var
sampleRecord : TSampleRecord;
begin
sampleRecord.s := TSampleRecord(copyDataStruct.lpData^).s;
sampleRecord.i := TSampleRecord(copyDataStruct.lpData^).i;
sampleRecord.d := TSampleRecord(copyDataStruct.lpData^).d;
cdMemo.Lines.Add(Format('Received record at %s',[DateToStr(Now)]));
cdMemo.Lines.Add(Format('sampleRecord.s = %s',[sampleRecord.s]));
cdMemo.Lines.Add(Format('sampleRecord.i = %d',[sampleRecord.i]));
cdMemo.Lines.Add(Format('sampleRecord.d = %s',[DateToStr(sampleRecord.d)]));
end;
procedure TReceiverMainForm.HandleCopyDataString(
copyDataStruct: PCopyDataStruct);
var
s : string;
begin
s := PChar(copyDataStruct.lpData);
cdMemo.Lines.Add(Format('Received string "%s" at %s',[s, DateToStr(Now)]));
end;
procedure TReceiverMainForm.WMCopyData(var Msg: TWMCopyData);
var
copyDataType : TCopyDataType;
begin
copyDataType := TCopyDataType(Msg.CopyDataStruct.dwData);
//Handle of the Sender
cdMemo.Lines.Add(Format('WM_CopyData from: %d',[msg.From]));
case copyDataType of
cdtString: HandleCopyDataString(Msg.CopyDataStruct);
cdtImage: HandleCopyDataImage(Msg.CopyDataStruct);
cdtRecord: HandleCopyDataRecord(Msg.CopyDataStruct);
end;
//Send something back
msg.Result := cdMemo.Lines.Count;
end;
end.