设置 OpenFileDialog/SaveFileDialog 的起始位置
对于 WinForm 应用程序中的任何自定义对话框(表单),我可以在显示它之前设置其大小和位置:
form.StartPosition = FormStartPosition.Manual;
form.DesktopBounds = MyWindowPosition;
这在处理多个监视器时尤其重要。 如果没有此类代码,当您从已拖动到第二个显示器的应用程序中打开对话框时,该对话框将显示在主显示器上。 这呈现出较差的用户体验。
我想知道是否有任何钩子可以设置标准 .NET OpenFileDialog 和 SaveFileDialog(没有 StartPosition 属性)的位置。
For any custom dialog (form) in a WinForm application I can set its size and position before I display it with:
form.StartPosition = FormStartPosition.Manual;
form.DesktopBounds = MyWindowPosition;
This is particularly important when dealing with multiple monitors. Without such code, when you open a dialog from an application that you have dragged to a second monitor, the dialog appears on the primary monitor. This presents a poor user experience.
I am wondering if there are any hooks to set the position for the standard .NET OpenFileDialog and SaveFileDialog (which do not have a StartPosition property).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我怀疑您能做的最好的事情就是确保使用 重载
ShowDialog
接受IWin32Window
用作父级。 这可能帮助它选择合适的位置; 最常见的:I suspect that the best you can do is make sure you use the overload of
ShowDialog
that accepts anIWin32Window
to use as the parent. This might help it choose an appropriate location; most commonly:请查看 CodeProject 上的这篇文章。 摘抄:
Check out this article on CodeProject. Excerpt:
OpenFileDialog 和 SaveFileDialog 将自己定位在左上角
最近显示的窗口的客户区。 因此,只需在创建并显示该对话框之前创建一个新的不可见窗口,该窗口位于您希望对话框出现的位置。
OpenFileDialog and SaveFileDialog position themselves in the upper-left corner of
the client area of the most recently displayed window. So just create a new invisible window positioned where you want the the dialog to appear before creating and showing that dialog.
我昨天大部分时间都遇到这个问题。 BobB 的回答对我帮助最大(谢谢 BobB)。
您甚至可以创建一个私有方法,在
dialog.ShowDialog()
方法调用之前创建一个窗口并关闭它,并且它仍然会将OpenFileDialog
居中。然后在
ShowDialog()
方法之前的任何方法中调用它。I had this problem for most of yesterday. BobB's answer was the one that helped me out the most (Thanks BobB).
You can even go as far as to make a private method that creates a window and closes it before the
dialog.ShowDialog()
method call and it will still centre theOpenFileDialog
.Then call it in any method before the
ShowDialog()
method.我是这样做的:
我想要显示 OpenFileDialog 的点:
重新定位代码:
我启动一个线程来查找标题为“Open”的窗口。 (通常在 3 次迭代或 15 毫秒内发现。)然后我使用获得的句柄设置其位置。 (有关位置/大小参数,请参阅 SetWindowPos 文档。)
Kludgy。
Here's how I did it:
The point where I want to display the OpenFileDialog:
The repositioning code:
I start a thread that looks for a window with the "Open" title. (Typically found in 3 iterations or 15 milliseconds.) Then I set its position with the obtained handle. (See SetWindowPos documentation for the position/size parameters.)
Kludgy.
MSDN 上有一个相当古老的方法示例。
http://msdn.microsoft.com/en-us/library/ms996463。 aspx
它包含实现您自己的允许扩展性的 OpenFileDialog 类所需的所有代码。
There is quite an old example of one approach on MSDN.
http://msdn.microsoft.com/en-us/library/ms996463.aspx
It includes all the code needed to implement your own OpenFileDialog class that allows extensibility.
非常感谢BobB对此的回复。 还有一些“陷阱”。 调用 OpenFileDialog1.ShowDialog(PositionForm) 时必须传递 PositionForm 的句柄,否则 BobB 的技术并非在所有情况下都可靠。 另外,现在 W8.1 启动了一个新的文件打开控件,其中包含 SkyDrive,W8.1 文件打开控件中的文档文件夹位置现在已被固定。 因此,我通过设置 ShowHelp = True 来使用旧的 W7 控件来打开 fileopen。
这是我最终使用的 VB.NET 代码,这是我对社区的贡献,以防有帮助。
Very grateful for BobB's reply on this one. There are a few more "gotchas". You have to pass the handle of PositionForm when calling OpenFileDialog1.ShowDialog(PositionForm) otherwise BobB's technique is not reliable in all cases. Also, now that W8.1 launches a new fileopen control with SkyDrive in it, the Documents folder location in the W8.1 fileopen control is now screwed. So I frig fileopen to use the old W7 control by setting ShowHelp = True.
Here is the VB.NET code I ended up using, my contribution to the community in case it helps.
以 Rob Sherrit 在 2014 年 1 月 22 日的回复为灵感,我创建了一个新模块,并将其命名为 CKRFileDialog(随你所想),其中包含以下代码:
然后,我在各个模块中调用此代码,如下所示:
如果执行“ FileOpen”确保有一个 FileOpenDialog 组件添加到您的表单或代码中,并根据需要调整该组件的属性
(例如 InitDirectory、Multiselect 等)
使用 FileSaveDialog 组件时执行相同的操作(可能适用于 FileOpenDialog 组件的不同属性)。
要“显示”对话框组件,请使用一行代码,如下所示,传递两个参数,第一个参数是您正在使用的文件对话框(“打开”或“保存”),第二个参数是您希望在其上覆盖对话框的表单。
CKRFileDialog.Show(saveFileDialog1, CoveredForm)
或者
CKRFileDialog.Show(openFileDialog1, CoveredForm)
请记住,如果您使用 SkyDrive,则必须传递“True”作为第三个参数:
CKRFileDialog.Show(saveFileDialog1, CoveredForm, True)
或者
CKRFileDialog.Show(openFileDialog1, CoveredForm, True)
我将对话框的“偏移”设置为表单上横向和纵向的 1/8
“CoveredForm”,但您可以将其设置回 1/2(如 Rob Sherret 的代码中所示)或您希望的任何值。
这似乎是最简单的方法
谢谢罗布! :-)
Using Rob Sherrit's response on Jan 22 '14 as inspiration, I created a new module and called it CKRFileDialog (call it what you want) which contains the following code:
I then call this code in my various modules as follows:
If performing a "FileOpen" ensure that there is a FileOpenDialog component added to your form or code and adjust the properties of the component if you wish
(e.g. InitDirectory,Multiselect,etc.)
Do the same when using FileSaveDialog components (Different properties to the FileOpenDialog component may apply).
To "show" the dialog component use a line of code as follows, passing two parameters, the first the FileDialog you are using ("Open" or "Save") and the second parameter the Form upon which you wish to overlay the dialogue.
CKRFileDialog.Show(saveFileDialog1, CoveredForm)
or
CKRFileDialog.Show(openFileDialog1, CoveredForm)
Remember, if you are using SkyDrive you must pass "True" as a third parameter:
CKRFileDialog.Show(saveFileDialog1, CoveredForm, True)
or
CKRFileDialog.Show(openFileDialog1, CoveredForm, True)
I set the "offset" of the dialogue to be 1/8 the way across and down on the form
"CoveredForm", but you can set that back to 1/2 (as in Rob Sherret's code) or whatever value you wish.
This seemed the easiest approach
Thanks Rob! :-)