How to find out which monitor is in the WPF window?

In a C # application, how can I find out if there is a WPF window on the main monitor or another monitor?

+37
c # wpf
Mar 17 '10 at 20:30
source share
6 answers

If the window is maximized, you cannot rely on window.Left or window.Top at all, since they can be coordinates before they are maximized. But you can do this in all cases:

var screen = System.Windows.Forms.Screen.FromHandle( new System.Windows.Interop.WindowInteropHelper(window).Handle); 
+23
Jan 29 '12 at 20:59
source share

Other answers so far do not address part of the WPF question. Here is my trick.

WPF does not seem to disclose detailed information about the screen, as shown in the Windows Forms Screen class mentioned in other answers.

However, you can use the WinForms screen class in your WPF program:

Add links to System.Windows.Forms and System.Drawing

 var screen = System.Windows.Forms.Screen.FromRectangle( new System.Drawing.Rectangle( (int)myWindow.Left, (int)myWindow.Top, (int)myWindow.Width, (int)myWindow.Height)); 

Please note that if you are a nitpicker, you may have noticed that this code may have the right and bottom coordinates on one pixel in the case of converting double to int. But since you are nitpicker, you will be more than happy to fix my code :-)

+8
Aug 29 2018-11-11T00:
source share

To do this, you need to use some proprietary methods.

https://msdn.microsoft.com/en-us/library/windows/desktop/dd145064(v=vs.85).aspx

 internal static class NativeMethods { public const Int32 MONITOR_DEFAULTTOPRIMARY = 0x00000001; public const Int32 MONITOR_DEFAULTTONEAREST = 0x00000002; [DllImport( "user32.dll" )] public static extern IntPtr MonitorFromWindow( IntPtr handle, Int32 flags ); } 

Then you simply check which monitor your window is in, and which is the main one. Like this:

  var hwnd = new WindowInteropHelper( this ).EnsureHandle(); var currentMonitor = NativeMethods.MonitorFromWindow( hwnd, NativeMethods.MONITOR_DEFAULTTONEAREST ); var primaryMonitor = NativeMethods.MonitorFromWindow( IntPtr.Zero, NativeMethods.MONITOR_DEFAULTTOPRIMARY ); var isInPrimary = currentMonitor == primaryMonitor; 
+2
Apr 28 '16 at 15:26
source share

You can use the Screen.FromControl method to get the current screen for the current form as follows:

 Screen screen = Screen.FromControl(this); 

You can then check Screen.Primary to see if the current screen is the main one.

0
Mar 17 '10 at 20:31
source share

Check out How to find out on which screen the application in C # works
In addition, running the application in a dual-screen environment has an interesting solution:

 bool onPrimary = this.Bounds.IntersectsWith(Screen.PrimaryScreen.Bounds); 

where "this" is the main form of your application.

0
Mar 17 '10 at 20:40
source share
 public static bool IsOnPrimary(Window myWindow) { var rect = myWindow.RestoreBounds; Rectangle myWindowBounds= new Rectangle((int)rect.X, (int)rect.Y, (int)rect.Width, (int)rect.Height); return myWindowBounds.IntersectsWith(WinForms.Screen.PrimaryScreen.Bounds); /* Where using System.Drawing; using System.Windows; using WinForms = System.Windows.Forms; */ } 
0
Sep 18 '11 at 4:28
source share



All Articles