【问题标题】:Maximize WPF Window on the current screen在当前屏幕上最大化 WPF 窗口
【发布时间】:2011-05-20 11:39:57
【问题描述】:

我有一个无窗口的 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;
            }
        }

【问题讨论】:

  • 你说“无窗”是指无边界吗?
  • 是的,它是无边界的,基本上我的意思是 Window.WindowStyle 没有
  • 当我最大化一个窗口时,它总是在它所在的屏幕上最大化。当你给窗口一个边框时它会起作用吗?
  • WindowState.Maximized工作正常,但是当我使用WindowState.Normal时,窗口无法恢复。

标签: c# .net wpf windows


【解决方案1】:

我在我的 MainWindow(第一个控件)构造函数中包含了这一行:

Application.Current.MainWindow.WindowState = WindowState.Maximized;

【讨论】:

  • 我发誓,这继续表明,在 WPF 中,最简单的答案有时是最难找到的!感谢这个例子。
【解决方案2】:

由于任务栏,您应该使用用户工作区的大小:

this.Width=SystemParameters.WorkArea.Width;
this.Height=SystemParameters.WorkArea.Height;

您可以在视图的构造函数中使用它

【讨论】:

  • 如果要设置在左上角添加这个(this.Top = SystemParameters.WorkArea.Top; this.Left = SystemParameters.WorkArea.Left;)
  • 这仅适用于主屏幕。其他屏幕无法通过 SystemParameters 访问,并且可能具有不同的尺寸和旋转。
【解决方案3】:

我不确定这是否得到解答-我使用

创建了一个示例应用程序
WindowStyle = WindowStyle.None;

我创建了一个按钮并在点击处理程序上做了这个-

WindowState = WindowState.Maximized

我为窗口连接了 MouseLeftButtonDown 处理程序以拖动移动-

this.MouseLeftButtonDown += new(MainWindow_MouseLeftButtonDown);

private void MainWindow_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
   DragMove();
}

当我将窗口拖到第二台显示器并单击最大化按钮时,它在当前窗口中最大化,而不是在启动窗口中。我使用的是 VS2010 和 .NET 4。如果这有帮助,请告诉我。

【讨论】:

    【解决方案4】:

    一个有 7 票赞成的问题值得 正确答案。 :D

    使用此窗口代替普通窗口,然后 Maxmize/Minimize/normalize 将自行处理。

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

    【讨论】:

    • 我遵循了您的代码,但得到了一些奇怪的功能。你介意看看我的question吗?
    • 在辅助监视器上最大化时,如果它大于主监视器,则会失败。
    • 具体来说,来自 Murray 的链接。我认为这是完成它的部分(如果我错了,请纠正我)github.com/MahApps/MahApps.Metro/blob/…
    【解决方案5】:

    看看这个问题和答案: How to center a WPF app on screen?

    您可以使用 Windows.Forms.Screen 中描述的函数来获取当前屏幕。 然后也许将 Windows 的 StartupLocation 设置为此屏幕(在您已经做的最大化之前)可能会达到您想要的效果,但老实说,我没有花时间亲自尝试。

    【讨论】:

      【解决方案6】:

      我问了一个类似的问题,您可能会觉得有帮助。 How Can I Make a WPF Window Maximized on the Screen with the Mouse Cursor?

      【讨论】:

        【解决方案7】:

        在加载之前,我们无法最大化窗口。因此,通过挂钩 fullScreenWindow 的 Loaded 事件并按照以下方式处理事件:

        private void Window_Loaded(object sender, RoutedEventArgs e) 
        {
            WindowState = WindowState.Maximized;
        }
        

        【讨论】:

        • 这不是真的!您可以毫无问题地实施 ctor { this.InitializeComponent(); this.WindowState = WindowState.Maximized; } 这很好用!
        【解决方案8】:

        通过这样做,我的应用程序在辅助屏幕中最大化

        在主窗口顶部添加:

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

        我们开始吧!

        【讨论】:

          【解决方案9】:

          c# 应用程序首先在主显示器上启动,除非它被移动,否则您的代码将起作用。但是,如果您的 wpf 应用程序将被移动到另一个显示新位置,则可以记录并存储在本地配置文件中。但是,您的应用程序将没有边框或任何其他本机控件,因此您还必须实现移动位。当您的窗口移动时,您将能够使用 SystemParameters 捕获显示索引。

          祝你好运

          【讨论】:

            【解决方案10】:

            我刚刚遇到了同样的问题。就我而言,事实证明,当我完成它时,我正在隐藏我的弹出窗口。因此,如果我下次调用它并要求它最大化,它会在原始屏幕上执行。一旦我开始关闭它,它就开始在正确的屏幕上最大化。

            【讨论】:

              【解决方案11】:

              试试:

              Window.WindowState = WindowState.Normal;

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2016-08-12
                • 2017-11-24
                • 1970-01-01
                • 2011-03-08
                • 1970-01-01
                • 2010-12-24
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多