Introduction
This code demonstrates how you can easily add an additional button (such as Help "?" button) to WPF Window titlebar.
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>
-->
<Border Background="White" Margin="{Binding Source={x:Static shell:SystemParameters2.Current}, Path=WindowNonClientFrameThickness}">
<ContentPresenter Content="{TemplateBinding Content}" />
</Border>
-->
<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
{
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);
}
}
}
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