简体   繁体   English

如何在“打开”对话框WinAPI C ++中设置文件的路径

[英]How to set path of file in Open dialog WinAPI C++

I wonder how can I set a file path in the Open Dialog, that was started by another process. 我想知道如何在“打开对话框”中设置由另一个进程启动的文件路径。 I mean this dialog 我的意思是这个对话

在此处输入图片说明

I need to set path to file. 我需要设置文件路径。

What is the best way to do this ? 做这个的最好方式是什么 ?

Of course I can always send keys using SendInput but is there any more elegant way to do this ? 当然,我总是可以使用SendInput发送键,但是还有其他更优雅的方法吗? For example to send WM_COMMAND or something else ? 例如发送WM_COMMAND或其他?

Thanks 谢谢

What you are asking can only be solved with hacks. 您的要求只能通过黑客解决。

The best approach is to use MSAA / UI Automation as much as possible. 最好的方法是尽可能使用MSAA / UI自动化

Assuming you know the HWND of the Open dialog you can walk all the child items looking for something with the role of ROLE_SYSTEM_COMBOBOX. 假设您知道“打开”对话框的HWND,则可以遍历所有子项以查找具有ROLE_SYSTEM_COMBOBOX角色的项。 Inside this item you need to find something with the role of ROLE_SYSTEM_TEXT that is editable (enabled and visible). 在此项内部,您需要找到具有ROLE_SYSTEM_TEXT角色的可编辑(启用和可见)的对象。 Just looking for a ROLE_SYSTEM_TEXT item might find the search box in the top right corner. 仅查找ROLE_SYSTEM_TEXT项可能会在右上角找到搜索框。

If you can't find this object then it might be the older style dialog, restart the search and only look for ROLE_SYSTEM_TEXT. 如果找不到该对象,则可能是较旧的样式对话框,请重新开始搜索,仅查找ROLE_SYSTEM_TEXT。

It might be a good idea to verify this detection by comparing the HWNDs id (I will discuss these later). 通过比较HWND ID来验证此检测可能是一个好主意(我将在后面讨论)。

When you find the item you can change the text with put_accValue() . 找到项目后,可以使用put_accValue()更改文本。

There is also a CDM_SETCONTROLTEXT message but you need to be inside the process to use it. 也有一个CDM_SETCONTROLTEXT消息,但是您需要在进程内部使用它。


If you don't know the HWND of the dialog or want to do this to all open dialogs on the system then you need a hook so you know when there is a new HWND candidate. 如果您不知道对话框的HWND或想对系统上所有打开的对话框执行此操作,则需要一个钩子,以便知道何时有新的HWND候选对象。 Detecting these can be tricky. 检测这些可能很棘手。

The only option I know is to look for certain controls with certain ids. 我知道的唯一选择是寻找具有特定ID的特定控件。 In particular there will be a ListBox with the id of lst1 and either a Edit box or a Combo box with a specific id. 特别是,将有一个ID为lst1的ListBox和一个具有特定ID的Edit框或Combo框。

You need to do a lot of testing to get this right. 您需要进行大量测试才能实现此目的。 Remember that on Vista and later there are two dialog implementations ( IFileOpenDialog and GetOpenFileName ) and you need to test both. 请记住,在Vista和更高版本上,有两个对话框实现( IFileOpenDialogGetOpenFileName ),您需要对两者进行测试。 GetOpenFileName even has a old and a new mode (compatibility mode when old applications use templates and/or hooks). GetOpenFileName甚至具有新旧模式(旧应用程序使用模板和/或挂钩时的兼容模式)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM