CommonOpenFileDialog导致Windows窗体缩小 [英] CommonOpenFileDialog cause Windows Form to shrink
问题描述
我一直在使用Windows Forms应用程序,并且最近添加了一个简单的设置页面,该页面允许用户选择输出的文件夹。 OpenFileDialog
丑陋且不好用,因此我在 WindowsAPICodePack 中添加了对 CommonOpenFileDialog的访问权限
-一切都很好。
当我打开Windows窗体应用程序 CommonOpenFileDialog
时缩小为较小的尺寸,如图所示。
左侧通常是程序,右侧是打开对话框的地方:
I之前和之后都尝试过检查 Form
的大小,这没有变化,所以我碰到了一些砖墙。任何信息都是有用的,如有需要,我可以提供更多详细信息。
打开对话框的代码为:
CommonOpenFileDialog对话框= new CommonOpenFileDialog();
dialog.DefaultDirectory = selectedFolderTextBox.Text;
dialog.IsFolderPicker = true;
if(dialog.ShowDialog()!= CommonFileDialogResult.Ok)返回;
selectedFolderTextBox.Text = dialog.FileName;
当我更改设置->系统 中将缩放和布局 的值提高100%。
我发现了几种解决方案:
解决方案1:为高DPI支持配置Windows窗体
此解决方案仅适用于NET Framework 4.7或更高版本。
将此添加到 App.config 文件。
<系统.Windows.Forms.ApplicationConfigurationSection>
< add key = DpiAwareness value = PerMonitorV2 />
< /System.Windows.Forms.ApplicationConfigurationSection>
在app.config文件中启用每个监视器的DPI感知。
Windows窗体
引入了新的
System.Windows.Forms.ApplicationConfigurationSection 到
的元素支持从.NET
Framework 4.7开始添加的新功能和自定义项。要利用支持
DPI高的新功能,请将以下内容添加到应用程序配置文件中。< System.Windows.Forms.ApplicationConfigurationSection>
< add key = DpiAwareness value = PerMonitorV2 />
< /System.Windows.Forms.ApplicationConfigurationSection>
重要
<在早期版本的.NET Framework中,您使用清单为
添加了高DPI支持。不再推荐这种方法,因为它
会覆盖app.config文件中定义的设置。
解决方案2:使用Ookii.Dialogs.WinForms NuGet程序包
使用 Ookii.Dialogs.WinForms NuGet程序包。它没有缩小的问题。它具有 VistaOpenFileDialog
类似于WindowsAPICodePack的 CommonFileDialog
。它还具有一个不错的文件夹浏览器 VistaFolderBrowserDialog
,如 CommonFileDialog
和 IsFolderPicker
设置为 true
。
解决方案3:覆盖.exe文件的高DPI缩放行为
此解决方案要求分别手动更改每个应用程序.exe文件的兼容性设置,因此,这不是最佳解决方案。
为此,您需要右键单击.exe文件,选择属性->兼容性->更改DPI高设置,然后选中覆盖DPI高缩放行为,然后选择选项之一(请参阅:如何在Windows 10中使用DPI缩放来修复模糊的旧应用程序)
I've been working on a Windows Forms application, and have recently added a simple settings page that allows the user to select a folder for where the output goes. The OpenFileDialog
is ugly and not nice to use, so I've added in the WindowsAPICodePack to get access to the CommonOpenFileDialog
- all good there.
When I open the CommonOpenFileDialog
, the Windows form application shrinks to a smaller size, as shown in the image attached.
On the left is the program normally, on the right is with the dialog open:
I've tried checking the size of the Form
before and after, that's not changing, so I'm hitting a bit of a brick wall. Any information would be useful, I can provide more details if needed.
Code to open the dialog is:
CommonOpenFileDialog dialog = new CommonOpenFileDialog();
dialog.DefaultDirectory = selectedFolderTextBox.Text;
dialog.IsFolderPicker = true;
if (dialog.ShowDialog() != CommonFileDialogResult.Ok) return;
selectedFolderTextBox.Text = dialog.FileName;
This problem happens to me when I change the Scale and layout in windows Settings->System form 100% to a higher value. It probably has to do with high DPI and DPI scaling.
I found several solution:
Solution 1: Configuring Windows Forms for high DPI support
This solution is only for .NET Framework version 4.7 or higher.
Add this to to App.config file.
<System.Windows.Forms.ApplicationConfigurationSection>
<add key="DpiAwareness" value="PerMonitorV2" />
</System.Windows.Forms.ApplicationConfigurationSection>
Source: Configuring your Windows Forms app for high DPI support:
Enable per-monitor DPI awareness in the app.config file.
Windows Forms introduces a new System.Windows.Forms.ApplicationConfigurationSection element to support new features and customizations added starting with the .NET Framework 4.7. To take advantage of the new features that support high DPI, add the following to your application configuration file.
<System.Windows.Forms.ApplicationConfigurationSection> <add key="DpiAwareness" value="PerMonitorV2" /> </System.Windows.Forms.ApplicationConfigurationSection>
Important
In previous versions of the .NET Framework, you used the manifest to add high DPI support. This approach is no longer recommended, since it overrides settings defined on the app.config file.
Solution 2: Use Ookii.Dialogs.WinForms NuGet package
Use the Ookii.Dialogs.WinForms NuGet package. It doesn't have the shrinking problem. It has a VistaOpenFileDialog
similar to the CommonFileDialog
of WindowsAPICodePack. It also has a nice folder browser VistaFolderBrowserDialog
like the CommonFileDialog
with IsFolderPicker
set to true
.
Solution 3: Override high DPI scaling behavior for the .exe file
This solution requires to manually change the compatibility settings for each application .exe file individually, so it not the best solution.
To do this you need to right-click on the .exe file, select Properties->Compatibility->Change high DPI settings and check Override high DPI scaling behavior and select one of the options (see: How to use DPI scaling in Windows 10 to fix blurry old apps)
这篇关于CommonOpenFileDialog导致Windows窗体缩小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!