Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WinForms app changes its scaling after opening WPF window from form

At first I would like to give some context of problem: We have large legacy WinForms application, which we decided to move to WPF. But its impossible (for many reasons, including business) to completely rewrite an app from scratch, and we will do this migration step-by-step (one form after another). So for now we need to use WPF windows and controls in WinForms app.

Today I've made a first step in migration process: I recreated some simple form on WPF (it became a Window). The new window, which I've just recreated on WPF, is opened by pressing button on other form (which is standard WindowsForms form). Important note: my Windows is set up for 120 DPI, not default 96. All things were fine till I started the app to see what new window looks like.

So, here's what I've seen: App started normally, and looked as always. I opened "parent" form and pressed a button to open new WPF window. After pressing button WPF window opened as expected, but all visible application elements (main window, "parent" form, buttons, toolbars, etc.), including new WPF window, increased their sizes, they started to look bigger. It's not all. When I closed WPF window, nothing happened with scale - all app elements remained big. Then I closed "parent" form, from which I opened WPF window. And here was the magic - all app elements resized back to their original sizes. Then I opened "parent" form and WPF window again - but scale not changed, all app elements keeped their sizes. I repeated "open-close" of "parent" form and WPF window many times (during same application run, I didn't restart app) - no resize of elements.

After this I've made some investigations, and came to this: If I put any code, that uses any type from WPF assemblies, in Program.cs file before main application initializes, then app will always draw all its elemets bigger than they were before, but when opening WPF window there will be no scale change. Example of Program.cs file (in my case it's called EntryPoint):

internal sealed class EntryPoint
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread] 
    static void Main(string[] args) 
    {
        try
        {
            Application.SetCompatibleTextRenderingDefault(false);
            Application.EnableVisualStyles();
            ****System.Windows.Application wpfApp = new System.Windows.Application();****             

            App.Start(args);
            if(App.Context != null)
                App.Show();
        }
        catch(Exception ex)
        {
            App.ShowError(Properties.Resources.UnexpectedError, ex);
            Application.Exit();
        }
    }
}

I've added line marked with **** (sorry, can't figure out how to make line bold in code snippet). During several tests I understood, that it doesn't matter what code I add - it can be creating new System.Windows.Window object even without showing it, or any other line of code, which uses any type from WPF assemblies.

So, the main question is: what can cause described behaviour? If anyone faced this problem, or have any ideas, it wold be great if you share your thoughts.

Thank you for reading this not so short question.

like image 786
Sergey Avatar asked Apr 23 '16 18:04

Sergey


1 Answers

Problem solved by adding [DisableDpiAwareness] attribute to startup project. Also, I found more info about this problem:

Mixed WPF and winforms project DPI awareness

DPI Scaling in .Net 3.5 in Mixed WinForms and WPF Application

Hope this will help someone.

like image 162
Sergey Avatar answered Nov 15 '22 11:11

Sergey