Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

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

0.00/5 (No votes)
22 Jul 2014 1  
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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here