sendmessage如何发送结构体数据
我的代码如下
- Delphi(Pascal) code
. type TDATA = record MissionName :string; sTime :string; Completed :string; Mission :string; MissionCnt :string; end;......var DATA:^TDATA;begin New(DATA); DATA^.MissionName:=edtMissionName.Text; //,编译没问题,运行到这里就出错 DATA^.Completed:='0'; DATA^.sTime:=DateToStr(DateTimePicker1.Date) + ' ' + edtTime.Text; DATA^.Mission:=mmoMission.Text; DATA^.MissionCnt:=MmoCnt.Text; SendMessage(FrmMission.Handle,MyMessage,0,lparam(DATA));
[解决办法]
Record的使用有问题罢?跟发送消息无关
DATA后面的尖括号去掉:)
type
myData=^TData;
TDATA = record
MissionName :string;
sTime :string;
Completed :string;
Mission :string;
MissionCnt :string;
end;
调用:
var ss:myData;
begin
new(ss);
ss.MissionName:='test';
dispose(ss);
end;
[解决办法]
楼主自定义消息发送内容,请参看这个单元,使用的WM_COPYDATA消息,他用的是两个应用程序之间的通信:)道理是一样的,我就不再照着去改写了
请看这个单元的SendData方法,界面很简单:就是发送0:字符串;1:Image;2:Record;消息的Handle有所变化
unit SenderMain;
{
How to send information (String, Image, Record) between two Delphi applications
http://delphi.about.com/od/windowsshellapi/a/wm_copydata.htm
Learn how to send the WM_CopyData message between two Delphi
applications to exchange information and make two applications
communicate. The accompanying source code demonstrates how to
send a string, record (complex data type) and even graphics
to another application.
~Zarko Gajic
About Delphi Programming
http://delphi.about.com
}
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
(*
Declared in Windows.pas
TCopyDataStruct = packed record
dwData: DWORD; //up to 32 bits of data to be passed to the receiving application
cbData: DWORD; //the size, in bytes, of the data pointed to by the lpData member
lpData: Pointer; //Points to data to be passed to the receiving application. This member can be nil.
end;
*)
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 := 1973;
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.