I got the following code from here and have changed it a little and I have changed the original question a little also.
The timer interval is set to 5000.
After the following 3 events occur the 'Events OnTimer' procedure will start to work.
- 1.WebBrowser1.Navigate('Any webpage');
- 2.wait for it to load
- 3.programmatically press a download file button
The problem now is I can't find the 'Edit'(Class Name) handle that belongs to(or is the child of) the 'Save as' dialog box. The handle for the 'Edit' comes to '0' in the code below, but if I use my mouse pointer and the following code:
HND:= WindowFromPoint(PNT);
Label1.Caption:= IntToStr(HND);
the handle gives a result. Once I have the handle, I can use:
SetWindowText(EditHandle, 'test text');
to change the text in 'Edit'(Class Name).
procedure TForm1.Timer1Timer(Sender: TObject);
Var
WHandle : HWND ;
ParentHandle : DWORD ;
P : Array[0..256] Of Char ;
ProcessIdActif : DWORD ;
begin
ProcessIdActif := 0 ;
GetWindowThreadProcessId (handle,@ProcessIdActif);
WHandle := FindWindow( Nil, Nil);
While (WHandle <> 0) Do
begin
P[0] := #0;
GetWindowText(WHandle, P, 255);
if P[0] <> #0 then
begin
GetWindowThreadProcessId (WHandle,@ParentHandle);
if ProcessIdActif = ParentHandle then
begin
if CompareText(p,'File Download') = 0 then
begin
ButtonHandle := FindWindowEx(WHandle, 0, 'Button', '&Save');
if (ButtonHandle > 0) then
PostMessage(ButtonHandle, BM_CLICK, 0, 0);
end
else if CompareText(p,'Save As') = 0 then
begin
EditHandle := FindWindowEx(WHandle, 0, 'Edit',NIL);
if (EditHandle > 0) then
SetWindowText(EditHandle, 'test text');
end;
end;
end;
WHandle := GetWindow(WHandle, GW_HWNDNEXT);
end;
end;
I've been trying to understand everything here but I'm missing something.
I'm able to press any windows dialog button by moving the mouse and pressing the mouse programatically, but I'd like to figure out how to press these buttons in a cleaner way.