简体   繁体   中英

How can I handle Windows scaling from a WinForm with an embedded WPF UserControl (which was instantiated from an Excel VSTO add-in's ribbon)?

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.

在此处输入图像描述

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:

在此处输入图像描述

Actual behavior:

在此处输入图像描述

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]

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM