问题描述
我一直在开发一个 Windows 窗体应用程序,最近添加了一个简单的设置页面,允许用户选择一个文件夹来存放输出.OpenFileDialog
丑陋且不好用,所以我在 WindowsAPICodePack 中添加了对 CommonOpenFileDialog
的访问 - 一切都很好.
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.
当我打开 CommonOpenFileDialog
时,Windows 窗体应用程序会缩小到更小的尺寸,如附图所示.
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:
我尝试过检查 Form
前后的大小,但没有改变,所以我遇到了一些问题.任何信息都会很有用,如果需要,我可以提供更多详细信息.
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.
打开对话框的代码是:
CommonOpenFileDialog dialog = new CommonOpenFileDialog();
dialog.DefaultDirectory = selectedFolderTextBox.Text;
dialog.IsFolderPicker = true;
if (dialog.ShowDialog() != CommonFileDialogResult.Ok) return;
selectedFolderTextBox.Text = dialog.FileName;
推荐答案
我在windows Settings->System窗体中更改Scale and layout时出现这个问题100% 到更高的值.这可能与高 DPI 和 DPI 缩放有关.
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:
此解决方案仅适用于 .NET Framework 4.7 或更高版本.
将此添加到 App.config 文件中.
Add this to to App.config file.
<System.Windows.Forms.ApplicationConfigurationSection>
<add key="DpiAwareness" value="PerMonitorV2" />
</System.Windows.Forms.ApplicationConfigurationSection>
来源:配置您的高 DPI 支持的 Windows 窗体应用程序:
在 app.config 文件中启用每个显示器的 DPI 感知.
Enable per-monitor DPI awareness in the app.config file.
Windows 窗体介绍了一个新的System.Windows.Forms.ApplicationConfigurationSection 元素到支持从 .NET 开始添加的新功能和自定义框架 4.7.利用支持高的新功能DPI,将以下内容添加到您的应用程序配置文件中.
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>
重要
在以前版本的 .NET Framework 中,您使用清单来添加高 DPI 支持.不再推荐这种方法,因为它覆盖 app.config 文件中定义的设置.
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.
解决方案 2:使用 Ookii.Dialogs.WinForms NuGet 包
使用 Ookii.Dialogs.WinForms NuGet 包.它不存在缩水问题.它有一个类似于 WindowsAPICodePack 的 CommonFileDialog
的 VistaOpenFileDialog
.它还有一个不错的文件夹浏览器 VistaFolderBrowserDialog
,就像 CommonFileDialog
一样,IsFolderPicker
设置为 true
.
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
.
此解决方案需要单独手动更改每个应用程序 .exe 文件的兼容性设置,因此不是最佳解决方案.
This solution requires to manually change the compatibility settings for each application .exe file individually, so it not the best solution.
为此,您需要右键单击 .exe 文件,选择 属性->兼容性->更改高 DPI 设置并选中 覆盖高 DPI 缩放行为并选择其中一个选项(请参阅:如何在 Windows 10 中使用 DPI 缩放来修复模糊的旧应用)
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 窗体缩小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!