CommonOpenFileDialog cause Windows Form to shrink
Asked Answered
S

2

23

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;
Spelldown answered 23/3, 2017 at 11:53 Comment(8)
Consider adding the code you are using to open the dialog. Also that is a pretty funny bug.Benefaction
cant say ive seen that happen Does the same happen if you make a new app and open the same commonopenfiledialog?Swipple
@Swipple I didn't think to try that, thanks for the suggestion. Unfortunately I just created a blank project, and it's showing the same behaviour.Spelldown
Right, so I have just checked some settings on my laptop, and figured out the cause. My laptop screen was set under Display Settings as my Main Display, and the "Change the size of text, apps and other items" (DPI?) was set to 125%. My second monitor was set to 100%. I have just changed these settings to both 100% which fixed the issue. I also tried swapping the Main Display to the second monitor, and keeping the DPI (?) settings as they were previously (so 100% on the monitor, 125% on the laptop screen), and that has also resolved the issue.Spelldown
I'm having the same issue -- simply displaying the CommonOpenFileDialog shrinks the entire window to what it would appear as if my scale was 100%. Unfortunately, my monitor is a TV screen and reducing the scale from 150% to 100% makes many things unreadable. Have you found a solution that can be done in the code?Extra
Sorry for the delay @Extra I've been away- I'm afraid not, the scope of the tool I was building didn't justify looking further into it unfortunately. I would be interested if anyone has an update, good luck.Spelldown
Dang! Well, thanks for responding, anyway. This is going to be a fun fix, since it looks like the bug is somewhere inside the CodePack itself!Extra
I'm having the exact same problem :) I'm using a 4K monitor with 150% DPI scaling... When will high-DPI scaling ever work properly on any platform, god knows!Severson
C
18

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)

Chirography answered 8/8, 2019 at 11:10 Comment(1)
Second solution worked perfectly for me on my Inspiron 7591 2n1 which is kind of like a Surface Pro. I had to use Visual Studio 2013 for a customer, so I had to get the Ookii .nupkg using Visual Studio 2019 (because Ookii requires NuGet 3.3 which I could not get to work with VS 2013), then I referenced that folder/.dll in my VS2013 project.Rissa
S
10

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%).

Suberin answered 4/12, 2017 at 13:39 Comment(2)
Perfect! Didn't suspect it could be that easy. Thank you!Millais
Worked perfectly. ThanksFahrenheit

© 2022 - 2024 — McMap. All rights reserved.