我知道我可以通过使用来获得主屏幕的大小
System.Windows.SystemParameters.PrimaryScreenWidth;
System.Windows.SystemParameters.PrimaryScreenHeight;
Run Code Online (Sandbox Code Playgroud)
但是如何获得当前屏幕的大小?(多屏用户并不总是使用主屏幕,并非所有屏幕都使用相同的分辨率,对吧?)
能够从XAML访问大小会很好,但是从代码(C#)这样做就足够了.
我正在开发一个应用程序,它记住用户关于表单最后位于屏幕上的位置的首选项.在某些情况下,用户将其放在辅助屏幕上,然后在没有第二个屏幕的情况下稍后启动应用程序(有时窗体显示在屏幕外).其他时候,用户将改变其分辨率,从而产生类似的效果.
我希望在Form_Shown事件处理程序中执行此检查.基本上我想确定表格是否完全不在屏幕上,所以我可以重新定位它.
有什么建议?
有没有办法确定在任何桌面连接的监视器中当前是否可以看到打开的WPF窗口?通过可见我的意思是窗口的边界矩形与任何监视器的桌面矩形相交.
我需要此功能来确定是否需要重新定位窗口,因为监视器配置(工作区域边界,监视器计数)在应用程序重新启动(保存窗口位置)之间发生了变化.
我已经提出了下面的代码,它似乎工作,但它有几个问题:
您是否知道解决上述部分或全部3个问题的解决方案?
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Media;
internal static class Desktop
{
private static Size dpiFactor = new Size(1.0, 1.0);
private static bool isInitialized;
public static IEnumerable<Rect> WorkingAreas
{
get
{
return
Screen.AllScreens.Select(
screen =>
new Rect(
screen.WorkingArea.Left * dpiFactor.Width,
screen.WorkingArea.Top * dpiFactor.Height,
screen.WorkingArea.Width * dpiFactor.Width,
screen.WorkingArea.Height * dpiFactor.Height));
}
}
public static void TryInitialize(Visual visual)
{
if (isInitialized)
{
return;
}
var ps = PresentationSource.FromVisual(visual);
if (ps == null) …Run Code Online (Sandbox Code Playgroud)