
我想知道是否有一種方法可以在 Windows 本身打開的 DialogBox 中插入 FilePath,但從 Delphi 變數插入到 Windows Dialog?例如,在打開檔案資源管理器對話框的任何網站中單擊上傳按鈕,然后在 Delphi 中將路徑的值發送到網站的檔案資源管理器。
PS:我已經有了獲取他HWND句柄的代碼。
我不知道這是否可能,或者有沒有辦法做到這一點。
編輯:從站點中選擇檔案,我想要的只是通過 Delphi 中的應用程式變數輸入該站點的 FilePath。
uj5u.com熱心網友回復:
變體
如果您已經知道該對話框視窗的句柄,那么至少有 2 個變體:
- 通過遍歷子層次結構找到控制元件,使用
FindWindowEx() - 依賴對話框模板的 ID,使用
GetDlgItem()
兩者都可以很好地作業,但同時又很脆弱,因為對話視窗的結構(根據其控制元件和 ID)甚至可以通過 Service Pack 進行更改,更不用說整個Windows版本了。我在這里的經驗是從Win2000到Win7以及它在那些系統上作業。
對話框版本
您發布的螢屏截圖是對話框的“ Vista ”版本;舊的“ Win95 ”版本在訪問“檔案名”組合框方面是相同的。
舊的按鈕(“打開”和“取消”)在一行中是典型的。較舊的軟體可能仍會使用這些,但您的網路瀏覽器很可能不會:

自Vista以來,它的按鈕集中在一行中,并且值得注意的是具有完整的檔案夾窗格:

但是兩張圖片都顯示控制元件布局是相同的:aComboBoxEx32是視窗的直接子級,有自己的子級。所以我們可以對兩個版本使用相同的代碼。
代碼
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;
兩種變體之一應該已經可以了。在Win7x64上測驗成功
- 在Vista對話框版本中使用Paint的“打開”命令,以及
- one of my older program's "Open" command for the Win95 dialog version:
the text in the filename's ComboBox was set as expected.
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/422344.html
標籤:
