copyform.pas

来自「Delphi高级开发指南是开发程序的好帮手」· PAS 代码 · 共 73 行

PAS
73
字号
unit CopyForm;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    BtnSendCopyData: TButton;
    Button1: TButton;
    procedure BtnSendCopyDataClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.BtnSendCopyDataClick(Sender: TObject);
var
  Cds: TCopyDataStruct;
  Hwnd: THandle;
begin
  // small data (32 bits): top of form
  Cds.dwData := Top;
  // size of data
  Cds.cbData := Length (Caption) + 1;
  // allocate memory for the large block and fill it
  GetMem (Cds.lpData, Cds.cbData );
  StrCopy (Cds.lpData, PChar (Caption));
  // get the handle of the target window
  Hwnd := FindWindow ('TFormGetData', 'GetData');
  if Hwnd <> 0 then
  begin
    // send the message
    if SendMessage (
        Hwnd, WM_COPYDATA, Handle, Cardinal(@Cds)) = 1 then
      // received
      Beep;
  end
  else
    // target not found
    ShowMessage ('GetData window not found.');
  FreeMem (Cds.lpData);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  Hwnd: THandle;
begin
  // get the handle of the target window
  Hwnd := FindWindow ('TFormGetData', 'GetData');
  if Hwnd <> 0 then
    // send the plain string
    SendMessage (Hwnd, WM_USER, 0,
      Integer (PChar (Caption)))
  else
    // target not found
    ShowMessage ('GetData window not found.');
end;

end.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?