c# - 管理双屏最大化功能

标签 c# wpf window

我正在开发一个与多屏幕兼容的具有多个窗口的应用程序。我为所有窗口制作了自己的标题栏。

我的标题栏:

<Grid x:Class="TitleBar"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      Style="{DynamicResource TitleStyle}"
      MouseLeftButtonDown="gridBar_MouseLeftButtonDown"
      x:Name="titleBar">

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="auto"/>
    </Grid.ColumnDefinitions>

    <Label Grid.Column="0" [...]/>

    <Button Grid.Column="1" x:Name="bttnClose" [...]/>
    <Button Grid.Column="1" x:Name="buttonMinimize" [...]/>
</Grid>

具有以下功能:

private void gridBar_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    if (Window.GetWindow(this) != null)
    {
        if (e.ClickCount == 2) // double click handle
        {
            if (Window.GetWindow(this).WindowState == WindowState.Normal)                   
                Window.GetWindow(this).WindowState = WindowState.Maximized;
            else
                Window.GetWindow(this).WindowState = WindowState.Normal;
        }
        try
        {
            Window.GetWindow(this).DragMove();
        }
        catch { }
    }
}

除主窗口外,我所有的窗口都不能调整大小(最大/最小高度和宽度相等)。所以我想管理全屏到多屏环境。

我在我的 MainWindow 类中添加了这个:

protected override void OnStateChanged(EventArgs e)
{
    if (WindowState == WindowState.Maximized)
    {
        var hwnd = new System.Windows.Interop.WindowInteropHelper(this).EnsureHandle();
        var currentMonitor = NativeMethods.MonitorFromWindow(hwnd, NativeMethods.MONITOR_DEFAULTTONEAREST);
        var primaryMonitor = NativeMethods.MonitorFromWindow(IntPtr.Zero, NativeMethods.MONITOR_DEFAULTTOPRIMERTY);
        var isInPrimary = currentMonitor == primaryMonitor;

        // Don't want to hide the taskbar
        if (isInPrimary)
            this.MaxHeight = SystemParameters.MaximizedPrimaryScreenHeight;
        else
            this.MaxHeight = Double.PositiveInfinity;
    }
    base.OnStateChanged(e);
}

internal static class NativeMethods
{
    public const Int32 MONITOR_DEFAULTTOPRIMERTY = 0x00000001;
    public const Int32 MONITOR_DEFAULTTONEAREST = 0x00000002;

    [System.Runtime.InteropServices.DllImport("user32.dll")]
    public static extern IntPtr MonitorFromWindow(IntPtr handle, Int32 flags);
}

它几乎可以工作,但是当我设置 MaxHeight 时,它不会刷新实际的 Height。我必须按屏幕最大化两次以获得正确的尺寸。

你能帮帮我吗?

最佳答案

我终于找到了解决方案,但可能不是最好的:

private void Maximize()
{
    var hwnd = new System.Windows.Interop.WindowInteropHelper(this).EnsureHandle();
    var currentMonitor = NativeMethods.MonitorFromWindow(hwnd, NativeMethods.MONITOR_DEFAULTTONEAREST);
    var primaryMonitor = NativeMethods.MonitorFromWindow(IntPtr.Zero, NativeMethods.MONITOR_DEFAULTTOPRIMERTY);
    var isInPrimary = currentMonitor == primaryMonitor;

    // Don't want to hide the taskbar
    if (isInPrimary)
        this.MaxHeight = SystemParameters.MaximizedPrimaryScreenHeight;
    else
        this.MaxHeight = Double.PositiveInfinity;
}

protected override void OnStateChanged(EventArgs e)
{
    if (WindowState == WindowState.Maximized && DoRefresh)
    {
        DoRefresh = false;
        WindowState = WindowState.Normal;
        Maximize();
        DoRefresh = true;
    }
}

问题是在最大化的 Windows 上更改 Height 或 MaxHeight 没有效果。当状态更改为最大化时,我将其强制为正常并更改 MaxHeight。之后我将状态重置为最大化。我创建了 bool DoRefresh 以避免无限循环。

关于c# - 管理双屏最大化功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38351483/

相关文章:

c# - 引用 Microsoft.SPOT.Hardware.dll

c# - 接口(interface)在内部是如何工作的?

c# - 如何在 WPF 中 move Canvas 上的形状?

c# - 检查 SQLite 表输入字段 - C# WPF

c# - 在 MVC 中从 Google OAuth API 检索出生日期

c# - 增长的、固定容量的通用序列的最佳选择

c# - WPF 中 DataGrid 的双向绑定(bind)

visual-studio - 从 Visual Studio 内存窗口保存数据

jquery - 当 CSS 不是一个选项时使用 jQuery 进行媒体查询

c++ - C++ 中的跨平台 GUI 窗口