Reputation: 321
When creating a WPF window with AllowsTransparency="True" WindowStyle="None"
and maximizing it via this.WindowState = WindowState.Maximized;
the Window gets bigger than my screen.
When setting AllowTransparency="False"
i have a border around my Window, but the window won't get bigger than my screen.
In my Case I have a 1920x1080 screen and the window becomes 1934x1094.
On my 1280x1024 screen the window will become 1294x1038.
The Window will become still as big as this, whether or not AllowTransparency is enabled or not, yet with it disabled it works properly.
Setting AllowTransparency before maximizing doesen't work and throws an InvalidOperationException.
How to get a WPF window without a Windows-style border, but yet to maximize properly?
Upvotes: 16
Views: 10875
Reputation: 29028
Usually a window always has a resize border. Now, when you maximize the window Windows inflates the window size by the thickness of the resize border. This way the border gets pushed out of the screen i.e. the resize border will be hidden giving the window a clean full-screen appearance.
The important point is that Windows expects this resize border, while WPF allows to drop it from the Window
template.
If you override the window chrome or effectively set Window.WindowStyle
to WindowStyle.None
you will lose the resize border around the window. Windows won't recognize this and continues to inflate the Window
with the goal to hide the resize border. Because in our case (WindowStyle.None
) the border got dropped from the template Windows effectively clips the window itself.
The solution is to compensate for the expected resize border thickness yourself.
You can do this by adding an additional resize Border
to your Window
template and define a Trigger
to handle the WindowState.Maximized
case (see Solution 1).
Alternatively, simply inflate and deflate the current Window.BorderThickness
(see Solution 2).
Instead of using a randomly guessed thickness value like 8
as suggested by other answers you should use the SystemParameters.WindowResizeBorderThickness
constant.
Add the window resize Border back to the template. It gets dropped when setting Window.WindowStyle
to WindowStyle.None
.
<Style TargetType="Window">
<Setter Property="WindowStyle"
Value="None" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Window}">
<Border x:Name="ResizeBorder">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<Grid Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" /> <!-- Title bar row -->
<RowDefinition /> <!-- Window.Content row -->
<RowDefinition Height="Auto" /> <!-- Resize gripper row -->
</Grid.RowDefinitions>
<AdornerDecorator Grid.Row="1">
<Border Background="Transparent"
Margin="{x:Static SystemParameters.WindowNonClientFrameThickness}">
<ContentPresenter />
</Border>
</AdornerDecorator>
</Grid>
</Border>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="WindowState"
Value="Maximized">
<Setter TargetName="ResizeBorder"
Property="BorderThickness"
Value="{x:Static SystemParameters.WindowResizeBorderThickness}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Inflate the Window.BorderThickness
when the Window.WindowState
property changes:
MainWindow.xaml.cs
partial class MainWindow : Window
{
private WindowState OldWindowState { get; set; }
public MainWindow()
{
InitializeComponent();
}
protected override void OnStateChanged(EventArgs e)
{
base.OnStateChanged(e);
this.BorderThickness = this.WindowState switch
{
WindowState.Maximized => InflateBorder(SystemParameters.WindowResizeBorderThickness),
WindowState.Normal or WindowState.Minimized when this.OldWindowState == WindowState.Maximized => DeflateBorder(SystemParameters.WindowResizeBorderThickness),
_ => this.BorderThickness
};
this.OldWindowState = this.WindowState;
}
private Thickness InflateBorder(Thickness thickness)
{
double left = this.BorderThickness.Left + thickness.Left;
double top = this.BorderThickness.Top + thickness.Top;
double right = this.BorderThickness.Right + thickness.Right;
double bottom = this.BorderThickness.Bottom + thickness.Bottom;
return new Thickness(left, top, right, bottom);
}
private Thickness DeflateBorder(Thickness thickness)
{
double left = this.BorderThickness.Left - thickness.Left;
double top = this.BorderThickness.Top - thickness.Top;
double right = this.BorderThickness.Right - thickness.Right;
double bottom = this.BorderThickness.Bottom - thickness.Bottom;
return new Thickness(left, top, right, bottom);
}
Upvotes: 1
Reputation: 3518
Unfortunately there is no good solution to this other than to make a WindowStyle="None"
window without resizing, and handle everything yourself. This means your own maximize/minimize/restore buttons that set the window dimensions to fit the screen. Your own live caption area for dragging. Your own borders with the appropriate cursors. Your own double-click handler to maximize/restore. Your own routine for checking the mouse position against the height of the screen for drag-to-dock. Etc. You get the idea. It's a pain in the neck, but if you do it once at least you'll have it for all future projects. Unfortunately you will lose the "Aero" animations, but alas.
I also want to point out one reason why this issue is very important. In at least some cases, WPF can't make full use of accelerated graphics when windows span two monitors (as they normally do any time a window is maximized). That means performance of D3DImage
, as well as any Effect
, suffers when the window is maximized. It was happening for me, and many of my users, which is what drew my attention to this issue.
Upvotes: 0
Reputation: 53
If you only care about the correct dimensions of your window and have no trouble with the appearance, you can wrap the contents of the window in System.Windows.Controls.Canvas
like this:
<Canvas Name="MyCanvas" Width="auto" Height="auto">
...
</Canvas>
Then you can use MyCanvas.ActualWidth
and MyCanvas.ActualHeight
to get the resolution of the current screen, with DPI settings taken into account and in device independent units.
It doesn't add any margins like the maximized window itself does.
It should work with multiple monitors too.
(Canvas accepts UIElement
s as children, so you should be able to use it with any content.)
Upvotes: 0
Reputation: 5005
A couple year late to the party but just increase your this.borderthickness
for the SizeChanged event like so:
<Window x:Class="MyApp.MainWindow"
ResizeMode="CanResize"
WindowStyle="SingleBorderWindow"
SizeChanged="Window_SizeChanged">
....
Code
....
</Window>
public void Window_SizeChanged(object sender, SizeChangedEventArgs e)
{
if (this.WindowState == WindowState.Maximized)
{
this.BorderThickness = new System.Windows.Thickness(8);
}
else
{
this.BorderThickness = new System.Windows.Thickness(0);
}
}
Upvotes: 9
Reputation: 37
set this property of you window.
MinHeight="100"
MinWidth="100"
Height="auto"
Width="auto"
Hope this will work
Upvotes: -2