'Create WPF window with specified client area size with working content resizing and equal client area size in Win10 and Win7
One approach is SizeToContent, but then the content resizing stops working. Or is there a way to turn it back on? Or maybe with some anchors.
[screenshot - SizeToContent resizing window]
]
I was told that in Win10 the thick borders from Win7 are still there, only invisible. For some reason they are still included in the window size.
[screenshot - Win10 window width with invisible borders]![]](https://i.stack.imgur.com/Ji4Ob.jpg)
[screenshot - Win7 window width with visible borders]![]](https://i.stack.imgur.com/GMCNz.jpg)
If I want on Win10 with invisible thick borders a standard window with titlebar with specified client area size (for example 400x400) which has resizable content, I finally managed to do it with help from SystemParameters.WindowNonClientFrameThickness. But still, I don't understand why I need to multiply it by 4 and 3 and not by 2 and 1.
<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" MouseDown="Window_MouseDown">
<Grid x:Name="Grid" Background="Bisque" >
</Grid>
</Window>
using System.Windows;
using System.Windows.Input;
namespace TEST
{
public partial class MainWindow : Window
{
public MainWindow()
{
Width = 400
+ SystemParameters.WindowNonClientFrameThickness.Bottom * 4; // why not * 2 ?
Height = 400
+ SystemParameters.WindowNonClientFrameThickness.Top
+ SystemParameters.WindowNonClientFrameThickness.Bottom * 3; // why not * 1 ?
InitializeComponent();
}
private void Window_MouseDown(object sender, MouseButtonEventArgs e)
{
MessageBox.Show(
"Window width = " + Width.ToString()
+ "\nWindow height = " + Height.ToString()
+ "\nGrid/Client width = " + Grid.ActualWidth.ToString()
+ "\nGrid/Client height = " + Grid.ActualHeight.ToString()
+ "\n"
+ "\nWindowResizeBorderThickness = " + SystemParameters.WindowResizeBorderThickness.ToString()
+ "\nWindowNonClientFrameThickness = " + SystemParameters.WindowNonClientFrameThickness.ToString()
+ "\nCaptionHeight = " + SystemParameters.CaptionHeight.ToString()
+ "\nResizeFrameHorizontalBorderHeight = " + SystemParameters.ResizeFrameHorizontalBorderHeight.ToString()
+ "\nResizeFrameVerticalBorderWidth = " + SystemParameters.ResizeFrameVerticalBorderWidth.ToString()
+ "\nThickHorizontalBorderHeight = " + SystemParameters.ThickHorizontalBorderHeight.ToString()
+ "\nThickVerticalBorderWidth = " + SystemParameters.ThickVerticalBorderWidth.ToString()
+ "\nThinHorizontalBorderHeight = " + SystemParameters.ThinHorizontalBorderHeight.ToString()
+ "\nThinVerticalBorderWidth = " + SystemParameters.ThinVerticalBorderWidth.ToString()
);
}
}
}
On Win10 it now works.
[screenshot - Win10 client area 400x400]![]](https://i.stack.imgur.com/c2UWs.jpg)
But on Win7 it stops working because of that multiplication.
[screenshot - Win7 client area 414x414]![]](https://i.stack.imgur.com/0JNZG.jpg)
So does exist some SystemParameters constant or whatever which indicates that there is an invisible border?
Or which constants do I add to my client area size?
Or do I really have to check if the OS is Win10? (or newer than Win7)
Or is there a normal way to do it that I'm missing.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
