delphi 获取外部程序句柄与发送消息  
官方Delphi 学习QQ群: 682628230(三千人)
频道

delphi 获取外部程序句柄与发送消息


源码
unit Unit1;

interface

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

type
TForm1 = class(TForm)
btn1: TButton;
mmo1: TMemo;
procedure btn1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
TSL: TStringList;
end; 
var
Form1: TForm1; 
implementation

{$R *.dfm}

function EnumChildWndProc(AhWnd: LongInt;
AlParam: lParam): boolean; stdcall;
var
WndClassName: array[0..254] of Char;
WndCaption: array[0..254] of Char;
cRect: TRect;
begin

GetClassName(AhWnd, wndClassName, 254); //获取类名
GetWindowText(aHwnd, WndCaption, 254); //获取控件caption
GetWindowRect(aHwnd, cRect); //获取控件的Rect
with form1.mmo1 do
begin
if (string(wndClassName) = 'TEdit') or (string(wndCaption) = '确定')
or (string(wndClassName) = 'TComboBox') then
begin
Form1.TSL.Add(IntToStr(cRect.Top) + '=' + IntToStr(AhWnd));
//把句柄保存下来备用

lines.add(string(wndClassName));
lines.add(string(wndCaption));
Lines.Add(IntToStr(cRect.Top) + ' ' + IntToStr(AhWnd) + ' ' + IntToStr(aHwnd));
lines.add('-------');
end;
end;
result := true;
end;


procedure TForm1.btn1Click(Sender: TObject);
var
FrmHandle: THandle;
name: string;
begin
ShellExecute(self.Handle, 'open', 'F:\zzx\PLSQL\plsqldev.exe', nil, nil, SW_HIDE);
//打开外部应用程序 使用 ShellExecute需引用 ShellAPI
Sleep(2000); //休眠2秒 为了能获取到窗体句柄
FrmHandle := FindWindow(nil, PChar('Oracle 登录')); //获取pl/sql登录窗体的句柄
if FrmHandle <> 0 then
begin
EnumChildWindows(FrmHandle, @EnumChildWndProc, 0); //遍历登录窗体里面的子控件 获取其句柄
end
else MessageBox(self.handle, '没找到该窗口句柄', '提示', 0);

name := 'name';
SendMessage(StrToInt(TSL.ValueFromIndex[0]), WM_SETTEXT, 0, LPARAM(name)); //name
SendMessage(StrToInt(TSL.ValueFromIndex[1]), WM_SETTEXT, 0, LPARAM(name)); //PW
SendMessage(StrToInt(TSL.ValueFromIndex[2]), WM_SETTEXT, 0, LPARAM(name)); //server
//上面是向各个子控件 (Edit) 发送设置文本消息 特殊原因统一写成了name 可以自行编写其他的

// SendMessage(StrToInt(TSL.ValueFromIndex[3]), WM_LBUTTONDOWN, 0, 0); //鼠标按下
// SendMessage(StrToInt(TSL.ValueFromIndex[3]), WM_LBUTTONUP, 0, 0); // 鼠标抬起
// SendMessage(StrToInt(TSL.ValueFromIndex[3]), WM_KEYDOWN, VK_DOWN, 0); // 发送向下键
// SendMessage(StrToInt(TSL.ValueFromIndex[3]), WM_LBUTTONDOWN, 0, 0); //鼠标按下
// SendMessage(StrToInt(TSL.ValueFromIndex[3]), WM_LBUTTONUP, 0, 0); // 鼠标抬起
//这里是选择ComboBox的第二项 sysdba用

SendMessage(StrToInt(TSL.ValueFromIndex[4]), BM_CLICK, 0, 0);
//点击确定按钮 登录
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
TSL := TStringList.Create;
TSL.Sorted := True;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
FreeAndNil(TSL);
end;

end.

推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.060604095458984 seconds