131

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.Windows.Forms.Screen.PrimaryScreen, which is apparently not WPF. Is there a WPF alternative that gives me the screen resolution or something like that?

animuson
  • 53,861
  • 28
  • 137
  • 147
Marcel B
  • 3,624
  • 2
  • 27
  • 39

9 Answers9

195

xaml

<Window ... WindowStartupLocation="CenterScreen">...
Ian Kemp
  • 28,293
  • 19
  • 112
  • 138
Pratik Deoghare
  • 35,497
  • 30
  • 100
  • 146
165

Put this in your window constructor

WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;

.NET FrameworkSupported in: 4, 3.5, 3.0

.NET Framework Client ProfileSupported in: 4, 3.5 SP1

Indy9000
  • 8,651
  • 2
  • 32
  • 37
  • 2
    Worked for me (.NET 4) and I also like `WindowStartupLocation.CenterOwner` for some child windows – Stonetip Jan 11 '12 at 18:38
  • 1
    The default is `Manual`. Ref: https://msdn.microsoft.com/en-us/library/system.windows.window.windowstartuplocation%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396 – CJBS Sep 21 '17 at 18:08
49

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?

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
8

You don't need to reference the System.Windows.Forms assembly from your application. Instead, you can use System.Windows.SystemParameters.WorkArea. This is equivalent to the System.Windows.Forms.Screen.PrimaryScreen.WorkingArea!

Mehdi
  • 2,194
  • 2
  • 25
  • 39
8

What about the SystemParameters class in PresentationFramework? It has a WorkArea property that seems to be what you are looking for.

But, why won't setting the Window.WindowStartupLocation work? CenterScreen is one of the enum values. Do you have to tweak the centering?

Eddie Butt
  • 493
  • 3
  • 4
  • Great find :) The problem with center screen for me at least is, my Log in window is small and if user is clickign around while app is opening it often gets unnoticed and goes into the background. But if i can open it on the primary display in the center it works fine. note: most users have 4+ screens – Michal Ciechan Apr 27 '10 at 16:20
4
var window = new MyWindow();

for center of the screen use:

window.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;

for center of the parent window use:

window.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner;
Artem
  • 149
  • 1
  • 2
3

I prefer to put it in the WPF code.

In [WindowName].xaml file:

<Window x:Class=...
...
WindowStartupLocation ="CenterScreen">
David Buck
  • 3,752
  • 35
  • 31
  • 35
2

There is no WPF equivalent. System.Windows.Forms.Screen is still part of the .NET framework and can be used from WPF though.

See this question for more details, but you can use the calls relating to screens by using the WindowInteropHelper class to wrap your WPF control.

Community
  • 1
  • 1
Jeff Yates
  • 61,417
  • 20
  • 137
  • 189
-1

You have property in window WindowStartupLocation.

Choose CenterScreen.

Please refer to the below image for more info. enter image description here

jimas13
  • 597
  • 5
  • 19