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 圆角panel

Delphi 隐藏任务栏程序,取消自动对齐和网格对齐

Delphi dll注入x86/x64/win2k~win8.1全可用

Delphi xe8中的正则表达式提取日语(平假名,片假名,汉字)

Delphi xe6 switch元件�繁�w���}

Delphi 提升进程权限到sedebugprivilege

Delphi: ttreeview 中禁止双击事件展开或关闭节点

Delphi 判断端口是否被使用

Delphi 创建一个大小不可改变的窗体,通用模板

Delphi android实例-读取设备联系人(xe8+小米2)

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



打赏

取消

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

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

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

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

评论

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