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;
   5: protected override void WndProc(ref Message message)
   6: {
   8:    base.WndProc(ref message);
  10:    if (message.Msg == WM_NCHITTEST && (int)message.Result == HTCLIENT)
  11:        message.Result = (IntPtr)HTCAPTION;
  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