65.9K
CodeProject is changing. Read more.
Home

WPF C# / VB.NET Additional (fourth) button on window titlebar (Aero only!)

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.17/5 (6 votes)

Jul 22, 2014

CPOL
viewsIcon

25986

downloadIcon

1715

Using WindowChrome from Microsoft.Windows.Shell.dll

Introduction

This code demonstrates how you can easily add an additional button (such as Help "?" button) to WPF Window titlebar.

Screenshot

Attention: this code works properly in Windows Vista, 7, 8, 8.1 only, and if Aero theme (or its equivalent theme, so as standart theme in Windows 8 or in Window 7 Home Basic) is enabled.

Prepaire your Project

Add reference to Microsoft.Windows.Shell.dll to your WPF Project.

Window XAML code

<Window x:Class="!!!PLEASE_PASTE_YOUR_NAMESPACE!!!.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:shell="clr-namespace:Microsoft.Windows.Shell;assembly=Microsoft.Windows.Shell"
    Title="Window1" Height="300" Width="300">

    <Window.Style>
        <Style TargetType="{x:Type Window}">
            <Setter Property="shell:WindowChrome.WindowChrome">
                <Setter.Value>
                    <shell:WindowChrome />
                </Setter.Value>
            </Setter>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Window}">
                        <Grid>
                            <!-- window background (covers black) -->
                            <Border Background="White" Margin="{Binding Source={x:Static shell:SystemParameters2.Current}, Path=WindowNonClientFrameThickness}">
                                <ContentPresenter Content="{TemplateBinding Content}" />
                            </Border>
                            <!-- fouth button! -->
                            <Button Name="btnHelp" Focusable="False" Height="25" HorizontalAlignment="Right" Margin="0,0,120,0" VerticalAlignment="Top" Width="40" shell:WindowChrome.IsHitTestVisibleInChrome="True">?</Button>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Style>

    <Grid>
    </Grid>
</Window>

Window code-behind code in C#.NET

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CSHARP_Additional__fourth__button_on_window_frame
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        public override void OnApplyTemplate()
        {
            var oDep = GetTemplateChild("btnHelp");
            if (oDep != null)
            {
                ((Button)oDep).Click += this.btnHelp_Click;
            }

            base.OnApplyTemplate();
        }

        private void btnHelp_Click(System.Object sender, System.Windows.RoutedEventArgs e)
        {
            MessageBox.Show("Help", "", MessageBoxButton.OK, MessageBoxImage.Information);
        }

        //=======================================================
        //Service provided by Telerik (www.telerik.com)
        //Conversion powered by NRefactory.
        //Twitter: @telerik
        //Facebook: facebook.com/telerik
        //=======================================================
    }
}

Window code-behind code in VB.NET

Class Window1 

    Public Overrides Sub OnApplyTemplate()
        Dim oDep = GetTemplateChild("btnHelp")
        If oDep IsNot Nothing Then
            AddHandler CType(oDep, Button).Click, AddressOf Me.btnHelp_Click
        End If

        MyBase.OnApplyTemplate()
    End Sub

    Private Sub btnHelp_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        MessageBox.Show("Help", _
                        "", _
                        MessageBoxButton.OK, _
                        MessageBoxImage.Information)
    End Sub

End Class