tags:

views:

2947

answers:

3

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?

+9  A: 

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
oh ... i didn't know that one. Thanks.
Marcel Benthin
I was looking for WindowStartupLocation; knew I had seen that before, very useful!
chocojosh
A: 

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.

Jeff Yates
+4  A: 

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
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
LnDCobra