C# Disciples

my life in Avalon ….

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

Advertisement

February 27, 2009 - Posted by | WPF

6 Comments »

  1. [...] Title less Windows in WPF (Marlon Grech) [...]

    Pingback by Dew Drop - February 28, 2009 | Alvin Ashcraft's Morning Dew | March 1, 2009 | Reply

  2. Just passing by.Btw, your website have great content!

    _________________________________
    Making Money $150 An Hour

    Comment by Mike | March 1, 2009 | Reply

  3. 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.

    Comment by Rishi | March 1, 2009 | Reply

  4. 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…

    Comment by Andrej Tozon | March 1, 2009 | Reply

  5. hi very greate and cool stuff keep post more

    Comment by Kiran | August 4, 2009 | Reply


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 )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 730 other followers