1

in daily life we have to deal with a lot of different types of display size it may be mobile or may be a 4k monitor. The screen resolution vary from display to display. Here another important factor is DPI (Display per inch) which is mainly a scaling factor in windows but have a great relation with screen resolution.

Now I give you an example first suppose I run the visual studio application on a Full HD monitor and the resolution is 1920x1080. Suppose I change my display resolution from full HD (1920x1080) to 1366x768. And after that I run the visual studio again on the same display and I see that the user interface (UI) is get slightly bigger and all the controls,text,icons and buttons are perfectly align according to the current monitor DPI scaling and resolution.

And the UI should get bigger as it happens when we run visual studio and other windows application on lower resolution.

I want to apply that similar effect in my WPF application so that each time the screen resolution and DPI changes, my application will automatically adjust according to the display DPI and resolution. I already try Microsoft per monitor DPI aware for windows 10 and edit the app.Mainfest file and uncomment some code according to the GitHub instructions but nothing works in my case.

I also bind with the screen resolution with my application height and width but it's cover whole the screen I don't want that.

But one thing I understand is I have to get the system DPI and then apply it on my window load event but I don't know how to do that in code.

Here is the MainWindow.xaml code (I put just a little amount of code because it's office project )

<Window
    x:Class="WpfApp1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="clr-namespace:WpfApp1"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Name="myMainWindow"
    Title="MainWindow"
    Width="1024"
    Height="547.4"
    AllowsTransparency="True"
    WindowStartupLocation="CenterScreen"
    WindowStyle="None"
    MouseLeftButtonDown="myMainWindow_MouseLeftButtonDown"
    mc:Ignorable="d">
    <Grid Name="MainGrid" Background="#282828">
        <Button
            x:Name="BtnSettings"
            Width="71"
            Height="24"
            Margin="165,14,0,0"
            HorizontalAlignment="Left"
            VerticalAlignment="Top"
            RenderOptions.BitmapScalingMode="NearestNeighbor"
            RenderOptions.ClearTypeHint="Enabled"
            SnapsToDevicePixels="True"
            UseLayoutRounding="True">
            <Button.Content>
                <TextBlock
                    Margin="0,-2,0,0"
                    FontFamily="Segoe UI"
                    FontSize="10"
                    TextOptions.TextFormattingMode="Display"
                    UseLayoutRounding="True">
                    Settings
                </TextBlock>
            </Button.Content>
        </Button>
        <Button
            x:Name="BtnAccounts"
            Width="71"
            Height="24"
            Margin="237,14,0,0"
            HorizontalAlignment="Left"
            VerticalAlignment="Top"
            RenderOptions.BitmapScalingMode="NearestNeighbor"
            RenderOptions.ClearTypeHint="Enabled"
            SnapsToDevicePixels="True"
            UseLayoutRounding="True">
            <Button.Content>
                <TextBlock
                    Margin="0,-2,0,0"
                    FontFamily="Segoe UI"
                    FontSize="10"
                    TextOptions.TextFormattingMode="Display"
                    UseLayoutRounding="True">
                    Accounts
                </TextBlock>
            </Button.Content>
        </Button>
    </Grid>
</Window>

And Here is my back-end c# logic code known as MainWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void myMainWindow_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            DragMove();
        }
    }
}

Another important factor is I also use view box but that does not working as much I wanted. Some people told me that I should use fluid layout and I should use Grid.RowDefinitions and Grid.ColumnDefinitions and I also use * and Auto for resizing and don't use absolute positioning and instead of I should use Margin and padding. I know that it's a issue but my main problem is regarding the DPI and resolution which has nothing to do with absolute positioning.

I should use scale transformation on my MainGrid and that should be bind with the DPI decorator and resolution but I don't have enough ideas that how to do that.

As I say previously that view box is not working because I use the Seoge UI font in my application and if you carefully see my MainWindow.xaml code then you see that I give font size=10 because the latter are taking a very good shape in this size and I also use TextOptions.TextFormattingMode="Display" for my UI design purpose.

My question is how my WPF application automatically detect the screen DPI and resolution and perfectly scale transform according to itself so there no blurry font issue or the Main window is very small etc. Perfectly fit and scale and good looking according to the display.

  • 1
    WPF is already, by default, DPI-aware. See first duplicate. Probably what you are really asking is how to _scale_ UI elements according to the window size. The primary mechanism to accomplish that is `ViewBox`. See e.g. second and third duplicates. If you still need help after reviewing those details, post a new question in which you provide a proper [mcve], along with a clear, concise explanation of what that code does, how that's different from what you want, and what _specifically_ you need help with. – Peter Duniho Apr 25 '21 at 01:10
  • @PeterDuniho This is not any duplicate question. I tried all of this but none of them are working. You don't understand my question what I want to say. My brief question is in lower resolution some WPF application are automatically scaled to match the system DPI. Take an example run the visual studio application in lower resolution and you see the visual studio user interlace are change and icon and text get bigger. I want this in my application. If you already have an demo application then you can give me the download link. – MERUN KUMAR MAITY Apr 25 '21 at 06:32
  • _"Take an example run the visual studio application in lower resolution and you see the visual studio user interlace are change and icon and text get bigger. I want this in my application"_ -- that's the opposite of DPI-aware. The whole point of DPI-aware is that the UI _doesn't_ fundamentally change when the resolution changes. Apps that aren't DPI aware wind up scaling up and down depending on resolution (they get smaller at higher resolutions). In any case, don't use comments for clarifications. Fix the question or post a new one. You can't fit a [mcve] in the comments. – Peter Duniho Apr 25 '21 at 06:39
  • @PeterDuniho sorry sir. You say that that’s the whole opposite of DPI aware. I hope you understand what my problem is. Now tell me what is that? Why in lower resolution apps behave like this ? May be I mixed up concept. Sir, please help me. – MERUN KUMAR MAITY Apr 25 '21 at 06:44
  • https://learn.microsoft.com/en-us/windows/win32/hidpi/high-dpi-desktop-application-development-on-windows – Peter Duniho Apr 25 '21 at 06:47
  • @PeterDuniho Thank you very much sir. Can you please give me a code examples? I already post my MainWindow xml code and back end logic code. Please post the answer with your modified code so that I mark your answer as solved. – MERUN KUMAR MAITY Apr 25 '21 at 07:02

0 Answers0