tags:

views:

27

answers:

1

I restore coordinates of Window on application startup. In good-old-Windows-Forms I used System.Windows.Forms.Screen collection. Is there anything similar in WPF world?

I did notice PrimaryScreen*, VirtualScreen* parameters in System.Windows.SystemParameters. However they left me hanging since it seems to be impossible to detect whether Window is inside bounds in cases when monitors are not same size.

+1  A: 

System.Windows.Forms.Screen works perfectly well within WPF, so I think the designers of WPF saw no advantage in replacing it with a WPF-specific version.

You'll have to do a coordinate transformation of course. Here's an easy class to do the conversion:

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;
  }
}

Example usage:

var converter = new ScreenBoundsConverter { Visual = this };

foreach(var screen in System.Windows.Forms.Screen.AllScreens)
{
  Rect bounds = converter.ConvertBounds(screen.Bounds);
  ...
}
Ray Burns