最大化窗口时从顶部拖动自定义标题栏无效。

15

我有一个自定义标题栏,窗口样式设置为无。在单击标题栏时,我检查是否双击(执行窗口最大化和还原),如果不是双击,则执行Window.DragMove。这对于将窗口吸附到侧面和顶部非常有效。但是,当我尝试在窗口最大化时拖动窗口(通常会恢复窗口),它什么也不做。以下是我的代码:

    static Window Window { get { return Application.Current.MainWindow; } }

    /// <summary>
    /// TitleBar_MouseDown - Drag if single-click, resize if double-click
    /// </summary>
    private static void TitleBar_MouseDown(object sender, MouseButtonEventArgs e)
    {
        if (e.ChangedButton == MouseButton.Left)
        {
            if (e.ClickCount == 2)
            {
                AdjustWindowSize();
            }
            else
            {
                Window.DragMove();//Here is where I do the drag move
            }
        }
    }

    /// <summary>
    /// Adjusts the WindowSize to correct parameters when Maximize button is clicked
    /// </summary>
    internal static void AdjustWindowSize()
    {
        if (Window.WindowState == WindowState.Maximized)
        {
            SystemCommands.RestoreWindow(Window);
        }
        else
        {
            SystemCommands.MaximizeWindow(Window);
        }

    }

    #region Button Events

    /// <summary>
    /// CloseButton_Clicked
    /// </summary>
    public static void Close()
    {
        SystemCommands.CloseWindow(Window);
    }

    /// <summary>
    /// MaximizedButton_Clicked
    /// </summary>
    public static void Maximize()
    {
        AdjustWindowSize();
    }

    /// <summary>
    /// Minimized Button_Clicked
    /// </summary>
    public static void Minimize()
    {
        SystemCommands.MinimizeWindow(Window);
    }

    #endregion

现代UI和MahApps.Metro以某种方式实现了它,我简要查看了它们的源代码,但找不到它们如何实现的方法。

提前致谢。

1个回答

44

我能够在纯XAML中实现所需的标题栏行为,包括Aero Snap。

因此您可以看到一个自定义的标题栏,它完全可拖动,双击最大化和还原并拖动以捕捉和释放。

XAML

<Window x:Class="CSharpWPF.MainWindow" 
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" >
    <WindowChrome.WindowChrome>
        <WindowChrome CaptionHeight="{Binding ActualHeight,ElementName=titlebar}"/>
    </WindowChrome.WindowChrome>
    <DockPanel LastChildFill="True">
        <Border Background="LightBlue" DockPanel.Dock="Top" Height="25" x:Name="titlebar">
            <TextBlock Text="{Binding Title, RelativeSource={RelativeSource FindAncestor,AncestorType=Window},FallbackValue=Title}" 
                       Margin="10,0,0,0"
                       VerticalAlignment="Center">
                <TextBlock.Effect>
                    <DropShadowEffect Color="White" ShadowDepth="3"/>
                </TextBlock.Effect>
            </TextBlock>
        </Border>
        <Border BorderBrush="LightGray" BorderThickness="1" Padding="4">
            <TextBlock Text="Window content"/>
        </Border>
    </DockPanel>
</Window>

结果

结果

现在你不需要编写任何代码来手动处理标题栏了。

可重复使用的样式

你还可以将上述内容包装在自定义样式中,然后应用于多个窗口。

<Style TargetType="Window" x:Key="CustomTitleBar">
    <Setter Property="WindowChrome.WindowChrome">
        <Setter.Value>
            <WindowChrome CaptionHeight="{x:Static SystemParameters.CaptionHeight}" />
        </Setter.Value>
    </Setter>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Window">
                <DockPanel LastChildFill="True">
                    <Border Background="LightBlue" DockPanel.Dock="Top" 
                            Height="{x:Static SystemParameters.CaptionHeight}" x:Name="titlebar">
                        <Grid>
                            <TextBlock Text="{TemplateBinding Title}" 
                                        Margin="10,0,0,0"
                                        VerticalAlignment="Center">
                                <TextBlock.Effect>
                                    <DropShadowEffect Color="White" ShadowDepth="3"/>
                                </TextBlock.Effect>
                            </TextBlock>
                        </Grid>
                    </Border>
                    <Border Background="{TemplateBinding Background}" BorderBrush="LightGray" 
                            BorderThickness="1" Padding="4">
                        <ContentPresenter/>
                    </Border>
                </DockPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

用法

<Window x:Class="CSharpWPF.View" 
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                Title="MainWindow" 
                Style="{StaticResource CustomTitleBar}" >
    <TextBlock Text="Window content"/>
</Window>

如何在您的代码中实现

在查看您的代码之后,我成功地进行了实现,而且只需进行很少的更改。

更改内容如下:

文件:CustomChrome.cs

第41行:将 CaptionHeight = 36 改为与您的标题栏高度相等(当前为0)。

var chrome = new WindowChrome() { GlassFrameThickness = new Thickness(-1), CaptionHeight = 36 };

第60行:将((FrameworkElement)sender).MouseDown += TitleBar_MouseDown;删除,因为不需要

第70行:删除不再使用的事件TitleBar_MouseDown

文件:CornerButtons.xaml

第13行:在StackPanel中添加WindowChrome.IsHitTestVisibleInChrome="True"

    <StackPanel SnapsToDevicePixels="True" Orientation="Horizontal" WindowChrome.IsHitTestVisibleInChrome="True">

文件: MainWindow.xaml

第17行: 在StackPanel中添加WindowChrome.IsHitTestVisibleInChrome="True"

<cc:CornerButtons Grid.Column="2">
    <StackPanel Orientation="Horizontal"
                WindowChrome.IsHitTestVisibleInChrome="True">

这就是全部,您的应用程序将拥有正常的标题栏,无需处理自定义逻辑。


我尝试过了,但是没有起作用。然后它做了一些奇怪的事情。 - James Esh
我尝试过那个方法,但不起作用,因为它覆盖了按钮(关闭、最小化、还原)。那时我就无法点击它们了。 - James Esh
3
你是否设置了 WindowChrome.IsHitTestVisibleInChrome="True"?这是让那些按钮正常工作所必需的。你也可以单独为每个按钮进行设置。 - pushpraj
我更新了我的答案,请查看答案中的 MainWindow.xaml,这将使主窗口中的 ComboBox 和 Button 也能够使用。 - pushpraj
1
自定义窗口很棒,但是当您最大化和最小化窗口时,它只是突然弹出和隐藏,缺少一些动画效果。您知道如何实现默认的最小化和最大化效果吗? - Uzair Ali
显示剩余10条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接