如何在窗口的标题区域中创建按钮?

时间:2016-02-03 08:08:21

标签: c# .net wpf windows

如何在窗口的标题区域中创建菜单按钮或WPF中的简单按钮,如下所示:

enter image description here 我更喜欢简单的解决方案。有没有?我发现的内容非常复杂。

3 个答案:

答案 0 :(得分:1)

您需要为其创建自定义窗口并编写样式。 对于考试:

public class CustomWindow : Window
{
    protected void MinimizeClick(object sender, RoutedEventArgs e)
    {
        WindowState = WindowState.Minimized;
    }

    protected void RestoreClick(object sender, RoutedEventArgs e)
    {
        WindowState = (WindowState == WindowState.Normal) ? WindowState.Maximized : WindowState.Normal;
    }

    protected void CloseClick(object sender, RoutedEventArgs e)
    {
        Close();
    }
}

风格:

 <Style TargetType="{x:Type local:CustomWindow}">
        <Setter Property="WindowStyle" Value="None"/>
        <Setter Property="Background" Value="White"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="BorderBrush" Value="Silver"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:CustomWindow}">
 <Border BorderThickness="{TemplateBinding BorderThickness}"
                            BorderBrush="{TemplateBinding BorderBrush}">
                        <Grid>
                            <Grid>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition />
                                </Grid.RowDefinitions>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition />
                                    <ColumnDefinition Width="Auto"/>
                                </Grid.ColumnDefinitions>
                                <Rectangle x:Name="moveRectangle" Fill="Transparent"
                                           Grid.Row="0" Grid.Column="0"/>

                                    Add custom button here

                                <StackPanel Grid.Row="0" Grid.Column="1" Orientation="Horizontal">
                                    <Button x:Name="minimizeButton" Style="{StaticResource WindowButtonStyle}"
                                            Content="0" />
                                    <Button x:Name="restoreButton" Style="{StaticResource WindowButtonStyle}"
                                            Content="1" />
                                    <Button x:Name="closeButton" Style="{StaticResource WindowButtonStyle}"
                                            Content="r" />
                                </StackPanel>
                                <Grid Background="{TemplateBinding Background}"
                                           Grid.Row="1" Grid.ColumnSpan="2" Margin="5,5,5,5">
                                    <AdornerDecorator>
                                        <ContentPresenter/>
                                    </AdornerDecorator>
                                </Grid>
                            </Grid>
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

添加按钮事件:

public override void OnApplyTemplate()
{
    Button minimizeButton = GetTemplateChild("minimizeButton") as Button;
    if (minimizeButton != null)
        minimizeButton.Click += MinimizeClick;

    Button restoreButton = GetTemplateChild("restoreButton") as Button;
    if (restoreButton != null)
        restoreButton.Click += RestoreClick;

    Button closeButton = GetTemplateChild("closeButton") as Button;
    if (closeButton != null)
        closeButton.Click += CloseClick;

    Subscribe if needed

    base.OnApplyTemplate();
}

答案 1 :(得分:1)

您正在谈论修改非客户区域。我对WPF的经验很少,但我在Google上获得的第一个链接是https://msdn.microsoft.com/en-us/library/microsoft.windows.shell.windowchrome.aspx

这个Microsoft库允许您根据需要自定义窗口镶边:)并且因为您不仅仅是抛弃整个东西(就像使用WindowStyle = None一样),整个窗口仍然可以正常工作你已经习惯了,包括系统按钮,拖动等。

那就是说,我不认为你的样本对用户来说是件好事。您不应该干扰chrome的 system 部分。左上角的菜单是窗口系统菜单,删除(或替换)它不是很友好。如果您决定更换它,至少要确保它与正常情况一样除了以便添加其他部分。

答案 2 :(得分:1)

另一个想法是简单地隐藏应用程序的标题栏,方法是以全屏模式启动它。 (设置WindowState =&#34;最大化&#34; WindowStyle =&#34;无&#34;)

然后,您可以将菜单按钮放在客户区的最顶部,就像您已经完成的那样。