Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CommonOpenFileDialog cause Windows Form to shrink

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:

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;
like image 275
zmh Avatar asked Mar 23 '17 11:03

zmh


2 Answers

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)

like image 197
Eliahu Aaron Avatar answered Oct 13 '22 12:10

Eliahu Aaron


Enable dpi-aware by adding app.manifest file, and uncomment this blocks.

<application xmlns="urn:schemas-microsoft-com:asm.v3">
    <windowsSettings>
    <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
    </windowsSettings>
</application>

This works for me on my Surface Pro 4(dpi scale 200%).

like image 27
Kagamia Avatar answered Oct 13 '22 11:10

Kagamia