別のアプリケーションで複数のテキストボックスに値を割り当て、クリックイベントをシミュレートします.

4541 ワード

呼び出されたアプリケーション:
unit Unit2;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TForm2 = class(TForm)
    Button1: TButton;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

procedure TForm2.Button1Click(Sender: TObject);
var
  h_form, h_text1, h_text2, h_button: HWND;
  str1, str2: string;
begin
  //  .
  h_form := findWindow('TForm1', 'Form1');

  //  . ( )
  h_text1 := FindWindowEx(h_form, 0, 'TEdit', nil);
  h_text2 := FindWindowEx(h_form, h_text1, 'TEdit', nil);
  str1 := ' 1!';
  str2 := ' 2!';
  SendMessage(h_text1, WM_SETTEXT, 0, LPARAM(str1));
  SendMessage(h_text2, WM_SETTEXT, 0, LPARAM(str2));

  //  . ( )
  h_button := FindWindowEx(h_form, 0, 'TButton', nil);
  SendMessage(h_button, WM_LBUTTONDOWN, 0, 0);
  SendMessage(h_button, WM_LBUTTONUP, 0, 0);

  label1.Caption := inttohex(h_form, 2);
  label2.Caption := inttohex(h_text1, 2);
  label3.Caption := inttohex(h_text2, 2);
  label4.Caption := inttohex(h_button, 2);
end;

end.
object Form2: TForm2
  Left = 0
  Top = 0
  Caption = 'Form2'
  ClientHeight = 211
  ClientWidth = 408
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 144
    Top = 8
    Width = 57
    Height = 17
    Caption = 'Label1'
  end
  object Label2: TLabel
    Left = 74
    Top = 48
    Width = 57
    Height = 25
    Caption = 'Label2'
  end
  object Label3: TLabel
    Left = 208
    Top = 48
    Width = 79
    Height = 17
    Caption = 'Label3'
  end
  object Label4: TLabel
    Left = 144
    Top = 96
    Width = 79
    Height = 25
    Caption = 'Label4'
  end
  object Button1: TButton
    Left = 144
    Top = 160
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 0
    OnClick = Button1Click
  end
end
によって呼び出されたプログラム:
unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Button1: TButton;
    Label3: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  // showmessage(' !');
  label3.Caption := ' !';
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  label3.Caption := '';
end;

end.
object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 360
  ClientWidth = 486
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 168
    Top = 155
    Width = 40
    Height = 13
    Caption = #29992#25143#21517':'
  end
  object Label2: TLabel
    Left = 168
    Top = 203
    Width = 28
    Height = 13
    Caption = #23494#30721':'
  end
  object Label3: TLabel
    Left = 208
    Top = 288
    Width = 193
    Height = 41
    Caption = 'Label3'
  end
  object Edit1: TEdit
    Left = 224
    Top = 152
    Width = 121
    Height = 21
    ImeName = #20013#25991'('#31616#20307') - '#26497#28857#20116#31508
    TabOrder = 0
  end
  object Edit2: TEdit
    Left = 224
    Top = 200
    Width = 121
    Height = 21
    ImeName = #20013#25991'('#31616#20307') - '#26497#28857#20116#31508
    TabOrder = 1
  end
  object Button1: TButton
    Left = 270
    Top = 248
    Width = 75
    Height = 25
    Caption = #30331#38470
    TabOrder = 2
    OnClick = Button1Click
  end
end