Click here to Skip to main content
15,891,981 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a window with the following properties:

CSS
ShowInTaskbar="False" WindowStyle="None" ResizeMode="NoResize" Topmost="True" AllowsTransparency="True" Background="Transparent"


and I want to maximize the window after pressing a button with the following line:

CSS
this.WindowState = System.Windows.WindowState.Maximized;


in doing so, the window is positioned me in the upper left but it is not maximized.

also tested with the following lines but did not work:

CSS
this.Height = SystemParameters.MaximizedPrimaryScreenHeight;
this.Width = SystemParameters.MaximizedPrimaryScreenWidth;
this.Top = 0; this.Left = 0;


Anyone know how to solve it?

Thank you very much.
Posted
Updated 6-Mar-18 3:42am
v2

Looks strange. Is so, I would temporary modified resize mode and them maximized. You may also need to handle the event StateChanged or override the virtual protected method StateChanged, to make sure you have the same ResizeMode when you change the WindowState again, either by assigning a new value to it, calling, for example RestoreBounds or by the UI user. Please see:
http://msdn.microsoft.com/en-us/library/system.windows.window.statechanged%28v=vs.100%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.window.onstatechanged%28v=vs.100%29.aspx[^];
see also:
http://msdn.microsoft.com/en-us/library/system.windows.window.resizemode%28v=vs.100%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.window.windowstate%28v=vs.100%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.window.restorebounds%28v=vs.100%29.aspx[^].

Good luck,
—SA
 
Share this answer
 
-better late then never-

From your code fragment I am assuming that you created your own custom window. First of all: if you use AllowsTransparency your window will not be able to display any old style controls (for example: web browser). Unfortunately, without this, the window will not snap to screen.

Putting that aside, what I did to maximize correctly is to

1. Add a maximize/restore behavior to the max/restore window button. This behavior simply triggers maximize/restore command.

2. In my XAML the entire window logic is inside a border. Like this...
XML
<Border x:Name="PART_WindowBorder">
<!-- window def. here -->
</Border>
3. My maximize/restore command looks like this.
C#
public void Execute(object parameter)
{
    var window = parameter as Window;

    if (window != null)
    {
        // Get border part.
        Border border = window.Template.FindName("PART_WindowBorder", window) as Border;

        // Fix border margin with maximizing (WPF bug)...
        if (window.WindowState == WindowState.Maximized)
        {
            border.Margin = new Thickness(0);
            window.WindowState = WindowState.Normal;
        } 
        else
        {
            border.Margin = new Thickness(6);
            window.WindowState = WindowState.Maximized;
        }
    }
}
So you simply increase the margin to 6 when maximized (offset is 7, my border is 1). In here, you can also check for correct window version and read margin using GetSystemMetrics.

Regards,
Tomaz
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900