我有两个显示器:
屏幕1:1920x1080的辅助屏幕
屏幕2:1600x900的主屏幕
屏幕1 比屏幕 2 大。
当我在屏幕 2 中打开我的应用程序,然后将其从屏幕 2 移动到屏幕 1 并尝试最小化然后最大化我的应用程序时,最大大小由屏幕 2 占用,而不是当前显示器大小(它看起来没有最大化与显示器大小相关(
我如何编辑我的代码,以便最大化和最小化以在应用程序现在存在的位置采用屏幕分辨率,而不是根据主显示器采用它?
我正在使用此线程中的代码来调整大小: https://blogs.msdn.microsoft.com/llobo/2006/08/01/maximizing-window-with-windowstylenone-considering-taskbar/
这与这个回复相同: https://stackoverflow.com/a/6315427/5825468
谢谢
要获取当前屏幕的大小,您可能必须使用Windows表单方法FromHandle,例如
using System.Windows;
using System.Windows.Forms;
using System.Windows.Interop;
namespace TestApp
{
public partial class MainWindow : Window
{
public Screen GetCurrentScreen(Window window)
{
return Screen.FromHandle(new WindowInteropHelper(window).Handle);
}
public MainWindow()
{
InitializeComponent();
var screen = GetCurrentScreen(this);
var height = screen.Bounds.Height;
var width = screen.Bounds.Width;
// ...
}
}
}
另一种选择是订阅窗口的 LocationChanged 事件,以了解您的应用程序窗口何时移动到辅助屏幕,请参阅 StepUp 的这个答案。
首先订阅状态更改事件:
public MainWindow()
{
InitializeComponent();
this.StateChanged += MainWindow_StateChanged;
}
然后仅在最小化后,恢复虚拟/主屏幕尺寸,如下所示: 由于我不知道您的默认应用程序大小是多少,如果它不是全屏,最后您可以通过系统参数屏幕尺寸轻松恢复它们,只需一点逻辑。
private void MainWindow_StateChanged(object sender, EventArgs e)
{
if (this.WindowState != WindowState.Minimized)
{
this.Width = SystemParameters.PrimaryScreenWidth;
this.Height = SystemParameters.PrimaryScreenHeight;
//this.Width = SystemParameters.VirtualScreenWidth;
//this.Height =SystemParameters.VirtualScreenHeight;
//this.Width = SystemParameters.WorkArea.Width
//this.Height =SystemParameters.WorkArea.Height;
}
}
应该处理这种情况,系统参数公开以下数据:
主屏幕高度/宽度:
屏幕的高度/宽度。
虚拟屏幕宽度/高度:
虚拟屏幕的宽度/高度。
工作区域:
接收工作区坐标的 RECT 结构,表示 作为虚拟屏幕坐标。