How to center a WPF app on screen?

前端 未结 8 838
清歌不尽
清歌不尽 2021-01-30 09:53

I want to center my WPF app on startup on the primary screen. I know I have to set myWindow.Left and myWindow.Top, but where do I get the values?

I found System.Wi

8条回答
  •  太阳男子
    2021-01-30 10:29

    You can still use the Screen class from a WPF app. You just need to reference the System.Windows.Forms assembly from your application. Once you've done that, (and referenced System.Drawing for the example below):

    Rectangle workingArea = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea;
    

    ...works just fine.

    Have you considered setting your main window property WindowStartupLocation to CenterScreen?

提交回复
热议问题