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]