(Print this page)

Adding a button on the title bar
Published date: Monday, November 26, 2007
On: Moer and Éric Moreau's web site

A long time ago, in one of my old VB6 applications, I had a custom button on the application title bar. Users loved it. When the application was rewritten to .Net, I lost that feature. Since then, I never found a method that let you easily add a button on the title bar of my application. Then one day, without searching for it, I found it!

Credits

I did not write the real “juice” of this demo. I want to give full credits to Thomas Stockwell and its article title “Add Transparent Menus and XP Titlebar Buttons to your application” that you can find on The CodeProject. Thanks to Thomas for letting me re-use great portion of his work and share it with you.

The code

The original code that you can download from the CodeProject contains 2 demos. The first one is for Visual Studio 2003 and includes something for transparent menus in addition to the button on the toolbar. The second demo is for Visual Studio 2005 and is limited to the button on the title bar.

The demo application I have packaged to go with this article (is for Visual Studio 2005) and is a single test bed form and the class of Thomas. The form contains a listbox control used to trace the activity of the new button control on the title bar.

How it works

First you need to add the class created by Thomas to your project.

Then you need to create an instance of this new class in the form where you want the button to appear on the toolbar. Notice that the instance is declared at the module level and that we are using the WithEvents keyword to trap and handle the events raised by the class.

Friend WithEvents wintraybtn As WinTrayButton = New WinTrayButton(Me)

Finally, in the Load event of your form (or any other method that runs when the form is initialized), you need to set the button’s properties:

wintraybtn.ButtonImageList = ImageList1
wintraybtn.ButtonMenuText = "Tray Button Menu"

The following section will tell you how to use the ButtonImageList property.

At runtime, if you right-click your new button or if you display the system menu (by clicking the top-left icon), you will see a new menu item added at the bottom of it. The caption of this new item is what you just put into the ButtonMenuText property.

Providing an image list

You can provide an image list to the control. If your image list contains a single image, the image will always be used whatever the state of the control.

If you provide an image list containing 4 images, each of them will be used according to the control state:

  • Image 0 will be used when the control is Normal
  • Image 1 will be used when the control is Hot
  • Image 2 will be used when the control is Pushed
  • Image 3 will be used when the control is Disabled
If your image list contains any other number of images, only the image at index 0 will be used.

If you don’t provide an image list, your button will display the same image as the minimize button.

Handling events

The event that you should at least handle is called MinTrayBtnClicked. If you don’t handle this, your button will only be another decoration on the form!

The code you write in this event is the same you would have wrote for any other button or menu item. Nothing is special about it except that it is visible directly on the title bar.

The demo application is handling a couple of those properties outputting a message into a listbox so that you can follow what is happening.

Figure 1:The demo application in action

Limitation

Thomas explicitly mentions that this code is meant for Windows XP. I have tested it under Vista. It is working but the button is not very good-looking!

If you download the code that goes with this article, you will find that I do a test to detect which version of Windows is running (search for the method named OS_Version). If anything else the XP is running, the button is not displayed.

I found one limitation to that button. If you set your Windows XP to optimize for speed (bypassing the uxTheme.dll to render the display), your user interface when then look plain (as Windows 2000) and the button on the title bar will appear a bit offset but still working. I have also made a test to detect that in the downloadable demo (search for the method named IsOSVersionOK).

Conclusion

With a little effort you can get a useful additional feature directly on the title bar of your application. It is really worth it in many situations. Just be careful if some of your users are still using older version of Windows!


(Print this page)