0

I've made a little WPF test-application with buttons in different units. Now I run this on a Screen with 96dpi and another with 226dpi.

What I expect is that the button without unit and the button with px unit would get smaller than the other buttons on the 226dpi display - but they keep all the same size.

How do I force the "pixel"-Buttons to use real pixels? Independent of the dpi-resolution or if the user set the scaling to 200%.

In my real application I want the Menüs and Trees and so on to be dpiaware, but in the middle I have some sort of graphics in a usercontrol and I want this usercontrol to use real pixels.

Here is the democode:

<Window x:Class="WpfApp2.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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">
  <Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="*" />
        <RowDefinition Height="*" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <Button Grid.Row="0" Grid.Column="0" Content="96 x 48" Width="96" Height="48" />
    <Button Grid.Row="1" Grid.Column="0" Content="96px x 48px" Width="96px" Height="48px" />
    <Button Grid.Row="2" Grid.Column="0" Content="1.0in x 0.5in" Width="1.0in" Height="0.5in" />
    <Button Grid.Row="3" Grid.Column="0" Content="2.54cm x 1.27cm" Width="2.54cm" Height="1.27cm" />
  </Grid>
</Window>  
GreenEyedAndy
  • 1,485
  • 1
  • 14
  • 31
  • Have you tested this with cm instead? Thats will keep the button to that size regardless of the DPI. – XAMlMAX Apr 03 '19 at 10:35
  • @XAMlMAX - have you looked on the demo-code? The last button is in cm and it doesn't work if the user is scaling to e.g. 150%. – GreenEyedAndy Apr 03 '19 at 11:53
  • WPF is using device independent units to measure elements sizes, accrording to [msdn](https://learn.microsoft.com/en-us/dotnet/api/system.windows.frameworkelement.width?view=netframework-4.7.2) You can have a look at this [thread](https://stackoverflow.com/questions/44683626/wpf-application-same-size-at-every-system-scale-scale-independent) and try to use such workaround – Pavel Anikhouski Apr 03 '19 at 12:28
  • Yes I have looked at the code, and that's why I asked about the results of tests with `cm` instead of `px`, don't forget I am trying to help here. If the graphic is the problem, then try to use vector graphic instead? It would make it look good on any DPI or resolution. – XAMlMAX Apr 03 '19 at 13:45
  • If you want size in 'real pixes' you need to calculate all desired sizes yourself, basing on current DPI. – Maciek Świszczowski Apr 03 '19 at 13:56

1 Answers1

0

I found this little Decorator-Class and using it solves my problem.

Decorator

But now there is a problem if someone has e.g. three monitors and uses different scaling on each monitor. The Decorator doesn't use the the setting for the monitor where the application is running.

GreenEyedAndy
  • 1,485
  • 1
  • 14
  • 31