1 year ago
#256675
Leo Nardo
Why is WPF on Win10 lying to me about window dimensions?
I have created a minimal C# WPF app with 400x400 dimensions window. (Also not working without "px" or creating window in code.)
<Window x:Class="TEST.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"
xmlns:local="clr-namespace:TEST"
mc:Ignorable="d"
Title="TEST" Height="400px" Width="400px" MouseDown="Window_MouseDown">
<Grid>
</Grid>
</Window>
On mouse click there is MessageBox showing window's width, which shows correct value 400.
using System.Windows;
using System.Windows.Input;
namespace TEST
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_MouseDown(object sender, MouseButtonEventArgs e)
{
MessageBox.Show("Width = " + Width.ToString());
}
}
}
But when I measured it in Windows Paint, the same exe is correct on Win7 and not on Win10. The same problem is with Height.
I can't find out what shrinks/transforms window on (my) Win10 or where is the problem.
EDIT: You can stop reading here, answer is in the first comment.
Same problem is on 24" Full HD pc and on 15.6" Full HD laptop. Both 100% DPI OS scale.
I just want to create window with exact pixel width/height.
Or somehow convert that 1/96 double-value things correctly on Win10 to pixels on 100% DPI-unscaled display.
The only working workaround on Win10 I found is using titleless window with help from System.Windows.Shell.WindowChrome. Then the dimensions are correct on Win10. But on that road I would have to make my own titlebar.
And setting SizeToContent does't seem like solution because then the content will not resize with resizing window. Or maybe it can be done this way and for example some grid can be modified to resize after initial SizeToContent of a window? But still, that can't be a plan A.
Some little other solution would be being able to retrieve exact values from some system display/style constants and modify my window dimensions with them on Win10.
Tested on .NET 6 and .NET Framework 4.0 and both with this problem, so I thing this shrinking has something to do with Windows 10, maybe even 11. (tested on latest Win10 version)
c#
wpf
windows-10
window
dimensions
0 Answers
Your Answer