Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Hiding the Caption and Icon in the Title Bar in Windows Vista

4.15/5 (14 votes)
28 Apr 20072 min read 1   1.2K  
Prevent the Caption and Icon from appearing in the Title Bar, while still appearing in the Task bar, like Windows Explorer

Screenshot - hide.png

Introduction

Ever noticed in Windows Explorer (My Computer) that the Caption (Title Bar Text) and Icon are not displayed? Ever wondered how to do it in your program?
This Guide Will Show You How.

Background

Say you have extended the Glass Effects a bit further (like in the example) and then have put a larger, easier to read title there, or perhaps a logo instead. Or as in Explorer you have what is usually in the title available just a bit further down. For example, this is the same as the screenshot above, but with the Caption visible:

Screenshot - nohide.png

It seems kind of pointless doesn't it? You may go as far to say it looks very unprofessional. Well, Vista has functions that enable you to hide the Caption and Icon, the best thing about using these is that the caption still appears in the task bar at the bottom. If you tried to remove it by blanking out the .Text property, the caption would disappear in the task bar, which will lead to confusion.

Using the code

Unfortunately there is no way to set the properties we want in Managed code, so we have to call Native "Unmanaged" Code to hide it. Luckily its not too hard to move over and it works perfectly. To Use it, you must first fill in an WTA_OPTIONS struct, which contains what you want to remove, and then you must call the function.

C#
DWM.WTA_OPTIONS ops = new DWM.WTA_OPTIONS();
// We Want To Hide the Caption and the Icon
ops.Flags = DWM.WTNCA_NODRAWCAPTION | DWM.WTNCA_NODRAWICON;
// If we set the Mask to the same value as the Flags, the Flags are Added. 
// If not they are Removed
ops.Mask = DWM.WTNCA_NODRAWCAPTION | DWM.WTNCA_NODRAWICON;
// Set It, The Marshal.Sizeof() stuff is to get the right size of the 
// custom struct, and in UINT/DWORD Form
DWM.SetWindowThemeAttribute(this.Handle,
    DWM.WindowThemeAttributeType.WTA_NONCLIENT,
        ref ops,
        (uint)Marshal.SizeOf(typeof(DWM.WTA_OPTIONS)
); 

I provided in the Code all four Flags that Microsoft documented, although you will probably only use the first two. If you don't understand the line:

C#
(uint)Marshal.SizeOf(typeof(DWM.WTA_OPTIONS) 

Don't Worry, its just to get the right memory size for the Structure. You will never need to change it even if you change the flags.

Points of Interest

Writing this code helped me realise just how easy it was to use native code in C#. Finding out the function to use took a lot longer than writing it!

The uints everywhere are C# closest match to DWORD, which is what the documentation specifies as the top.

For some reason, Microsoft made the AttributeType an enum, although at the moment there is only one value, WTA_NONCLIENT or 1. I kept it as an enum for consistency with the native code.

In the download, I also included the DWM Code to extend margins, because it works well with this, and is not that hard to do. I didn't write it, but I saw it on numerous sites so I can't remember who to credit. Sorry!

History

  • 1.0 - First Revision

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here