unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ShellApi;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
FUNCTION EnumChildProc(Wnd: hWnd; SL: TStrings): BOOL; stdcall;
VAR
szFull: array[0..MAX_PATH] of Char; //Buffer for window caption
BEGIN
Result := Wnd <> 0;
if Result then
BEGIN
GetWindowText(Wnd, szFull, SizeOf(szFull)); // put window text in buffer
if (Pos(SL[0], StrPas(szFull)) > 0) and // Test for text
(SL.IndexOfObject(TObject(Wnd)) < 0) then // Test for duplicate handles
SL.AddObject(StrPas(szFull), TObject(Wnd)); // Add item to list
EnumChildWindows(Wnd, @EnumChildProc, Longint(SL)); //Recurse into child windows
END;
END;
function ClickButton(ParentWindow: Hwnd; ButtonCaption: string): Boolean;
var
SL: TStringList;
H: hWnd;
begin
SL := TStringList.Create;
try
SL.AddObject(ButtonCaption, nil); // First item in list is text to find
EnumChildWindows(ParentWindow, @EnumChildProc, Longint(SL));
H := 0;
case SL.Count of
1: ShowMessage('Window text not found.');
2: H := hWnd(SL.Objects[1]);
else
ShowMessage('Ambiguous text detected.');
end;
finally
SL.Free;
end;
Result := H <> 0;
if Result then
PostMessage(H, BM_CLICK, 0, 0);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
hWindow : DWORD;
hButton : DWORD;
begin
// Programm starten
ShellExecute(0,'open','d:\Test\Test.exe',nil,nil,SW_NORMAL);
// und auf das
Handle des Fensters warten
hWindow := 0;
Sleep(1000);
while hWindow = 0 do hWindow := FindWindow(nil,'TESTFORM');
// Die Child Windows durchgehen
hButton := 0;
IF hWindow <> 0 THEN
ClickButton(hWindow,'TESTBUTTON');
END;
end.