WPF application the same size for each scale in the system (regardless of scale)

Is there a way to make my WPF application the same for all system scales?

When I change the Resize text, apps and other items in Windows system settings from 125% (recommended) to 100% to Full-HD screen, my WPF app becomes too small. To implement an independent system scale application, I wrote a function like this to change the scaling of my application to 125%:

private void ScaleTo125Percents()
{
    // Change scale of window content
    MainContainer.LayoutTransform = new ScaleTransform(1.25, 1.25, 0, 0);
    Width *= 1.25;
    Height *= 1.25;

    // Bring window center screen
    var screenHeight = System.Windows.SystemParameters.PrimaryScreenHeight;
    var screenWidth = System.Windows.SystemParameters.PrimaryScreenWidth;
    Top  = ( screenHeight - Height ) / 2;
    Left = ( screenWidth  - Width )  / 2;
}

      

But there are conditions for calling this function. The first of the screen should be Full-HD (there are APIs for this) and also the system scale should be 100% (there is no .NET API to get the system scale).

What can I do? Am I doing a standard way to make an independent app scale?

An example of scale independent applications I've seen:

  • Visual Studio 2017 Installer
  • Telegram desktop
+3


source to share


1 answer


Finally found the answer. First, get the system DPI scale using one of the following options:
  • Read the AppliedDPI

    dword registry located at Computer\HKEY_CURRENT_USER\Control Panel\Desktop\WindowMetrics

    . Then divide it by 96.
  • Or use this snippet:

    double dpiFactor = System.Windows.PresentationSource.FromVisual(this).CompositionTarget.TransformToDevice.M11;
    
          

    which returns a value between 1.0 and 2.5

Then create a config file that contains the application settings and set dpiFactor as the default scale. If the user prefers a custom scale, call this function when the window starts:



private void UserInterfaceCustomScale(double customScale)
{
    // Change scale of window content
    MainContainer.LayoutTransform = new ScaleTransform(customScale, customScale, 0, 0);
    Width *= customScale;
    Height *= customScale;

    // Bring window center screen
    var screenHeight = System.Windows.SystemParameters.PrimaryScreenHeight;
    var screenWidth = System.Windows.SystemParameters.PrimaryScreenWidth;
    Top  = ( screenHeight - Height ) / 2;
    Left = ( screenWidth  - Width )  / 2;
}

      

+2


source







All Articles