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.

窗體文件
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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM