locked
Bug windows forms with Windows 10, HighDPI (DPIAwareness)) RRS feed

  • Question

  • Hello,

    I have a minimalist Windows Forms project that used to behave correctly, and behaves incorrectly under Windows 10 with High DPI support.

    (the project can be downloaded here : http://chachatelier.fr/temp/TestWinFormsW10.zip

    As soon as I add the ability to support HighDPI, the controls positioning becomes wrong.

    See images (correct under W7 or W10 withouth HighDPI, then incorrect under W10+HighDPI)

    "Adding the ability to support HighDPI" means :

    -use .NET framework 4.7

    -declare Windows 10 as supported in the manifest (<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}"/>)

    -use various properties in app.config and manifest (<add key="DpiAwareness" value="PerMonitorV2"/>...)  (with docs like https://docs.microsoft.com/en-us/dotnet/framework/winforms/high-dpi-support-in-windows-forms

    How can I fix the wrong behaviour under W10 ?




    Thursday, November 22, 2018 9:16 AM

All replies

  • Hi,

    In general, we can solve some issues about DPI by setting the AutoScaleMode property of form to Dpi instead of Font.

    Try to modify the "TestWinFormsW10.cpp" like the code shown as follows:

        #include "stdafx.h"
        #include "MyPanel.h"
        using namespace System;
        using namespace System::Runtime::InteropServices;
    
        // add this statement
        [DllImport("user32.dll", EntryPoint = "SetProcessDPIAware", CallingConvention = CallingConvention::Cdecl, CharSet = CharSet::Ansi)]
        bool SetProcessDPIAware();
    
        [System::STAThreadAttribute]
        int main(array<System::String^>^ args)
        {
            OperatingSystem ^ osv = Environment::OSVersion;
            // code omitted
            Version ^ version = osv->Version;
            if (version->Major >= 6)
            {
                SetProcessDPIAware();
            }
    
            // Activation des effets visuels de Windows XP avant la création de tout contrôle
            System::Windows::Forms::Application::EnableVisualStyles();
            System::Windows::Forms::Application::SetCompatibleTextRenderingDefault(false);
            // code omitted
        }

    Regards,

    Stanly


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, November 23, 2018 9:58 AM
  • Adding SetProcessDPIAware(); doesn't change anything. I assume that my manifest and config files already enabled it.

    Changing AutoScaleMode::Font to AutoScaleMode::Dpi doesn't change anything either (it can become a total mess under W7 and W10 when using Dpi and AutoScaleDimensions, but it never made it work under W10)


    • Edited by C.h.a.c.h.a_ Monday, November 26, 2018 2:47 PM typo
    Friday, November 23, 2018 10:43 AM
  • Hi,

    I'm not sure if it is a BUG, and according to your description, I'm afraid this issue is more related about Windows 10 system, try to ask this question at Windows 10 forum.

    Regards,

    Stanly


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, November 26, 2018 2:36 AM