2010-04-13 74 views
5

我在应用程序启动时恢复Window的坐标。在老式的Windows窗体中,我使用了System.Windows.Forms.Screen集合。 WPF世界中有没有类似的东西?确保WPF窗口在屏幕边界内

我注意到,VirtualScreen*参数System.Windows.SystemParameters。然而,他们让我挂了,因为在显示器尺寸不一样的情况下,似乎无法检测到Window是否在界限内。

回答

4

System.Windows.Forms.Screen在WPF中工作得很好,所以我认为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 { Visual = this }; 

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