Providing a WPF Window Inside Screen Borders

I restore the window coordinates when the application starts. In good-for-Windows forms, I used the System.Windows.Forms.Screen collection. Is there something similar in the WPF world?

I noticed the parameters PrimaryScreen* , VirtualScreen* in System.Windows.SystemParameters . However, they left me hanging, as it is impossible to detect if the Window is inside the borders in cases where the monitors are not the same size.

+5
wpf
Apr 13 2018-10-13T00:
source share
1 answer

System.Windows.Forms.Screen works fine in WPF, so I think that the WPF designers could not replace it with a WPF-specific version.

Of course, you will need to convert the coordinates. Here's an easy class to convert:

 public class ScreenBoundsConverter { private Matrix _transform; public ScreenBoundsConverter(Visual visual) { _transform = PresentationSource.FromVisual(visual).CompositionTarget.TransformFromDevice; } public Rect ConvertBounds(Rectangle bounds) { var result = new Rect(bounds.X, bounds.Y, bounds.Width, bounds.Height); result.Transform(_transform); return result; } } 

Usage example:

 var converter = new ScreenBoundsConverter { Visual = this }; foreach(var screen in System.Windows.Forms.Screen.AllScreens) { Rect bounds = converter.ConvertBounds(screen.Bounds); ... } 
+4
Apr 14 '10 at 16:44
source share
— -



All Articles