截取两个字符串之间的字符串_如何在两个应用程序之间发送信息(字符串,图像,记录)

截取两个字符串之间的字符串

There are many situation when you need to allow for two applications to communicate. If you do not want to mess with TCP and sockets communication (because both applications are running on the same machine), you can *simply* send (and properly receive) a special Windows message: WM_COPYDATA.

在许多情况下,您需要允许两个应用程序进行通信。 如果您不想弄乱TCP和套接字通信 (因为两个应用程序都在同一台机器上运行),则可以*简单地*发送(并正确接收)一条特殊的Windows消息: WM_COPYDATA

Since handling Windows messages in Delphi is simple, issuing a SendMessage API call along with the WM_CopyData filled with the data to be sent is quite straight forward.

由于在Delphi中处理Windows消息很简单,因此直接发送一个SendMessage API调用以及WM_CopyData填充了要发送的数据。

WM_CopyData和TCopyDataStruct ( WM_CopyData and TCopyDataStruct )

The WM_COPYDATA message enables you to send data from one application to another. The receiving application receives the data in a TCopyDataStruct record. The TCopyDataStruct is defined in the Windows.pas unit and wraps the COPYDATASTRUCT structure that contains the data to be passed.

WM_COPYDATA消息使您可以将数据从一个应用程序发送到另一个应用程序。 接收应用程序接收TCopyDataStruct 记录中的数据。 TCopyDataStruct在Windows.pas单元中定义,并包装COPYDATASTRUCT结构,该结构包含要传递的数据。

Here's the declaration and the description of the TCopyDataStruct record:

这是TCopyDataStruct记录的声明和描述:

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

通过WM_CopyData发送字符串 ( Send a String over WM_CopyData )

For a "Sender" application to send data to "Receiver" the CopyDataStruct must be filled and passed using the SendMessage function. Here's how to send a string value over WM_CopyData:

为了使“发送器”应用程序将数据发送到“接收器”,必须使用SendMessage函数填充并传递CopyDataStruct。 这是通过WM_CopyData发送字符串值的方法:

procedure TSenderMainForm.SendString() ;
var
stringToSend : string;
copyDataStruct : TCopyDataStruct;
begin
stringToSend := 'About Delphi Programming';
copyDataStruct.dwData := 0; //use it to identify the message contents
copyDataStruct.cbData := 1 + Length(stringToSend) ;
copyDataStruct.lpData := PChar(stringToSend) ;
SendData(copyDataStruct) ;
end; 

The SendData custom function locates the receiver using the FindWindow API call:

SendData定制函数使用FindWindow API调用来定位接收者:

procedure TSenderMainForm.SendData(const 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)) ;
end;

In the code above, the "Receiver" application was found using the FindWindow API call by passing the class name of the main form ("TReceiverMainForm") and the caption of the window ("ReceiverMainForm").

在上面的代码中,通过传递主窗体的类名称(“ TReceiverMainForm”)和窗口标题(“ ReceiverMainForm”),使用FindWindow API调用找到了“ Receiver”应用程序。

Note: The SendMessage returns an integer value assigned by the code that handled the WM_CopyData message.

注意:SendMessage返回一个整数值,该整数值由处理WM_CopyData消息的代码分配。

处理WM_CopyData-接收字符串 ( Handling WM_CopyData - Receiving a String )

The "Receiver" application handles the WM_CopyData mesage as in:

“接收器”应用程序按以下方式处理WM_CopyData消息:

type
TReceiverMainForm = class(TForm)
private
procedure WMCopyData(var Msg : TWMCopyData) ; message WM_COPYDATA;
...
implementation
...
procedure TReceiverMainForm.WMCopyData(var Msg: TWMCopyData) ;
var
s : string;
begin
s := PChar(Msg.CopyDataStruct.lpData) ;
//Send something back
msg.Result := 2006;
end; 

The TWMCopyData record is declared as:

TWMCopyData记录声明为:

TWMCopyData = packed record
Msg: Cardinal;
From: HWND;//Handle of the Window that passed the data
CopyDataStruct: PCopyDataStruct; //data passed
Result: Longint;//Use it to send a value back to the "Sender"
end; 

正在发送字符串,自定义记录还是图像? ( Sending String, Custom Record or an Image? )

The accompanying source code demonstrates how to send a string, record (complex data type) and even graphics (bitmap) to another application.

随附的源代码演示了如何将字符串,记录(复杂的数据类型)甚至图形(位图)发送到另一个应用程序。

If you cannot wait the download, here's how to send a TBitmap graphics:

如果您不能等待下载,请按照以下步骤发送TBitmap图形:

procedure TSenderMainForm.SendImage() ;
var
ms : TMemoryStream;
bmp : TBitmap;
copyDataStruct : TCopyDataStruct;
begin
ms := TMemoryStream.Create;
try
bmp := self.GetFormImage;
try
bmp.SaveToStream(ms) ;
finally
bmp.Free;
end;
copyDataStruct.dwData := Integer(cdtImage) ; // identify the data
copyDataStruct.cbData := ms.Size;
copyDataStruct.lpData := ms.Memory;
SendData(copyDataStruct) ;
finally
ms.Free;
end;
end;

And how to receive it:

以及如何接收它:

procedure TReceiverMainForm.HandleCopyDataImage(
copyDataStruct: PCopyDataStruct) ;
var
ms: TMemoryStream;
begin
ms := TMemoryStream.Create;
try
ms.Write(copyDataStruct.lpData^, copyDataStruct.cbData) ;
ms.Position := 0;
receivedImage.Picture.Bitmap.LoadFromStream(ms) ;
finally
ms.Free;
end;
end; 

翻译自: https://www.thoughtco.com/send-information-between-applications-1058476

截取两个字符串之间的字符串

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值