Reputation: 175
TL;DR
It seems setting the AllowTransparency
to True
offsets the window up and left by 8 pixels when maximized, I need to get it back on screen.
In-depth
It looks like I am making a mistake somewhere, but am not sure why. I cannot post the entire code, but I will try to write everything relevant here.
I have my main window, which has a custom title bar. That means I had to set WindowsStyle
to None
and I was also using a template. Right from the start, I had problems with window being much bigger than my desktop, while maximized. It was going 8 pixels off screen on ALL SIDES. I ditched the template and tried to alter the existing window to suit my needs. By setting AllowTransparency
back to false
, I managed to get rid of the leakage on the sides. Problem that then occurred, is the border on top of the window, which I removed by setting the CaptionHeight
of WindowChrome
to 0.
Okay, the leakage is gone. But I believe my current problem is connected with what I wrote above. That is, I can't bind width and height to something, there is no appropriate property. This is what I tried from SystemParameters
:
SystemParameters.FullPrimaryScreenHeight;
SystemParameters.FullPrimaryScreenWidth;
SystemParameters.MaximizedPrimaryScreenHeight;
SystemParameters.MaximizedPrimaryScreenWidth;
SystemParameters.PrimaryScreenHeight;
SystemParameters.PrimaryScreenWidth;
SystemParameters.WorkArea.Height;
SystemParameters.WorkArea.Width;
SystemParameters.VirtualScreenWidth;
The actual height required (1048) is somewhere between SystemParameters.WorkArea.Height
(1040) and SystemParameters.MaximizedPrimaryScreenHeight
(1056).
And the actual width required (1928) is somewhere between SystemParameters.WorkArea.Width
(1920) and SystemParameters.MaximizedPrimaryScreenWidth
(1936).
You can see the pattern here.
This is my window code
<Window x:Class="MyApp.MainWindow"
...
WindowState="Normal"
ResizeMode="CanResizeWithGrip"
MaxHeight="{Binding SystemParameters.WorkArea.Height}"
WindowStyle="None">
<WindowChrome.WindowChrome>
<WindowChrome CaptionHeight="0"/>
</WindowChrome.WindowChrome>
....content....
</Window>
What also doesn't work:
- Window.Padding
- Window.Margin
- Window.Top
- WindowStyle="SingleBorderWindow"
What I have but don't want
XAML:
MaxWidth="{Binding MaximumWidth}"
C#
public double MaximumWidth
{
get
{
// I don't want anything hard-coded.
// Some other (correct) parameter would be fine as well,
// if we can't get the window on screen.
return SystemParameters.WorkArea.Width + 8;
}
}
Update: SingleBorderWindow
This is what happens when using WindowStyle="SingleBorderWindow"
.
So, not only is my window 8 pixels short to both my right and bottom edges (while binding to WorkArea), but you can see the actual window behind and the buttons are clickable (even where they are not visible). Really weird. It is like there are two windows, but only the one I need is not on the right location.
Upvotes: 3
Views: 4457
Reputation: 39
Here is an improvement of @Matus' answer which eliminates the hard-coded constants. The hard-coded constants are not correct in all situations (monitor resolution, Windows versions, personalization settings).
This code answers the question about calculating the size (and position) of the maximized window. It leaves some things to be desired, but they are outside of the scope of the question:
public MainWindow()
{
InitializeComponent();
// Settings for WindowsStyle.SingleBorderWindow, ThreeDBorderWindow, ToolWindow:
WindowStyle = WindowStyle.SingleBorderWindow;
WindowState = WindowState.Normal;
ResizeMode = ResizeMode.CanMinimize; // Disable maximizing and resizing
double differenceX = SystemParameters.MaximizedPrimaryScreenWidth - SystemParameters.WorkArea.Width;
double differenceY = SystemParameters.MaximizedPrimaryScreenHeight - SystemParameters.WorkArea.Height;
Left = -differenceX / 2;
Top = 0;
Width = SystemParameters.MaximizedPrimaryScreenWidth;
Height = SystemParameters.MaximizedPrimaryScreenHeight - differenceY / 2;
// Settings for WindowsStyle.None:
//WindowStyle = WindowStyle.None;
//WindowState = WindowState.Normal;
//ResizeMode = ResizeMode.CanMinimize;
//Left = 0;
//Top = 0;
//Width = SystemParameters.WorkArea.Width;
//Height = SystemParameters.WorkArea.Height;
}
Upvotes: 0
Reputation: 437
In case anyone else lands on this as a google result, this is what I found out.
It is not related to AllowTransparency, I have it set to false on my window. When the window is maximized, it gains an 8px margin, that is outside the screen.
I needed to have a WindowState.Normal window that would have the same working size as a maximized window on FullHD resolution with fixed TaskBar.
The solution was this:
this.MainWindow.WindowState = WindowState.Normal;
this.MainWindow.Width = 1920 + 2 * 8;
// 40px is assumed to be taken by the TaskBar
this.MainWindow.Height = 1080 - 40 + 2 * 8;
this.MainWindow.Left = -8;
this.MainWindow.Top = -8;
Upvotes: 0
Reputation: 1175
Oh. If I guess well, in your code which is hidden for us, on a certain event, to maximize your window, you change it's width to SystemParameters.MaximizedPrimaryScreenWidth. Wrong. Bind to WindowState and change it to WindowState.Maximized.
Upvotes: 0
Reputation: 306
The actual height required (1048) is somewhere between SystemParameters.WorkArea.Height (1040) and SystemParameters.MaximizedPrimaryScreenHeight (1056).
And the actual width required (1928) is somewhere between SystemParameters.WorkArea.Width (1920) and SystemParameters.MaximizedPrimaryScreenWidth (1936).
I think you need to understand Windows Resolutions to help you solve this and
i believe 15 inches Window is 1366 X 768 in resolution Width x Height
so you using resolution higher than this will make your app bigger in size than default system resolution. Hope this help
Upvotes: 0