WPF – Window Top and Left values are not updated correctly when maximizing a Window in .NET 4

WPF –  Window Top and Left values are not updated correctly when maximizing a Window in .NET 4

you can use this solution for update Window Top and Left values when maximizing a Window.

 

  private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
        {

         
            // if window Maximized set Left, top based on screen.WorkingArea Left, Top
            if (WindowState == WindowState.Maximized)
            {
                //get mouse position
                System.Windows.Point pt = Mouse.GetPosition(this);
                //get postion on screen
                Point pointToScreen = PointToScreen(pt);
                //get all screen
                System.Windows.Forms.Screen[] screens = System.Windows.Forms.Screen.AllScreens;
                //loop all screen
                foreach (System.Windows.Forms.Screen screen in screens)
                {
                    System.Drawing.Point formTopLeft = new System.Drawing.Point((int)pointToScreen.X, (int)pointToScreen.Y);
                    //check pointToScreen is on a screen
                    if (screen.WorkingArea.Contains(formTopLeft))
                    {
                        //set mainwindow Left, top based on screen.WorkingArea Left, Top
                        this.Left = screen.WorkingArea.Left;
                        this.Top = screen.WorkingArea.Top;
                    }
                }
            }
}

Leave a Reply

Your email address will not be published. Required fields are marked *