将 WM_COPYDATA 与 Delphi-Prism 应用程序一起使用

发布于 2024-09-24 07:42:20 字数 69 浏览 6 评论 0原文

有谁有接收和解释 WM_COPYDATA 消息的良好 delphi-prism 示例吗?我对如何处理消息数据结构特别感兴趣。

Does anyone have a good delphi-prism example of receiving and interpreting a WM_COPYDATA message? I'm particularly interested in how to deal with the message data structure.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静谧 2024-10-01 07:42:20

Warren,解码消息的关键,取决于您如何定义 COPYDATASTRUCT 在发送方和接收方应用程序中记录并使用 GetLParam 函数。

检查此代码

//the sender application, this is a simple console app
namespace ConsoleApplication_PrismSendData;

interface
uses
    System.Runtime.InteropServices;
type
  [StructLayout(LayoutKind.Sequential)]
  CopyDataStruct = public record //the record to be passed by the WM_COPYDATA message. 
  public
    var     dwData: IntPtr;
    var     cbData: System.Int32;
    var     [MarshalAs(UnmanagedType.LPStr)] //in this case using a pointer to a null-terminated array of ANSI characters.
    lpData: System.String;
  end;

  ConsoleApp = class
  public
    const     WM_COPYDATA: System.Int32 = $4a;//WM_COPYDATA  0x004A
    class method Main(args: array of string);
    [DllImport('user32.dll', EntryPoint := 'FindWindow')]
    class method FindWindow(lpClasName: System.String; lpWindowName: System.String): System.IntPtr; external;
    [DllImport('user32.dll', EntryPoint := 'SendMessage')]
    class method SendMessage(hWnd: System.IntPtr; Msg: System.Int32; wParam: System.Int32; var lParam: CopyDataStruct): System.Int32; external;
  end;


implementation

class method ConsoleApp.Main(args: array of string);
var
 cd   : CopyDataStruct;
 WHnd : IntPtr; 
begin
 cd.dwData := IntPtr(0);//Dummy
 cd.lpData := 'Hello from Delphi Prism - Console Application';//message to send
 cd.cbData := cd.lpData.Length;//set the length of the message
 WHnd      := FindWindow(nil, 'PrismForm');//find the handle for the window of the receiver app
 if (WHnd<>System.IntPtr.Zero) then //check if <> to 0
 begin
  SendMessage(WHnd, WM_COPYDATA, 0, var cd); //Send the message to the app
  Console.WriteLine('Message sent');
 end
 else
 Console.WriteLine('Window not found');
 Console.ReadKey();
end;

end. 

现在是接收器应用程序的代码。

首先,您必须覆盖 WndProc< /code>方法,要做到这一点,你只需编写类似这样的东西。

  MainForm = partial class(System.Windows.Forms.Form) //this is your form
  private
    const     WM_COPYDATA: System.Int32 = $4a; //declare the const for WM_COPYDATA message
  protected
    method Dispose(disposing: Boolean); override;
    method WndProc(var m: Message); override;//override WndProc method to process the messages
  public
    constructor;
  end;

然后声明发送者应用程序的相同结构来处理消息

  [StructLayout(LayoutKind.Sequential)] 
  CopyDataStruct = public record
  public
    var     dwData: IntPtr;
    var     cbData: System.Int32;
    var     [MarshalAs(UnmanagedType.LPStr)]
    lpData: System.String;
  end;

,最后像这样实现 WndProc 方法

method MainForm.WndProc(var m: Message);
var 
cd: CopyDataStruct;
s : System.String;
begin
  case m.Msg of 
    WM_COPYDATA:  
                begin
                  //the  GetLParam function convert the LParam value an object.
                  //the  typeof function obtains the type of an object
                  cd:= m.GetLParam(typeof(CopyDataStruct)) as CopyDataStruct;
                  s := cd.lpData.Substring(0,cd.cbData);
                  // do your stuff here
                end;
  end;
  inherited WndProc(var m);
end;

Warren, the key for decoding the message, depends how you define your COPYDATASTRUCT record in the sender and receiver applications and use the GetLParam function.

check this code

//the sender application, this is a simple console app
namespace ConsoleApplication_PrismSendData;

interface
uses
    System.Runtime.InteropServices;
type
  [StructLayout(LayoutKind.Sequential)]
  CopyDataStruct = public record //the record to be passed by the WM_COPYDATA message. 
  public
    var     dwData: IntPtr;
    var     cbData: System.Int32;
    var     [MarshalAs(UnmanagedType.LPStr)] //in this case using a pointer to a null-terminated array of ANSI characters.
    lpData: System.String;
  end;

  ConsoleApp = class
  public
    const     WM_COPYDATA: System.Int32 = $4a;//WM_COPYDATA  0x004A
    class method Main(args: array of string);
    [DllImport('user32.dll', EntryPoint := 'FindWindow')]
    class method FindWindow(lpClasName: System.String; lpWindowName: System.String): System.IntPtr; external;
    [DllImport('user32.dll', EntryPoint := 'SendMessage')]
    class method SendMessage(hWnd: System.IntPtr; Msg: System.Int32; wParam: System.Int32; var lParam: CopyDataStruct): System.Int32; external;
  end;


implementation

class method ConsoleApp.Main(args: array of string);
var
 cd   : CopyDataStruct;
 WHnd : IntPtr; 
begin
 cd.dwData := IntPtr(0);//Dummy
 cd.lpData := 'Hello from Delphi Prism - Console Application';//message to send
 cd.cbData := cd.lpData.Length;//set the length of the message
 WHnd      := FindWindow(nil, 'PrismForm');//find the handle for the window of the receiver app
 if (WHnd<>System.IntPtr.Zero) then //check if <> to 0
 begin
  SendMessage(WHnd, WM_COPYDATA, 0, var cd); //Send the message to the app
  Console.WriteLine('Message sent');
 end
 else
 Console.WriteLine('Window not found');
 Console.ReadKey();
end;

end. 

Now the code for the receiver app.

first you must override the WndProc method , to do this you just write something like this.

  MainForm = partial class(System.Windows.Forms.Form) //this is your form
  private
    const     WM_COPYDATA: System.Int32 = $4a; //declare the const for WM_COPYDATA message
  protected
    method Dispose(disposing: Boolean); override;
    method WndProc(var m: Message); override;//override WndProc method to process the messages
  public
    constructor;
  end;

then declare the same structure of the sender app to process the message

  [StructLayout(LayoutKind.Sequential)] 
  CopyDataStruct = public record
  public
    var     dwData: IntPtr;
    var     cbData: System.Int32;
    var     [MarshalAs(UnmanagedType.LPStr)]
    lpData: System.String;
  end;

and finally implement the WndProc method like this

method MainForm.WndProc(var m: Message);
var 
cd: CopyDataStruct;
s : System.String;
begin
  case m.Msg of 
    WM_COPYDATA:  
                begin
                  //the  GetLParam function convert the LParam value an object.
                  //the  typeof function obtains the type of an object
                  cd:= m.GetLParam(typeof(CopyDataStruct)) as CopyDataStruct;
                  s := cd.lpData.Substring(0,cd.cbData);
                  // do your stuff here
                end;
  end;
  inherited WndProc(var m);
end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文