0

I'm working on an application c# WPF. I have my own title bar and I coded function close, minimize and maximize. I'm working on PC with 2 screens and I have a problem when I maximize window. On main screen, If I set WindowState = WindowState.Maximized the task bar (the bar from windows 7) is hidden. I added a function to set MaxHeight depending on screen but when I change MaxHeight, the ActualHeight doesn't change. Concretely, when I maximize window, the window goes full screen. The second time I maximize, the height is correct, the task bar is not hidden.

What can I do to refresh height when changing MaxHeight ?

Here is my code :

    protected override void OnStateChanged(EventArgs e)
    {
        base.OnStateChanged(e);
        if (WindowState == WindowState.Maximized)
        {
            var hwnd = new System.Windows.Interop.WindowInteropHelper(this).EnsureHandle();
            var currentMonitor = NativeMethods.MonitorFromWindow(hwnd, NativeMethods.MONITOR_DEFAULTTONEAREST);
            var primaryMonitor = NativeMethods.MonitorFromWindow(IntPtr.Zero, NativeMethods.MONITOR_DEFAULTTOPRIMERTY);
            var isInPrimary = currentMonitor == primaryMonitor;

            if (isInPrimary)
                this.MaxHeight = SystemParameters.MaximizedPrimaryScreenHeight;
            else
                this.MaxHeight = Double.PositiveInfinity;
#if DEBUG
            var aH = this.ActualHeight;
            var h = this.Height;
            var mH = this.MaxHeight;
            if (aH != h || aH > mH)
            {
                System.Diagnostics.Debugger.Break();
                this.Height = this.MaxHeight;
            }               
#endif
        }
    }
A.Pissicat
  • 3,023
  • 4
  • 38
  • 93
  • 1
    Changing the Height property on a maximized window cannot have any affect. Setting MaxHeight *after* maximizing the window cannot have any affect, you'll have to do it *before* it gets maximized. Google "wpf avoid taskbar overlap on borderless window" to find out how to do it correctly. – Hans Passant Aug 17 '16 at 11:03
  • Indeed, I have move this piece of code before maximize and it works good. Thank you ! – A.Pissicat Aug 17 '16 at 12:45

1 Answers1

0

I´m no specialist in C# but had this problem in Pascal/Delphi. I had to specify the size of the form manually like so:

procedure MinMax; 
begin
    if Form.WindowState=wsNormal then
    begin
        Form.WindowState:=wsMaximized;
        with Screen.WorkAreaRect do
          Form.SetBounds(Left, Top, Right - Left, Bottom - Top);
    end
    else
    begin
         Form1.WindowState:=wsNormal;
    end;
end;

The Screen.WorkAreaRect gives you the actual rectangle without the taskbar-region, which can be placed top/left/bottom/right.

Hope that could help you.

BionicWave
  • 26
  • 3