C#| WPF - 自定义标题栏 - 最大化时从顶部拖动窗口不起作用

Jam*_*Esh 10 c# wpf customization window titlebar

我有一个自定义标题栏,窗口样式设置为无.在单击标题栏时,我检查是否是双击(如果窗口最大化并恢复),如果不是双击我做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
Run Code Online (Sandbox Code Playgroud)

现代UI和MahApps.Metro以某种方式做到这一点,我简要地查看了他们的源代码,但无法找到他们是如何做到的.

提前致谢.

pus*_*raj 39

我能够获得标题栏的所需行为,包括纯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>
Run Code Online (Sandbox Code Playgroud)

结果

结果

所以现在你不需要任何代码来手动处理标题栏.

可重复使用的款式

你也可以在一个自定义样式中包装上面,你可以在几个窗口上应用

<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>
Run Code Online (Sandbox Code Playgroud)

用法

<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>
Run Code Online (Sandbox Code Playgroud)

How to implement in your code

在查看您的代码之后,我确实设法通过很少的更改来实现它

变化是

文件: CustomChrome.cs

第41行:更改CaptionHeight = 36,当前为0.这应该等于标题栏高度

var chrome = new WindowChrome() { GlassFrameThickness = new Thickness(-1), CaptionHeight = 36 };
Run Code Online (Sandbox Code Playgroud)

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

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

文件: CornerButtons.xaml

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

    <StackPanel SnapsToDevicePixels="True" Orientation="Horizontal" WindowChrome.IsHitTestVisibleInChrome="True">
Run Code Online (Sandbox Code Playgroud)

文件: MainWindow.xaml

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

<cc:CornerButtons Grid.Column="2">
    <StackPanel Orientation="Horizontal"
                WindowChrome.IsHitTestVisibleInChrome="True">
Run Code Online (Sandbox Code Playgroud)

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

  • 您是否设置了`WindowChrome.IsHitTestVisibleInChrome="True"`,这是这些按钮工作所必需的。您也可以单独设置按钮。 (3认同)