【问题标题】:Maximize WPF window on 2 monitor在 2 个显示器上最大化 WPF 窗口
【发布时间】:2018-08-27 08:38:11
【问题描述】:

像标题一样,我希望我的 WPF 在 2 台显示器上最大化(现在我的电脑有 2 台显示器)。 我设置了

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

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

p/s:对不起,我的英语不好。

【问题讨论】:

  • 我认为这不是一个好主意。首先,边框厚度和显示器之间的物理间隙将使其对于任何文本或窗口中心的任何内容都具有不好的体验。此外,并非所有用户都将使用相同的分辨率/dpi 显示器,因此它在显示器之间看起来也会不一致,这通常是一种糟糕的体验。也许会产生另一个窗口,让用户决定把它放在哪里?
  • 您需要删除WindowState 属性。确保它没有设置为Maximized
  • @FurkanKambay 有很多跨显示器的应用程序。 微软 FSX; Prepar3D 和实用程序使 Ultramon 等任何应用程序都可以轻松实现这一点。 nVidia 轻松解决边框问题nvidia.custhelp.com/app/answers/detail/a_id/2669/~/…

标签: c# wpf maximize


【解决方案1】:

你需要找到点Left、Top、Height、Width并直接设置到你想要最大化的窗口

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);    
}

如果函数在相同的输入窗口和相同的目标监视器下多次调用,闪烁会更加明显。当然,有人可能会争辩说,可以获取窗口的当前状态并避免调用函数(或者如果检测到状态已经正确,则在函数中提前返回)。然而,这样的处理只会增加函数的复杂性。我在想是否不可能只是隐藏窗口并在后台以某种方式执行状态恢复,并且只在最后显示窗口。但是尝试注入一些 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);
}

就是这样。该函数按预期工作,即使多次调用,也没有闪烁。通过从窗口样式中删除 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,
        }

        /// <summary>
        /// POINT aka POINTAPI
        /// </summary>
        [StructLayout(LayoutKind.Sequential)]
        public struct POINT
        {
            /// <summary>
            /// x coordinate of point.
            /// </summary>
            public int x;
            /// <summary>
            /// y coordinate of point.
            /// </summary>
            public int y;

            /// <summary>
            /// Construct a point of coordinates (x,y).
            /// </summary>
            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;
        };

        /// <summary>
        /// </summary>
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        public class MONITORINFO
        {
            /// <summary>
            /// </summary>            
            public int cbSize = Marshal.SizeOf(typeof(MONITORINFO));

            /// <summary>
            /// </summary>            
            public RECT rcMonitor = new RECT();

            /// <summary>
            /// </summary>            
            public RECT rcWork = new RECT();

            /// <summary>
            /// </summary>            
            public int dwFlags = 0;
        }


        /// <summary> Win32 </summary>
        [StructLayout(LayoutKind.Sequential, Pack = 0)]
        public struct RECT
        {
            /// <summary> Win32 </summary>
            public int left;
            /// <summary> Win32 </summary>
            public int top;
            /// <summary> Win32 </summary>
            public int right;
            /// <summary> Win32 </summary>
            public int bottom;

            /// <summary> Win32 </summary>
            public static readonly RECT Empty = new RECT();

            /// <summary> Win32 </summary>
            public int Width
            {
                get { return Math.Abs(right - left); }  // Abs needed for BIDI OS
            }
            /// <summary> Win32 </summary>
            public int Height
            {
                get { return bottom - top; }
            }

            /// <summary> Win32 </summary>
            public RECT(int left, int top, int right, int bottom)
            {
                this.left = left;
                this.top = top;
                this.right = right;
                this.bottom = bottom;
            }


            /// <summary> Win32 </summary>
            public RECT(RECT rcSrc)
            {
                this.left = rcSrc.left;
                this.top = rcSrc.top;
                this.right = rcSrc.right;
                this.bottom = rcSrc.bottom;
            }

            /// <summary> Win32 </summary>
            public bool IsEmpty
            {
                get
                {
                    // BUGBUG : On Bidi OS (hebrew arabic) left > right
                    return left >= right || top >= bottom;
                }
            }
            /// <summary> Return a user friendly representation of this struct </summary>
            public override string ToString()
            {
                if (this == RECT.Empty) { return "RECT {Empty}"; }
                return "RECT { left : " + left + " / top : " + top + " / right : " + right + " / bottom : " + bottom + " }";
            }

            /// <summary> Determine if 2 RECT are equal (deep compare) </summary>
            public override bool Equals(object obj)
            {
                if (!(obj is Rect)) { return false; }
                return (this == (RECT)obj);
            }

            /// <summary>Return the HashCode for this struct (not garanteed to be unique)</summary>
            public override int GetHashCode()
            {
                return left.GetHashCode() + top.GetHashCode() + right.GetHashCode() + bottom.GetHashCode();
            }


            /// <summary> Determine if 2 RECT are equal (deep compare)</summary>
            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);
            }

            /// <summary> Determine if 2 RECT are different(deep compare)</summary>
            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】:

我希望用户点击最大化按钮然后窗口最大化 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;

}

【讨论】:

  • 但它并没有完全最大化。它仍然有间距。
猜你喜欢
  • 2012-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-24
  • 1970-01-01
  • 1970-01-01
  • 2012-02-29
  • 1970-01-01
相关资源
最近更新 更多