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.

相关阅读 >>

Delphi显示圆形头像circle image

Delphi 的内存操作函数(5): 复制内存

Delphi 如何将memo或richedit保存为utf 8文本文件?

Delphi 将listview保存为txt

Delphi多媒体函数

Delphi中一段可以判断网络连接的代码

Delphi 把修改好的cookies重新赋值给webbrowser1 doc

Delphi tfilestream和tmemorystream分别读取、创建、合并文件

Delphi sysutils.strcat

Delphi 泛型搜索字符串数组

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...

    正在狠努力加载,请稍候...