在当前屏幕上最大化WPF窗口

问题描述 投票:42回答: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;
            }
        }
c# .net wpf windows
10个回答
37
投票

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

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

0
投票

通过执行此操作,我使我的应用程序在辅助屏幕中获得最大化

在主窗口的顶部添加:

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

开始了 !


11
投票

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

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

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


4
投票

我不确定这是否已经回答 - 我创建了一个示例应用程序

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个upvotes的问题值得正确答案。 :d

使用此窗口而不是普通窗口,然后Maximize / 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);

2
投票

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

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


2
投票

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


1
投票

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

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

0
投票

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

祝好运


0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.