--记录下来备以后用 【打开外部程序、消息、句柄】,技术有限,希望不要误人子弟了。
源码
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.
窗体文件
object Form1: TForm1
Left = 192
Top = 130
Width = 313
Height = 497
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
OnDestroy = FormDestroy
PixelsPerInch = 96
TextHeight = 13
object btn1: TButton
Left = 211
Top = 419
Width = 75
Height = 25
Caption = '打开pl/sql'
TabOrder = 0
OnClick = btn1Click
end
object mmo1: TMemo
Left = 0
Top = 0
Width = 209
Height = 449
Lines.Strings = (
'mmo1')
TabOrder = 1
end
end