通过另外一个应用程序给多个文本框赋值, 模拟单击事件

被调用的应用程序:

[delphi] view plain copy
 
 print?
  1. unit Unit2;  
  2.   
  3. interface  
  4.   
  5. uses  
  6.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,  
  7.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;  
  8.   
  9. type  
  10.   TForm2 = class(TForm)  
  11.     Button1: TButton;  
  12.     Label1: TLabel;  
  13.     Label2: TLabel;  
  14.     Label3: TLabel;  
  15.     Label4: TLabel;  
  16.     procedure Button1Click(Sender: TObject);  
  17.   private  
  18.     { Private declarations }  
  19.   public  
  20.     { Public declarations }  
  21.   end;  
  22.   
  23. var  
  24.   Form2: TForm2;  
  25.   
  26. implementation  
  27.   
  28. {$R *.dfm}  
  29.   
  30. procedure TForm2.Button1Click(Sender: TObject);  
  31. var  
  32.   h_form, h_text1, h_text2, h_button: HWND;  
  33.   str1, str2: string;  
  34. begin  
  35.   // 获取窗口的句柄.  
  36.   h_form := findWindow('TForm1', 'Form1');  
  37.   
  38.   // 获取文本框的句柄. (注意多个)  
  39.   h_text1 := FindWindowEx(h_form, 0, 'TEdit', nil);  
  40.   h_text2 := FindWindowEx(h_form, h_text1, 'TEdit', nil);  
  41.   str1 := '本文1!';  
  42.   str2 := '本文2!';  
  43.   SendMessage(h_text1, WM_SETTEXT, 0, LPARAM(str1));  
  44.   SendMessage(h_text2, WM_SETTEXT, 0, LPARAM(str2));  
  45.   
  46.   // 获取按钮的句柄. (注意多个)  
  47.   h_button := FindWindowEx(h_form, 0, 'TButton', nil);  
  48.   SendMessage(h_button, WM_LBUTTONDOWN, 0, 0);  
  49.   SendMessage(h_button, WM_LBUTTONUP, 0, 0);  
  50.   
  51.   label1.Caption := inttohex(h_form, 2);  
  52.   label2.Caption := inttohex(h_text1, 2);  
  53.   label3.Caption := inttohex(h_text2, 2);  
  54.   label4.Caption := inttohex(h_button, 2);  
  55. end;  
  56.   
  57. end.  
[cpp] view plain copy
 
 print?
  1. object Form2: TForm2  
  2.   Left = 0  
  3.   Top = 0  
  4.   Caption = 'Form2'  
  5.   ClientHeight = 211  
  6.   ClientWidth = 408  
  7.   Color = clBtnFace  
  8.   Font.Charset = DEFAULT_CHARSET  
  9.   Font.Color = clWindowText  
  10.   Font.Height = -11  
  11.   Font.Name = 'Tahoma'  
  12.   Font.Style = []  
  13.   OldCreateOrder = False  
  14.   PixelsPerInch = 96  
  15.   TextHeight = 13  
  16.   object Label1: TLabel  
  17.     Left = 144  
  18.     Top = 8  
  19.     Width = 57  
  20.     Height = 17  
  21.     Caption = 'Label1'  
  22.   end  
  23.   object Label2: TLabel  
  24.     Left = 74  
  25.     Top = 48  
  26.     Width = 57  
  27.     Height = 25  
  28.     Caption = 'Label2'  
  29.   end  
  30.   object Label3: TLabel  
  31.     Left = 208  
  32.     Top = 48  
  33.     Width = 79  
  34.     Height = 17  
  35.     Caption = 'Label3'  
  36.   end  
  37.   object Label4: TLabel  
  38.     Left = 144  
  39.     Top = 96  
  40.     Width = 79  
  41.     Height = 25  
  42.     Caption = 'Label4'  
  43.   end  
  44.   object Button1: TButton  
  45.     Left = 144  
  46.     Top = 160  
  47.     Width = 75  
  48.     Height = 25  
  49.     Caption = 'Button1'  
  50.     TabOrder = 0  
  51.     OnClick = Button1Click  
  52.   end  
  53. end  

