最大化当前屏幕上的 WPF 窗口
本文关键字:WPF 窗口 屏幕 最大化 | 更新日期: 2023-09-27 17:57:10
我有一个无窗口的 wpf 应用程序,每当我将窗口状态设置为最大化时,它都会在主显示器上最大化它。
我想做的是让它最大化应用程序正在运行的显示器。
所以知道我会怎么做吗?
我目前的代码只是
private void titleBarThumb_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
if (this.WindowState == System.Windows.WindowState.Normal)
{
this.WindowState = System.Windows.WindowState.Maximized;
}
else
{
this.WindowState = System.Windows.WindowState.Normal;
}
}
我已经在我的 MainWindow(第一个控件)构造函数中包含以下行:
Application.Current.MainWindow.WindowState = WindowState.Maximized;
由于任务栏,您应该使用用户工作区的大小:
this.Width=SystemParameters.WorkArea.Width;
this.Height=SystemParameters.WorkArea.Height;
您可以在视图的构造函数中使用它
我不确定这是否得到回答 - 我创建了一个示例应用程序
WindowStyle = WindowStyle.None;
我创建了一个按钮,并在点击处理程序上这样做-
WindowState = WindowState.Maximized
我连接了鼠标左按钮向下处理程序,以便窗口拖动移动-
this.MouseLeftButtonDown += new(MainWindow_MouseLeftButtonDown);
private void MainWindow_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
DragMove();
}
当我将窗口拖到第二台显示器并单击最大化按钮时,它在当前窗口中最大化,而不是启动窗口。我使用的是VS2010和.NET 4。让我知道这是否有帮助。
一个有 7 个赞成票的问题应该得到正确的答案。 :D
使用此窗口而不是普通窗口,然后最大化/最小化/规范化将自行处理。
using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
public partial class MyWindow : Window
{
public MyWindow ()
{
this.InitializeComponent();
this.SourceInitialized += this.OnSourceInitialized;
}
#endregion
#region Methods
private static IntPtr WindowProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
switch (msg)
{
case 0x0024:
WmGetMinMaxInfo(hwnd, lParam);
handled = true;
break;
}
return (IntPtr)0;
}
private static void WmGetMinMaxInfo(IntPtr hwnd, IntPtr lParam)
{
var mmi = (MINMAXINFO)Marshal.PtrToStructure(lParam, typeof(MINMAXINFO));
// Adjust the maximized size and position to fit the work area of the correct monitor
IntPtr monitor = MonitorFromWindow(hwnd, (int)MonitorFromWindowFlags.MONITOR_DEFAULTTONEAREST);
if (monitor != IntPtr.Zero)
{
var monitorInfo = new MONITORINFO();
GetMonitorInfo(monitor, monitorInfo);
RECT rcWorkArea = monitorInfo.rcWork;
RECT rcMonitorArea = monitorInfo.rcMonitor;
mmi.ptMaxPosition.x = Math.Abs(rcWorkArea.Left - rcMonitorArea.Left);
mmi.ptMaxPosition.y = Math.Abs(rcWorkArea.Top - rcMonitorArea.Top);
mmi.ptMaxSize.x = Math.Abs(rcWorkArea.Right - rcWorkArea.Left);
mmi.ptMaxSize.y = Math.Abs(rcWorkArea.Bottom - rcWorkArea.Top);
}
Marshal.StructureToPtr(mmi, lParam, true);
}
private void OnSourceInitialized(object sender, EventArgs e)
{
var window = sender as Window;
if (window != null)
{
IntPtr handle = (new WindowInteropHelper(window)).Handle;
HwndSource.FromHwnd(handle).AddHook(WindowProc);
}
}
}
DLL 导入和声明
[StructLayout(LayoutKind.Sequential)]
public struct MINMAXINFO
{
public POINT ptReserved;
public POINT ptMaxSize;
public POINT ptMaxPosition;
public POINT ptMinTrackSize;
public POINT ptMaxTrackSize;
} ;
public enum MonitorFromWindowFlags
{
MONITOR_DEFAULTTONEAREST = 0x00000002
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class MONITORINFO
{
public int cbSize = Marshal.SizeOf(typeof(MONITORINFO));
public RECT rcMonitor;
public RECT rcWork;
public int dwFlags;
}
[StructLayout(LayoutKind.Sequential, Pack = 0)]
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
public static readonly RECT Empty;
public int Width
{
get
{
return Math.Abs(this.Right - this.Left);
} // Abs needed for BIDI OS
}
public int Height
{
get
{
return this.Bottom - this.Top;
}
}
public RECT(int left, int top, int right, int bottom)
{
this.Left = left;
this.Top = top;
this.Right = right;
this.Bottom = bottom;
}
public RECT(RECT rcSrc)
{
this.Left = rcSrc.Left;
this.Top = rcSrc.Top;
this.Right = rcSrc.Right;
this.Bottom = rcSrc.Bottom;
}
public bool IsEmpty
{
get
{
// BUGBUG : On Bidi OS (hebrew arabic) left > right
return this.Left >= this.Right || this.Top >= this.Bottom;
}
}
public override string ToString()
{
if (this == Empty)
{
return "RECT {Empty}";
}
return "RECT { left : " + this.Left + " / top : " + this.Top + " / right : " + this.Right + " / bottom : " +
this.Bottom + " }";
}
public override bool Equals(object obj)
{
if (!(obj is RECT))
{
return false;
}
return (this == (RECT)obj);
}
public override int GetHashCode()
{
return this.Left.GetHashCode() + this.Top.GetHashCode() + this.Right.GetHashCode() +
this.Bottom.GetHashCode();
}
public static bool operator ==(RECT rect1, RECT rect2)
{
return (rect1.Left == rect2.Left && rect1.Top == rect2.Top && rect1.Right == rect2.Right &&
rect1.Bottom == rect2.Bottom);
}
public static bool operator !=(RECT rect1, RECT rect2)
{
return !(rect1 == rect2);
}
}
[DllImport("user32.dll", SetLastError = true)]
public static extern bool GetMonitorInfo(IntPtr hMonitor, MONITORINFO lpmi);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr MonitorFromWindow(IntPtr handle, int flags);
在加载窗口之前,我们无法最大化窗口。因此,通过挂接 fullScreenWindow 的 Loaded 事件并按以下方式处理该事件:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
WindowState = WindowState.Maximized;
}
通过这样做,我在辅助屏幕中最大化了我的应用程序
在主窗口顶部添加以下内容:
using Screen = System.Windows.Forms.Screen;
在最大化处理程序中添加以下内容:
private void AdjustWindowSize()
{
if (this.WindowState == WindowState.Maximized)
{
this.WindowState = WindowState.Normal;
}
else
{
System.Drawing.Rectangle r = Screen.GetWorkingArea(new System.Drawing.Point((int)this.Left, (int)this.Top));
this.MaxWidth = r.Width;
this.MaxHeight = r.Height;
this.WindowState = WindowState.Maximized;
}
}
来吧!
C# 应用程序首先在主显示器上启动,除非它被移动,否则您的代码将正常工作。但是,如果您的 wpf 应用程序将移动到另一个显示器,则可以记录新位置并将其存储在本地配置文件中。但是,你的应用将没有边框或任何其他本机控件,因此你还必须实现移动位。当窗口移动时,您将能够使用系统参数捕获显示索引。
祝你好运
我刚刚遇到了同样的问题。 就我而言,事实证明,当我完成弹出窗口时,我隐藏了弹出窗口。 因此,如果我下次调用它并要求它最大化,它会在原始屏幕上执行此操作。 一旦我开始关闭它,它就开始在正确的屏幕上最大化。
尝试:
Window.WindowState = WindowState.Normal;