How to center a WPF app on screen?

C#WpfScreen Positioning

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

xaml

<Window ... WindowStartupLocation="CenterScreen">...

Solution 2 - C#

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

Solution 3 - C#

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?

Solution 4 - C#

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?

Solution 5 - C#

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!

Solution 6 - C#

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;

Solution 7 - C#

I prefer to put it in the WPF code.

In [WindowName].xaml file:

<Window x:Class=...
...
WindowStartupLocation ="CenterScreen">

Solution 8 - C#

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMarcel BView Question on Stackoverflow
Solution 1 - C#Pratik DeoghareView Answer on Stackoverflow
Solution 2 - C#Indy9000View Answer on Stackoverflow
Solution 3 - C#Michael PetrottaView Answer on Stackoverflow
Solution 4 - C#Eddie ButtView Answer on Stackoverflow
Solution 5 - C#MehdiView Answer on Stackoverflow
Solution 6 - C#ArtemView Answer on Stackoverflow
Solution 7 - C#Jason RobinsonView Answer on Stackoverflow
Solution 8 - C#Jeff YatesView Answer on Stackoverflow