delphi消息發送字符串
其實不論什麼方法,歸根揭底都是經過傳遞對象的指針來達到效果的。
方法一:
procedure SendString(strMSG: string);
var
Data: tagCOPYDATASTRUCT;
pBuf: PChar;
begin
GetMem(pBuf, Length(strMSG) + 1);
try
ZeroMemory(pBuf, Length(strMSG) + 1);
StrPCopy(pBuf, strMSG);
Data.cbData:= Length(strMSG);
Data.dwData:= Length(strMSG);
Data.lpData:= pBuf;
SendMessage(hTargetWin, WM_COPYDATA, Integer(Self.Handle), Integer(@Data));
finally
FreeMem(pBuf);
end;
end;
procedure WMCopyData(var MSG: TMessage); message WM_COPYDATA;
procedure TForm1.WMCopyData(var MSG: TMessage);
var
Data : ^tagCOPYDATASTRUCT;
strMSG: string;
begin
Data:= Pointer(Msg.lParam);
strMSG:= StrPas(Data.lpData);
ShowMessage(strMSG);
end;
方法二:
TMyRecord=record s:string; end;
tt:TMyRecord;
var tt:TMyRecord; begin tt.s:='s2343243'; PostMessage(handle,WM_My,integer(tt),5); end;
發送消息,因爲參數只能是一個integer,你這樣發只能發4個字節,因此要改爲髮指針,好比:
var tt:TMyRecord; begin tt.s:='s2343243'; PostMessage(handle,WM_My,integer(@tt),5); end;
接收的時候固然也不能用原來的方法:My:=TMyRecord(msg.WParam);這樣取回來了,也要把TMyRecord聲明成指針:
type PMyRecord=^TMyRecord; var My:PMyRecord; my:=PMyRecord(msg.WParam);html
http://www.delphitop.com/html/jiqiao/2170.html指針