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

xriantvc  于 2023-01-27  发布在  Windows
关注(0)|答案(9)|浏览(200)

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

zpf6vheq1#

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

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

5gfr0r5j2#

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

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

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

aiazj4mn

aiazj4mn3#

我不确定这个问题是否得到了解答-我使用

WindowStyle = WindowStyle.None;

我创建了一个按钮并在单击处理程序上执行了以下操作-

WindowState = WindowState.Maximized

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

this.MouseLeftButtonDown += new(MainWindow_MouseLeftButtonDown);

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

当我把我的窗口拖到我的第二个监视器上并单击最大化按钮时,它在当前窗口中最大化,而不是启动窗口。我正在使用VS2010和.NET 4。让我知道这是否有帮助。

kt06eoxx

kt06eoxx4#

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

mtb9vblg5#

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

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

bz4sfanl6#

我有我的应用程序得到最大化在第二屏幕上这样做
在主窗口顶部添加以下内容:

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

开始了!

5lwkijsr

5lwkijsr7#

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

e1xvtsh3

e1xvtsh38#

我刚刚遇到了同样的问题。在我的情况下,它原来是这样的事实,我隐藏我的弹出窗口时,我做了它。所以,如果我调用它下次,并要求它最大化,它会做它在原来的屏幕上。一旦我开始关闭它,而不是,它开始最大化在适当的屏幕上。

pbwdgjma

pbwdgjma9#

试试看:

Window.WindowState = WindowState.Normal;

相关问题