Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / WinForms

Take Control of Your Non-Client-Area!

3.36/5 (11 votes)
31 May 2008CPOL3 min read 1   1.8K  
Learn how to control your non-client-area, while keeping all the stuff Vista puts on it.

MyBar

Introduction

Microsoft always seems to add some snazzy new features to programs like Office. Then, all the other developers run off to do it themselves, sell controls, etc. Well, there are plenty of ribbons out there, but there is something everyone seems to have forgotten. You can't draw on the title bar. So... how did Microsoft and those UserControl makers do it? That's what I wanted to know, because I needed to put stuff on the title bar. Googled everything I could think of, and ended up with an interesting C++ article about. After about a week, I came up with six lines of code that did the job. Never would have thought 6 lines = 1 week, eh? So, I suppose, you just want to make cool title bars now, I will get on with it now...

Background

This article assumes you and the end user will both have Vista with Aero Glass. There is no code to check for this, or if you even have Vista, and I do not know what happens when you try on XP. The most complex part is the seven line WndProc, but we won't focus on the WinAPI much. Instead, I will assist you in solving the various issues this causes.

Using the code

To add this to your own application, you need to simply follow the tutorial below.

Open (or create a new) project. Select a Windows Forms Application, if it is a new project. After everything loads, click on your form once. Set the BackColor in the Properties window to Black. This allows it to be painted glassy. Now, add a reference to the Vista Controls Library. It is on CodePlex and included in my project zip above. Go back to the form editor and set your Form's BorderStyle to None. In the form Loaded event, add FormName.formborderstyle = System.Windows.Forms.FormBorderStyle.FixedSingle. Go to a new line in the Form's Load event, and add this code: VistaControls.DWM.DWMManager.EnableGlassSheet(Me). Now, for the most important part. Inside Public Class Formname, add Protected Overrides WndProc and hit Enter. Add the code below:

VB
If M.Msg = &H83 then
    Dim point as new point(m.lparam.toint32)
    m.result = new intptr(-1)
    return
end if

Put that in before MyBase.WndProc.

Now, run your application and you should have a title-less, icon-less, undraggable form with unresponsive title-bar buttons. Fantastic!

Because this an intermediate/advanced article, I am not going to go over all of this (you can find it in the solution above), but only the more difficult or unexpected parts. Please post a comment if you want help with a part I didn't cover. If it is not already open, you may want to open the project above for reference.

First, when the maximize option is clicked, it checks if the window is minimized, maximized, or normal. If it is normal, it maximizes, else it 'normalizes?' back into a window.

Second, there is a timer called checkButtons. This checks if Minimize or Maximize are disabled on the forums, and disables the appropriate button handler; however, this is one way. You cannot re-enable a button handler once it is disabled. What happens here is:

It checks if the buttons exist, and if they do not, moves the non-existent-buttons' handlers far off the screen (will be destroyed in the next update).

Now, for the reason I told you to set BorderStyle to None. This is because it gives you a more accurate picture of what your form will look like, because VB won't let you put stuff on the 'titlebar' in the designer.

Please consult the sample for anything I didn't cover, and feel free to post a comment asking about something in the sample you do not get.

Points of interest

The Desktop Window Manager must be extending the window's glass in some way in order to control the non-client area.

Also, it is strange how little code it takes once you know what you are doing.

History

None... Yet.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)