delphi消息发送字符串


本文整理自网络,侵删。

 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);

相关阅读 >>

Delphi中查找指定文件的例程

Delphi android程序启动过程

Delphi 检测网络是否连通

Delphi 检查程序是否在(vm,vpc等)虚拟机运行 Delphi(测试可用)

Delphi shellexecute 打开文件夹

Delphi检测程序是否在优盘上运行

Delphi 用文件流读取文本文件字符串的方法

Delphi 取得任意程序的命令行

Delphi imagelist-图片(bmp)按名称而不按索引

Delphi http post json示例

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...