Windows 7-style Notification Area Applications in WPF: Part 5 (Fixing Aero Borders)

View source on GitHub.

An issue that came to my attention only recently is that the borders of WPF (update: WPF is not actually to blame) windows without captions/title-bars (that is, with ResizeMode set to ‘CanResize’ and WindowStyle set to ‘None’) are drawn incorrectly when the DWM (read: Aero Glass) is enabled. Specifically, the upper and left borders are drawn one pixel too thin (e.g. 3 pixels when the system setting is 4 pixels) and the colour of the bottom and right borders is different to that of other windows. I’ve tried to illustrate these differences in the screenshot below (the image on the left is of a WPF window).

Aero Border Example

It is conceivable that this will be fixed in a future version of WPF Windows, but for now we can use the DwmExtendFrameIntoClientArea function to do it manually. This is only necessary when the DWM is enabled, of course.

Extending Aero Glass into the client area of a window has been covered countless times elsewhere, but I’ll reproduce the process here for convenience.

First, we’ll define the PInvoke signature:

You’ll notice that the function uses a MARGINS type, which is pretty simple:

In our window’s markup, we need to use a Border (or equivalent) as our root element:

And finally, in our code behind, we’ll add the following code (you can add it to your Window’s Loaded event if you only want to do this once):

It is assumed that the GlassEnabled boolean I reference has been set with the DwmIsCompositionEnabled function, the implementation of which is well documented elsewhere.

The neat trick I used to find the window’s DPI comes from this post by fjparisIII.

Update 2011-06-11: Fixed some DPI issues.

Leave a comment

Your email address will not be published. Required fields are marked *