0

image

I want to know if there's a way to insert, for example, a FilePath in a DialogBox opened by Windows itself, but inserted from a Delphi variable to the Windows Dialog? Something like, click on an Upload button in any website that does open the File Explorer Dialog, and in Delphi send the value of the Path to the File Explorer of Website.

PS : I already have the code to get his HWND handle.

I don't know if this is possible, or is there's a way to do it.

Edit : A Select File from the site, what I want is simply to input the FilePath of this site by an app variable in Delphi.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • You want to _customize_ i.e. the "Open" or "Select folder" dialog? As in [Custom preview for open dialog](https://stackoverflow.com/q/2204706/4299358) and [File Open Dialog with Encodings combobox](https://stackoverflow.com/q/1076827/4299358)? That differs per dialog. Add a screenshot to your question of the dialog in question with a painted button/control of your choice to make things more clear. – AmigoJack Jan 26 '22 at 18:17
  • I want insert the PathFile of the Windows Dialog from Delphi application, but not an Dialog opened by Delphi App, only by whatever opens a Select Folder/File – Geovanii Amaral Jan 26 '22 at 18:22
  • My main purpose is related with WebScrapping using Web Driver. But I got to a part where I'm not able to pass the file to the site's upload input, it's asynchronous and out of scope, I don't have access permission, but as the site upload opens the select file, the only way I thought it was to get the file locally with an Open Dialog and send it to the Select File site, which is a Windows Dialog, a workaround but... – Geovanii Amaral Jan 26 '22 at 18:32
  • You are looking for UIAutomation – David Heffernan Jan 27 '22 at 05:57

1 Answers1

-2

Variants

If you know that dialog window's handle already then there are at least 2 variants:

  1. finding the control by going through the children hierarchy, using FindWindowEx()
  2. relying on the dialog template's IDs, using GetDlgItem()

Both may work out great, but are at the same time fragile, since the dialog window's structure (as per its controls and IDs) can change even by Service Packs already, let alone entire Windows versions. My experience here is from Win2000 up to Win7 and on those systems it worked.

Dialog versions

What you posted as screenshot is the "Vista" version of the dialog; the older "Win95" version is identical in terms of accessing the "filename" ComboBox.

The old one is typical for having the buttons ("open" and "cancel") in one row. Older software might still use these, but your web browser most likely not: "Open" dialog window, Win95 version

The one since Vista has its buttons in one line and is notable of featuring a full folder pane: "Open" dialog window, Vista version

But both pictures show that the control layout is the same: a ComboBoxEx32 is a direct child of the window, having its own child(s). So we can use the same code for both versions.

Code

var
  hDialog, hCbx32, hCbx, hEdit, hFilename: HWND;
  sText: String;
begin
  hDialog:= 9568854;  // Dialog window, class "32770"
  sText:= 'M:\my\filename.ext';  // What should be set

  // Variant #1: finding the control by parents and childs.
  // Luckily both the old dialog up to XP and the new dialog
  // since Vista do not differ as per the filename ComboBox.
  hCbx32:= FindWindowEx( hDialog, 0, 'ComboBoxEx32', nil );  // Most likely the 3rd child control
  hCbx:=   FindWindowEx( hCbx32,  0, 'ComboBox',     nil );  // Actual ComboBox inside that
  hEdit:=  FindWindowEx( hCbx,    0, 'Edit',         nil );  // Edit control inside ComboBox
  SendMessage( hEdit, WM_SETTEXT, 0, LPARAM(PChar(sText)) );

  // Variant #2: using dialog template IDs, which haven't
  // changed since XP with one of its Service Packs. However,
  // tested with Win7 only.
  hFilename:= GetDlgItem( hDialog, $47C );  // "cmb13", found at least in XP SP3
  if hFilename= 0 then hFilename:= GetDlgItem( hDialog, $480 );  // "edt1" = Maybe prior to XP without any SP
  SendMessage( hFilename, WM_SETTEXT, 0, LPARAM(PChar(sText)) );
end;

One of both variants should do already. Successfully tested on Win7x64

  • using Paint's "Open" command for the Vista dialog version, and
  • one of my older program's "Open" command for the Win95 dialog version:

the text in the filename's ComboBox was set as expected.

AmigoJack
  • 5,234
  • 1
  • 15
  • 31