How do you center your main window in WPF?

前端 未结 13 1345
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-28 07:07

I have a WPF application and I need to know how to center the wain window programatically (not in XAML).

I need to be able to do this both at startup and in response

相关标签:
13条回答
  • 2020-11-28 07:18

    As a basic solution, you can use the window's StartupLocation property, set it to one of the enum values defined in System.Windows.WindowStartupLocation enumeration, there is one for center of screen:

    _wpfWindow.StartupLocation = System.Windows.WindowStartupLocation.CenterScreen;
    

    Unfortunately it's not always quite so simple; you need to account for multiple monitors, taskbars, etc. The "CenterScreen" option opens the window in the center of the screen that has the mouse cursor. See this SO question for a lot of information, or reference the api.

    0 讨论(0)
  • 2020-11-28 07:21

    Based on @Wild_A answer I just subscribed to the SizeChanged event, and added this event handler:

    private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
    {
        try
        {
            Rect workArea = SystemParameters.WorkArea;
            this.Left = (workArea.Width - e.NewSize.Width) / 2 + workArea.Left;
            this.Top = (workArea.Height - e.NewSize.Height) / 2 + workArea.Top;
        }
        catch (Exception ex) { ... Handel exception; }
    }
    
    0 讨论(0)
  • 2020-11-28 07:24

    If you it to be maximized at once
    this.WindowState = System.Windows.WindowState.Maximized;

    0 讨论(0)
  • 2020-11-28 07:25

    Well, for startup time, you can set the startup location:

    window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
    

    Later, you'll need to query it. The information (at least for the primary screen) is available via SystemParameters.PrimaryScreenWidth/Height.

    0 讨论(0)
  • 2020-11-28 07:25

    What I am using in my app, it is working for multiple displays and for different DPI setting

        //center a window on chosen screen
        public static void CenterWindow(Window w, System.Windows.Forms.Screen screen = null)
        {
            if(screen == null)
                screen = System.Windows.Forms.Screen.PrimaryScreen;
    
            int screenW = screen.Bounds.Width;
            int screenH = screen.Bounds.Height;
            int screenTop = screen.Bounds.Top;
            int screenLeft = screen.Bounds.Left;
    
            w.Left = PixelsToPoints((int)(screenLeft + (screenW - PointsToPixels(w.Width, "X")) / 2), "X");
            w.Top = PixelsToPoints((int)(screenTop + (screenH - PointsToPixels(w.Height, "Y")) / 2), "Y");
        }
    
        public static double PixelsToPoints(int pixels, string direction)
        {
            if (direction == "X")
            {
                return pixels * SystemParameters.WorkArea.Width / System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width;
            }
            else
            {
                return pixels * SystemParameters.WorkArea.Height / System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height;
            }
        }
    
        public static double PointsToPixels(double wpfPoints, string direction)
        {
            if (direction == "X")
            {
                return wpfPoints * System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width / SystemParameters.WorkArea.Width;
            }
            else
            {
                return wpfPoints * System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height / SystemParameters.WorkArea.Height;
            }
        }
    
    0 讨论(0)
  • 2020-11-28 07:27

    In case you need to draw a window in an multiple screen environment. I've made a static class where the following method can be re-used:

    public static void PostitionWindowOnScreen(Window window, double horizontalShift = 0, double verticalShift = 0)
    {
        Screen screen = Screen.FromHandle(new System.Windows.Interop.WindowInteropHelper(window).Handle);
        window.Left = screen.Bounds.X + ((screen.Bounds.Width - window.ActualWidth) / 2) + horizontalShift;
        window.Top = screen.Bounds.Y + ((screen.Bounds.Height - window.ActualHeight) / 2) + verticalShift;        
    }
    

    In the constructor of the Window now just call the method:

    this.Loaded += (s, a) => Globals.PostitionWindowOnScreen(this, 0, 0)
    
    0 讨论(0)
提交回复
热议问题