问题
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.
回答1:
When in Window_StateChanged you detect Maximized set it back to Normal. Take a look at this: disable maximize capacity in a wpf window
回答2:
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
回答3:
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
来源:https://stackoverflow.com/questions/47599712/c-sharp-wpf-maximized-windows-position