I have two monitors:
我有两台显示器:
Screen 1: which is secondary screen with 1920x1080
屏幕1:这是1920x1080的二级屏幕
Screen 2: which is primary screen with 1600x900
屏幕2:这是1600x900的主屏幕
Screen 1 is larger then Screen 2.
屏幕1大于屏幕2。
When I open my application in Screen 2, and then move it from screen 2 to screen 1 and try to minimize and then maximize my application, the maximum size is taken by screen 2, and not by current monitor size(it doesn't appear maximized related to monitor size)
当我在屏幕2中打开我的应用程序,然后将其从屏幕2移动到屏幕1并尝试最小化然后最大化我的应用程序时,屏幕2采用最大尺寸,而不是当前显示器尺寸(它不会出现)与监视器大小相关的最大化)
How I can edit my code so in maximize and minimize to take the screen resolution where the application exist now instead of taking it depending on the primary monitor?
我如何编辑我的代码以便最大化和最小化以获取应用程序现在存在的屏幕分辨率而不是取决于主监视器?
I am using the code in this thread for the matter of resizing: https://blogs.msdn.microsoft.com/llobo/2006/08/01/maximizing-window-with-windowstylenone-considering-taskbar/
我正在使用此线程中的代码来调整大小:https://blogs.msdn.microsoft.com/llobo/2006/08/01/maximizing-window-with-windowstylenone-considering-taskbar/
Which is same as this reply: https://*.com/a/6315427/5825468
与此回复相同:https://*.com/a/6315427/5825468
Thanks
谢谢
2 个解决方案
#1
4
To get the size of the current screen you probably have to use the windows forms method FromHandle like
要获得当前屏幕的大小,您可能必须使用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;
// ...
}
}
}
Another option would be to subscribe to the LocationChanged event of the window to know when your application window has been moved to a secondary screen, see this answer of StepUp.
另一种选择是订阅窗口的LocationChanged事件以了解您的应用程序窗口何时被移动到辅助屏幕,请参阅StepUp的这个答案。
#2
3
first subscribe to the state changed event:
首先订阅状态更改事件:
public MainWindow()
{
InitializeComponent();
this.StateChanged += MainWindow_StateChanged;
}
and then only after minimize, restore the virtual/primary screen sizes as follow: and since I don't know what is your default app size and if it's not a full screen, at the end you can restore them easily by the SystemParameters screen size, with a little logic.
然后仅在最小化之后,恢复虚拟/主屏幕大小,如下所示:由于我不知道您的默认应用程序大小是什么,如果它不是全屏,最后您可以通过SystemParameters屏幕大小轻松恢复它们,有点逻辑。
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;
}
}
that should handle the situation, the SystemParameters expose the following data:
应该处理这种情况,SystemParameters公开以下数据:
PrimeryScreenHeight /宽度:
The height/Width of the screen.
屏幕的高度/宽度。
VirtualScreenWidth /高度:
The width/height of the virtual screen.
虚拟屏幕的宽度/高度。
工作区域:
A RECT structure that receives the work area coordinates, expressed as virtual screen coordinates.
接收工作区坐标的RECT结构,表示为虚拟屏幕坐标。
#1
4
To get the size of the current screen you probably have to use the windows forms method FromHandle like
要获得当前屏幕的大小,您可能必须使用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;
// ...
}
}
}
Another option would be to subscribe to the LocationChanged event of the window to know when your application window has been moved to a secondary screen, see this answer of StepUp.
另一种选择是订阅窗口的LocationChanged事件以了解您的应用程序窗口何时被移动到辅助屏幕,请参阅StepUp的这个答案。
#2
3
first subscribe to the state changed event:
首先订阅状态更改事件:
public MainWindow()
{
InitializeComponent();
this.StateChanged += MainWindow_StateChanged;
}
and then only after minimize, restore the virtual/primary screen sizes as follow: and since I don't know what is your default app size and if it's not a full screen, at the end you can restore them easily by the SystemParameters screen size, with a little logic.
然后仅在最小化之后,恢复虚拟/主屏幕大小,如下所示:由于我不知道您的默认应用程序大小是什么,如果它不是全屏,最后您可以通过SystemParameters屏幕大小轻松恢复它们,有点逻辑。
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;
}
}
that should handle the situation, the SystemParameters expose the following data:
应该处理这种情况,SystemParameters公开以下数据:
PrimeryScreenHeight /宽度:
The height/Width of the screen.
屏幕的高度/宽度。
VirtualScreenWidth /高度:
The width/height of the virtual screen.
虚拟屏幕的宽度/高度。
工作区域:
A RECT structure that receives the work area coordinates, expressed as virtual screen coordinates.
接收工作区坐标的RECT结构,表示为虚拟屏幕坐标。