none
DPI Issue with Winforms application

    Question

  • I got a question about DPI issue with winforms applications.

    I am having a winforms C# application, built with .NET 4.5.2 using VS 2013. Many of our customers are using Window 8 or 8.1 and Windows 10 and some of them are complaining about the DPI issue when they switch between laptop and big monitors. To solve this I have added the below section to my manifest file:

    <windowsSettings>

            <!-- Per Monitor V1 [OS >= Windows 8.1]

             Values: False, True, Per-monitor, True/PM -->

            <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">

              true/PM

            </dpiAware>

            <!-- Per Monitor V1 [OS >= Windows 10 Anniversary Update (1607, 10.0.14393, Redstone 1)]

             Values: Unaware, System, PerMonitor -->

            <!-- Per Monitor V2 [OS >= Windows 10 Creators Update (1703, 10.0.15063, Redstone 2)]

             Value: PerMonitorV2 -->

            <dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">

              PerMonitorV2, PerMonitor

            </dpiAwareness>

    </windowsSettings>

    But still some issues are not solved. We could see issues with some user controls and also while creating OLE objects from our app to MS word (not getting the size of the objects properly).

    I understand that this issue has been addressed with .NET 4.7 but unfortunately we don’t have a plan to upgrade this year. Could you please suggest some fixes or documents that I could follow to fix this? Thank you.

    Tuesday, February 6, 2018 1:40 PM

All replies

  • Hi BKoshy,

    Try to set the AutoScaleMode property of your form to Dpi instead of Font:

    Then change some code in the Program.cs:

    class Program
    {
        [STAThread]
        static void Main()
        {
            if (Environment.OSVersion.Version.Major >= 6)
                SetProcessDPIAware();
    
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        private static extern bool SetProcessDPIAware();
    }

    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.

    Wednesday, February 7, 2018 2:02 AM
    Moderator
  • Thanks Stanly, but unfortunately we can't modify all the forms properties. Its huge project.

    I am looking for some configuration level settings to control this. Thanks

    Regards

    Biju Koshy

    Wednesday, February 7, 2018 12:07 PM
  • Hi Koshy,

    If you do not want to set the Form, I'm afraid you can only do it with app.manifest file and set the following:

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

    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.

    Thursday, February 8, 2018 2:33 AM
    Moderator
  • Actually, Biju, Stanly's suggestion to put those changes into the static Main may end up working OK for you, without having to change the AutoScaleMode = Dpi in every Form. I can see a difference in how a Form is displayed with only that change in Main and no other changes.

    Now, that's assuming that the static Main() method is in a separate Program.cs file, and not imbedded in every Form.

    I assume since you said that you can't modify every Form, that means that you don't have your own "Framework" of base Control-type classes from which all your UI stuff is derived. If you had a BaseForm class, you'd only have to modify that AutoScaleMode on that one class, and it would automatically be on all the derived classes. Too late for this particular application if you don't have that, but you might consider doing something like that for future endeavors. For future reference, here's one of my blog posts:

    https://geek-goddess-bonnie.blogspot.com/2009/10/why-and-how-to-sub-class-base-classes.html


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Monday, February 12, 2018 6:21 AM