Home > Net >  Insert values on Windows Dialog from Delphi Dialog
Insert values on Windows Dialog from Delphi Dialog

Time:01-28

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.

CodePudding user response:

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.

  •  Tags:  
  • Related