1

Context

We have a problem with the Windows scaling feature (100%, 125%, 150% ...) and our WinForm. We instantiate our WinForm from an Excel VSTO Add-in, and each WinForm contains a single component which is a UserControl (WFP) hosted with an ElementHost in the WinForm.

Each UserControl embeds all the necessary components, with an MVVM approach.

The problem occurs only when the user has two screens and the main screen has a zoom scale of 100% and the secondary screen is on 125% or more.

enter image description here

Excel Ribbon.cs

private void button1_Click(object sender, RibbonControlEventArgs e)
{
    var form = new MainForm();
    form.Show();
}

MainForm.cs

public MainForm()
{
    this.InitializeComponent();
    this.MainUserControl = new MainUserControl();
    this.Controls.Add(new ElementHost 
    { 
        Dock = DockStyle.Fill, 
        Child = this.MainUserControl 
    });
}

Expected behavior:

enter image description here

Actual behavior:

enter image description here

The problem only occurs on the main screen (this screen was set to 100% scale) but not on the second screen.

Other information

Another interesting point is that when we move the WinForm from the main screen to the second screen, it displays correctly; and then we move it from the second screen to the main and that's functional.

At this point, if we try to resize the WinForm then it breaks again.

What I've allready try:

First of all we tried to add a app.manifest and uncomment the DPI Aware block

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

We also uncomment the compatibility support for Windows 10 (all our collaborators are under W10, no need to support more)

<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />

We trying to added the two following methods on the Addin Startup event, but without any results

System.Windows.Forms.Application.EnableVisualStyles();
System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false);

Also we've got the following line in the AssemblyInfo.cs

[assembly: System.Windows.Media.DisableDpiAwareness]
Kotus
  • 11
  • 3
  • Read the notes here: [DPI Awareness - Unaware in one Release, System Aware in the Other](https://stackoverflow.com/a/50276714/7444103) and here: [How to configure an app to run correctly on a machine with a high DPI setting](https://stackoverflow.com/a/13228495/7444103). – Jimi Apr 17 '19 at 16:54
  • Thanks for you answer @Jimi, we tried many of what is said in the different notes, but nothing helps us to fix the problem.I edited my post to notify what we tried. – Kotus Apr 19 '19 at 07:26
  • If you're *embedding* a component that is not DPIAware, that object is subject to virtualization (the System will size it for you (or paint it, if you will), since the object is not *Aware* (Aware means *Dont bother, I'll do it myself*). All components need to follow the same Awareness status and type (SystemAware, PerMonitor, PerMonitorV2 etc.). You cannot enable DPIAwareness in the manifest and add `System.Windows.Media.DisableDpiAwareness` at the same time, if that's what you did. Make everything DPIAware. You'll have to re-layout a couple of things, but that's all. – Jimi Apr 19 '19 at 09:32

2 Answers2

1

I had an issue look similar to this! The only thing I did was set the AutoScaleMode in the Properties of the WinForm to Font instead of Inherit and it fixed my problem without adding DPI Awareness!

Huy
  • 11
  • 1
0

Instead of using windows form, why not use WPF directly. It handle scaling issue by itself.

YUVRAJ
  • 109
  • 3