126

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
  • 52,378
  • 28
  • 138
  • 145
Marcel B
  • 3,514
  • 2
  • 25
  • 39

8 Answers8

184

xaml

<Window ... WindowStartupLocation="CenterScreen">...
Ian Kemp
  • 26,561
  • 17
  • 107
  • 129
Pratik Deoghare
  • 33,028
  • 29
  • 96
  • 145
159

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,493
  • 2
  • 29
  • 36
  • 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
  • 58,479
  • 27
  • 141
  • 176
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,073
  • 2
  • 24
  • 38
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
  • 483
  • 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,594
  • 33
  • 29
  • 34
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
  • 60,114
  • 20
  • 136
  • 187