Elior
Elior

Reputation: 3266

Maximize custom WPF form to be as the screen size

I'm trying to create a custom form in WPF. I set the windowStyle to be None

I added 3 buttons :

  1. Close
  2. Max/Min
  3. Hide

What I'm trying to do is when the user clicks on the second button is , if the form in normal state , maximize the form to be as the screen size, else if the form is on maximize state , set the form to the initial size..

This is what I tried, but nothing happens when I first click on the second button

  private bool maximized = false;
  private void button2_Click(object sender, RoutedEventArgs e)
    {
        if (!maximized)
        {
            this.MaxHeight = SystemParameters.PrimaryScreenHeight;
            this.MaxWidth = SystemParameters.PrimaryScreenWidth;
            this.WindowState = System.Windows.WindowState.Maximized;
            maximized = true;
        }
        else
        {
            this.WindowState = System.Windows.WindowState.Normal;
            maximized = false;
        }
    }

Am I need to add Invalidate or something like that?

Upvotes: 0

Views: 1778

Answers (1)

Elior
Elior

Reputation: 3266

Ok, I Solved it.. When I created the xaml file.. I added a rectangle so the form body will be the rectangle.. I needed to change the rectangle width and height instead..

  private void button2_Click(object sender, RoutedEventArgs e)
    {
        if (!maximized)
        {

            this.FormBody.Width = SystemParameters.WorkArea.Width; //rectangle's width
            this.FormBody.Height = SystemParameters.WorkArea.Height;// rectangle's height
            this.WindowState = System.Windows.WindowState.Maximized;
            maximized = true;
        }
        else
        {
            this.WindowState = System.Windows.WindowState.Normal;
            maximized = false;
        }
    }

I also change the SystemParameters.PrimaryScreenWidth to be SystemParameters.WorkArea.Width and also with the Height so in this way the form wont exceed the taskbar

Upvotes: 1

Related Questions