zoukankan      html  css  js  c++  java
  • 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);

    http://www.delphitop.com/html/jiqiao/2170.html

  • 相关阅读:
    Android 黑科技保活实现原理揭秘
    Flutter +携程=?
    图解设计模式-Prototype模式
    图解设计模式-Singleton模式
    比较B-tree索引与Hash索引
    类元数据Class Metadata
    DriverManager类
    XMLMapperBuilder类
    PooledDataSource类
    Java并发编程的艺术(笔记)
  • 原文地址:https://www.cnblogs.com/ziliudi/p/5070903.html
Copyright © 2011-2022 走看看