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:
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.
DWM.WTA_OPTIONS ops = new DWM.WTA_OPTIONS();
ops.Flags = DWM.WTNCA_NODRAWCAPTION | DWM.WTNCA_NODRAWICON;
ops.Mask = DWM.WTNCA_NODRAWCAPTION | DWM.WTNCA_NODRAWICON;
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:
(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 uint
s 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