Title less Windows in WPF

Back in the Win Forms days we had to do a lot of tricks in order to have a title less Window. One (and I believe the best) approach to do this was to override the WndProc and handle the messages to trick windows in thinking that there is a Title. Something like this

   1: private const int WM_NCHITTEST = 0x84;
   2: private const int HTCLIENT = 0x1;
   3: private const int HTCAPTION = 0x2;
   4:
   5: protected override void WndProc(ref Message message)
   6: {
   7:
   8:    base.WndProc(ref message);
   9:
  10:    if (message.Msg == WM_NCHITTEST && (int)message.Result == HTCLIENT)
  11:        message.Result = (IntPtr)HTCAPTION;
  12:
  13: }

This is quite cool and clean… But WPF gives is a nicer way how to do it….

So first of all we need to set the WindowStyle property of the Window to None. It would look something like this

   1: <Window x:Class="TitleLessWindow.Window1"
   2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   4:     Title="Window1" Height="300" Width="300" WindowStyle="None">
   5: </Window>

Now the problem is that the user cannot drag the window around. In order to do this we just have to write 1 line of code…. yes 1 line of code :) THANKS WPF

MouseDown += delegate { DragMove(); };

   1: MouseDown += delegate { DragMove(); };

Put that line of code in the constructor of the Window and that’s it :)

Now the user can drag the window around without any problem :)

I suggest that you also have a look at this brilliant post about Custom windows

About these ads

8 thoughts on “Title less Windows in WPF

  1. Pingback: Dew Drop - February 28, 2009 | Alvin Ashcraft's Morning Dew

  2. I think you need to put

    this.MouseLeftButtonDown += delegate { DragMove(); };

    else you will get an InvalidOperationException on right-click that you can only call DragMove when primary mouse button is down.

    Also, I am not sure about anyone else, but I am getting in redrawing problems in Win 7 using the WindowStyle in “None” mode.

  3. Usually you’d want to set your Window’s style in a resource, in which case you’d need a Command to initialize the dragging and keep the codebehind clear. Also, when your app goes to the “not responding mode”, the border and the title bar are back…

  4. Pingback: Dragging a Titleless Window in WPF « Various Rants

  5. Use below line to avoid Error : Can only call DragMove when primary mouse button is down.

    this.MouseLeftButtonDown += delegate { DragMove(); };

  6. Pingback: Fix Wpf Adorner Error Message Windows XP, Vista, 7, 8 [Solved]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s