调用的程序:

[delphi] view plain copy
 
 print?
  1. unit Unit1;  
  2.   
  3. interface  
  4.   
  5. uses  
  6.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,  
  7.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;  
  8.   
  9. type  
  10.   TForm1 = class(TForm)  
  11.     Edit1: TEdit;  
  12.     Edit2: TEdit;  
  13.     Label1: TLabel;  
  14.     Label2: TLabel;  
  15.     Button1: TButton;  
  16.     Label3: TLabel;  
  17.     procedure Button1Click(Sender: TObject);  
  18.     procedure FormCreate(Sender: TObject);  
  19.   private  
  20.     { Private declarations }  
  21.   public  
  22.     { Public declarations }  
  23.   end;  
  24.   
  25. var  
  26.   Form1: TForm1;  
  27.   
  28. implementation  
  29.   
  30. {$R *.dfm}  
  31.   
  32. procedure TForm1.Button1Click(Sender: TObject);  
  33. begin  
  34.   // showmessage('登陆成功!');  
  35.   label3.Caption := '登陆成功!';  
  36. end;  
  37.   
  38. procedure TForm1.FormCreate(Sender: TObject);  
  39. begin  
  40.   label3.Caption := '';  
  41. end;  
  42.   
  43. end.  
[cpp] view plain copy
 
 print?
  1. object Form1: TForm1  
  2.   Left = 0  
  3.   Top = 0  
  4.   Caption = 'Form1'  
  5.   ClientHeight = 360  
  6.   ClientWidth = 486  
  7.   Color = clBtnFace  
  8.   Font.Charset = DEFAULT_CHARSET  
  9.   Font.Color = clWindowText  
  10.   Font.Height = -11  
  11.   Font.Name = 'Tahoma'  
  12.   Font.Style = []  
  13.   OldCreateOrder = False  
  14.   OnCreate = FormCreate  
  15.   PixelsPerInch = 96  
  16.   TextHeight = 13  
  17.   object Label1: TLabel  
  18.     Left = 168  
  19.     Top = 155  
  20.     Width = 40  
  21.     Height = 13  
  22.     Caption = #29992#25143#21517':'  
  23.   end  
  24.   object Label2: TLabel  
  25.     Left = 168  
  26.     Top = 203  
  27.     Width = 28  
  28.     Height = 13  
  29.     Caption = #23494#30721':'  
  30.   end  
  31.   object Label3: TLabel  
  32.     Left = 208  
  33.     Top = 288  
  34.     Width = 193  
  35.     Height = 41  
  36.     Caption = 'Label3'  
  37.   end  
  38.   object Edit1: TEdit  
  39.     Left = 224  
  40.     Top = 152  
  41.     Width = 121  
  42.     Height = 21  
  43.     ImeName = #20013#25991'('#31616#20307') - '#26497#28857#20116#31508  
  44.     TabOrder = 0  
  45.   end  
  46.   object Edit2: TEdit  
  47.     Left = 224  
  48.     Top = 200  
  49.     Width = 121  
  50.     Height = 21  
  51.     ImeName = #20013#25991'('#31616#20307') - '#26497#28857#20116#31508  
  52.     TabOrder = 1  
  53.   end  
  54.   object Button1: TButton  
  55.     Left = 270  
  56.     Top = 248  
  57.     Width = 75  
  58.     Height = 25  
  59.     Caption = #30331#38470  
  60.     TabOrder = 2  
  61.     OnClick = Button1Click  
  62.   end  
  63. end  


http://blog.csdn.net/huang_xw/article/details/8644506

原文地址:https://www.cnblogs.com/findumars/p/7257477.html