Delphi 獲取外部程序句柄與發送消息

 --記錄下來備之後用 【打開外部程序、消息、句柄】,技術有限,但願不要誤人子弟了。sql

源碼
unit Unit1;spa

interfaceorm

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

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}string

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

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));
//把句柄保存下來備用io

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


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);form

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 endend

相關文章
相關標籤/搜索