void Nish(char* szBlog);

Nish’s thoughts on MFC, C++/CLI and .NET

void Nish(char* szBlog); header image 2

WPF tip : Using a custom title bar

October 27th, 2007 · 3 Comments

While you can always custom paint the windows title bar, in most cases it ends up being a very messy approach with possible side effects. What I do in WPF is to disable the title bar in the window by setting its WindowStyle to none.

<Window x:Class="..."
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    ...
    MinWidth="700" WindowStyle="None" Icon="...">

I then define a user control that will act as the title bar and place it on top of the window. Adding maximize/restore, minimize, and close buttons is not a biggie. You just need to remember to toggle the maximize button with restore (to match normal windows behavior). All you need to do is to set WindowState to WindowState.Minimized, WindowState.Normal, or WindowState.Maximized as required. You might also want to handle double-click and route it to the maximize/restore handler (again to match windows behavior). The one final step would be to handle title bar based drag. To do that, handle the MouseDown event and do this :-

void TitleBar_MouseDown(object sender, MouseButtonEventArgs e)
{
    if(e.ChangedButton == MouseButton.Left)
        Application.Current.MainWindow.DragMove();
}

Tags: WPF (Avalon)

3 responses so far ↓

  • 1 Colin // Dec 9, 2007 at 2:55 am

    Good work Nish.

  • 2 Mike // Feb 24, 2010 at 1:45 pm

    The major problem with this approach is when the window is maximized it occupies the whole screen, covering the task bar.

  • 3 Gana // Jun 10, 2010 at 12:59 am

    I have the same problem of covering the whole window with task bar, did u have any solution for this

Leave a Comment