最大化2监视器上的WPF窗口

喜欢标题我希望我的WPF在2台显示器上最大化(现在我的电脑有2台显示器)。 我设置

this.Width = System.Windows.Forms.Screen.AllScreens[0].Bounds.Width + System.Windows.Forms.Screen.AllScreens[1].Bounds.Width 

但它不完整,像图像一样间隔。 我希望用户单击最大化按钮然后窗口最大化2监视器。 有人知道吗? 谢谢你们!

p / s:抱歉我的英语不好。

您需要找到左,上,高,宽的点并直接设置到要最大化的窗口

 MonitorFromPoint MonitorFromRect MonitorFromWindow 

为了完整起见,我首先介绍了如何在指定的监视器上最大化窗口的天真方法。

 void Maximize(HWND hWnd, HMONITOR hMonitor) { // access monitor info MONITORINFO monitorInfo = { sizeof(MONITORINFO) }; GetMonitorInfo(hMonitor, &monitorInfo); // restore window to normal size if it is not yet ShowWindow(hWnd, SW_RESTORE); // move window to the monitor SetWindowPos(hWnd, nullptr, monitorInfo.rcMonitor.left, monitorInfo.rcMonitor.top, 0, 0, SWP_NOZORDER | SWP_NOSIZE); // maximize window ShowWindow(hWnd, SW_MAXIMIZE); } 

如果使用相同的输入窗口和相同的目标监视器多次调用函数,则闪烁更明显。 当然,有人可以争辩说,有可能获得窗口的当前状态并避免调用函数(或者如果检测到状态已经正确,则在函数中进行一些早期返回)。 然而,这种处理只会增加function的复杂性。 我在想是不可能只是隐藏窗口并在后台以某种方式恢复状态,并且只在最后显示窗口。 但是尝试注入一些SetRender(hWnd,FALSE)或ShowWindow(SW_HIDE)调用只会使输出更糟糕。 经过一些测试,我发现更改最大化/正常窗口只会改变窗口样式中的一位(WS_MAXIMIZE),最后通过这些信息,我得到了最终的好解决方案:

 void Maximize(HWND hWnd, HMONITOR hMonitor) { // access monitor info MONITORINFO monitorInfo = { sizeof(MONITORINFO) }; GetMonitorInfo(hMonitor, &monitorInfo); const LONG currStyles = GetWindowLong(hWnd, GWL_STYLE); SetWindowLong(hWnd, GWL_STYLE, currStyles | WS_MAXIMIZE); const auto rc = monitorInfo.rcMonitor; SetWindowPos(&CWnd::wndTop, rc.left, rc.top, rc.Width(), rc.Height(), 0); } 

就是这样。 该function按预期工作,如果甚至多次调用,则没有闪烁。 通过从窗口样式中删除WS_MAXIMIZE并使用正确的矩形信息调用SetWindowPos,可以轻松更改该函数以将窗口恢复到正常大小。

  [DllImport("User32.dll")] private static extern IntPtr MonitorFromPoint([In]System.Drawing.Point pt, [In]uint dwFlags); //https://msdn.microsoft.com/en-us/library/windows/desktop/dn280510(v=vs.85).aspx [DllImport("Shcore.dll")] private static extern IntPtr GetDpiForMonitor([In]IntPtr hmonitor, [In]DpiType dpiType, [Out]out uint dpiX, [Out]out uint dpiY); public enum DpiType { Effective = 0, Angular = 1, Raw = 2, } ///  /// POINT aka POINTAPI ///  [StructLayout(LayoutKind.Sequential)] public struct POINT { ///  /// x coordinate of point. ///  public int x; ///  /// y coordinate of point. ///  public int y; ///  /// Construct a point of coordinates (x,y). ///  public POINT(int x, int y) { this.x = x; this.y = y; } } [StructLayout(LayoutKind.Sequential)] public struct MINMAXINFO { public POINT ptReserved; public POINT ptMaxSize; public POINT ptMaxPosition; public POINT ptMinTrackSize; public POINT ptMaxTrackSize; }; ///  ///  [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] public class MONITORINFO { ///  ///  public int cbSize = Marshal.SizeOf(typeof(MONITORINFO)); ///  ///  public RECT rcMonitor = new RECT(); ///  ///  public RECT rcWork = new RECT(); ///  ///  public int dwFlags = 0; } ///  Win32  [StructLayout(LayoutKind.Sequential, Pack = 0)] public struct RECT { ///  Win32  public int left; ///  Win32  public int top; ///  Win32  public int right; ///  Win32  public int bottom; ///  Win32  public static readonly RECT Empty = new RECT(); ///  Win32  public int Width { get { return Math.Abs(right - left); } // Abs needed for BIDI OS } ///  Win32  public int Height { get { return bottom - top; } } ///  Win32  public RECT(int left, int top, int right, int bottom) { this.left = left; this.top = top; this.right = right; this.bottom = bottom; } ///  Win32  public RECT(RECT rcSrc) { this.left = rcSrc.left; this.top = rcSrc.top; this.right = rcSrc.right; this.bottom = rcSrc.bottom; } ///  Win32  public bool IsEmpty { get { // BUGBUG : On Bidi OS (hebrew arabic) left > right return left >= right || top >= bottom; } } ///  Return a user friendly representation of this struct  public override string ToString() { if (this == RECT.Empty) { return "RECT {Empty}"; } return "RECT { left : " + left + " / top : " + top + " / right : " + right + " / bottom : " + bottom + " }"; } ///  Determine if 2 RECT are equal (deep compare)  public override bool Equals(object obj) { if (!(obj is Rect)) { return false; } return (this == (RECT)obj); } /// Return the HashCode for this struct (not garanteed to be unique) public override int GetHashCode() { return left.GetHashCode() + top.GetHashCode() + right.GetHashCode() + bottom.GetHashCode(); } ///  Determine if 2 RECT are equal (deep compare) 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); } ///  Determine if 2 RECT are different(deep compare) public static bool operator !=(RECT rect1, RECT rect2) { return !(rect1 == rect2); } } [DllImport("user32")] internal static extern bool GetMonitorInfo(IntPtr hMonitor, MONITORINFO lpmi); [DllImport("User32")] internal static extern IntPtr MonitorFromWindow(IntPtr handle, int flags); 

链接: – http://msdn.microsoft.com/en-us/library/windows/desktop/dd145071%28v=vs.85%29.aspx http://blogs.msdn.com/b/oldnewthing/archive/ 2010/04/12 / 9994016.aspx

我希望用户单击最大化按钮然后窗口最大化2监视器。

当用户单击最大化按钮时,将其放入。

 if (System.Windows.Forms.Screen.AllScreens.Length > 1) { System.Drawing.Rectangle entireSize = System.Drawing.Rectangle.Empty; foreach (System.Windows.Forms.Screen s in System.Windows.Forms.Screen.AllScreens) entireSize = System.Drawing.Rectangle.Union(entireSize, s.Bounds); //this.WindowState = NORMAL // SET Window State to Normal. this.Width = entireSize.Width; this.Height = entireSize.Height; this.Left = entireSize.Left; this.Top = entireSize.Top; }