0

I have a window in an application created with WPF and C#. I have set MaxHeight=200, MaxWidth=500 and StartupLocation=CenterScreen.However maximizing this window makes it positioned on the left upper region of the screen i.e: not in the center any more! The maximized window is always on the left upper part even though I set Left=200 and Top=200 in the Window_StateChanged event on the condition of maximizing.

  private void Window_StateChanged(object sender, EventArgs e)
   {
       if (this.WindowState == WindowState.Maximized)
       {
       //   Left = System.Windows.SystemParameters.WorkArea.Width - Width;
          Left = 200;
        //  Top = System.Windows.SystemParameters.WorkArea.Height - Height;
         Top = 200;          
       }
   }

What should I do to make sure the maximized windows is positioned in the center? Thanks very much in advance.

Doua Ali
  • 175
  • 1
  • 5
  • 21

3 Answers3

2

When in Window_StateChanged you detect Maximized set it back to Normal. Take a look at this: disable maximize capacity in a wpf window

Emilio Lucas Ceroleni
  • 1,559
  • 2
  • 9
  • 13
  • Thanks, this works fine but restoring down the window doesn't restore it to its normal width and height. Any hints, please? – Doua Ali Dec 02 '17 at 09:41
  • I mean, after clicking the maximize button I need it to turn into the restore down button because it doesn't. – Doua Ali Dec 02 '17 at 11:00
  • Well, you can do that when handling Window_StateChanged for example, setting the size back again. But I think wat you need is a dialog box. Check this out: https://learn.microsoft.com/en-us/dotnet/framework/wpf/app-development/dialog-boxes-overview#custom-dialog-boxes – Emilio Lucas Ceroleni Dec 02 '17 at 17:15
1

You can't set the position of a maximised window. By definition, it tries to take up the whole screen so will always position the top left to 0,0. All you can do for your needs is to intercept the State changed event, set the window sizes to your max values and then set the State back to WindowState.Normal

  • Thanks, this works fine but restoring down the window doesn't restore it to its normal width and height. Any hints, please? – Doua Ali Dec 02 '17 at 10:58
  • I mean, after clicking the maximize button I need it to turn into the restore down button because it doesn't. – Doua Ali Dec 02 '17 at 11:00
  • You won't be able to do this automatically as you are hijacking the Maximise function. If intercepting the State change, store the previous size somewhere. You'll then need to create your own Restore button as the Window thinks its in the WindowState.Normal – Andrew Baylis Dec 03 '17 at 21:58
0

You can do this by overriding WndProc

protected override void WndProc( ref Message m ) 
   { 
       if( m.Msg == 0x0112 ) //WM_SYSCOMMAND
       { // Check your window state here
           if (m.WParam == new IntPtr( 0xF030 ) ) // Maximize event - SC_MAXIMIZE from Winuser.h  
       { // THe window is being maximized }
       }
       base.WndProc(ref m);
    }

For more reference check out this link https://www.google.co.in/url?sa=t&source=web&rct=j&url=https://stackoverflow.com/questions/1295999/event-when-a-window-gets-maximized-un-maximized&ved=0ahUKEwjPk9zUve3XAhVBGpQKHRCsAKEQFggkMAA&usg=AOvVaw1bgX8XejRP3t2tJ4txKQrq

Satish R
  • 153
  • 2
  • 12