简体   繁体   中英

How to create a button in the title area of a window?

How can I create a menu button or just a simple button in WPF in title area of a window which would look like this:

在此处输入图片说明 I prefer a simple solution. Is there any? What I've found so is pretty complicated.

You need create custom window and write style for it. For examlpe:

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

And Style:

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

Add button event:

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

You're talking about modifying the non-client area. I have very little experience with WPF, but the very first link I got on Google was https://msdn.microsoft.com/en-us/library/microsoft.windows.shell.windowchrome.aspx .

This Microsoft library allows you to customize the window chrome as you see fit :) And since you're not just throwing the whole thing away (as you would with WindowStyle = None ), the whole window still works the way you're used to, including the system buttons, dragging etc.

That said, I don't think your sample is a good thing for the user. You shouldn't interfere with the system parts of the chrome. The top-left menu is the window system menu, and it's not very friendly to remove (or replace) it. If you decide to replace it anyway, at least make sure that it works exactly as usual except for the additional parts you want to add.

Another idea would be to simply hide the title bar of your application, by starting it in full screen mode. (set WindowState="Maximized" WindowStyle="None")

Then, you can place your menu button in the top most portion of the client area as you have done already.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM