Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
确保WPF窗口在屏幕边界内_Wpf - Fatal编程技术网

确保WPF窗口在屏幕边界内

确保WPF窗口在屏幕边界内,wpf,Wpf,我在应用程序启动时恢复窗口的坐标。在好的旧Windows窗体中,我使用了System.Windows.Forms.Screencollection。WPF世界里有什么相似的东西吗 我确实注意到System.Windows.SystemParameters中的PrimaryScreen*,VirtualScreen*参数。然而,他们让我绞尽脑汁,因为在监视器大小不同的情况下,似乎无法检测窗口是否在边界内。System.Windows.Forms.Screen在WPF中工作得非常好,所以我认为WPF

我在应用程序启动时恢复窗口的坐标。在好的旧Windows窗体中,我使用了
System.Windows.Forms.Screen
collection。WPF世界里有什么相似的东西吗


我确实注意到
System.Windows.SystemParameters
中的
PrimaryScreen*
VirtualScreen*
参数。然而,他们让我绞尽脑汁,因为在监视器大小不同的情况下,似乎无法检测窗口是否在边界内。

System.Windows.Forms.Screen
在WPF中工作得非常好,所以我认为WPF的设计者认为用WPF特定的版本取代它没有任何优势

当然,你必须进行坐标变换。下面是一个简单的类来进行转换:

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;
  }
}
用法示例:

var converter = new ScreenBoundsConverter(this);

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