R – WPF Glass window fallback

aeroaero-glasswpf

Creating Glass window is as easy as calling DwmExtendFrameIntoClientArea in WPF, but that is only half of the trick. If you disable aero, and get the XP-like skin thats where the pain begins:

In XP (or disabled aero) you must call DrawThemeBackground in order to get "transparent like feel", Internet explorer does this too on its top, try disabling aero and look that.

I've cooked up application that does just that, fallback gracefully when Aero is disabled in Windows.Forms.

The question: But doing it in WPF is different, the OnRender (OnPaint equiv. in avalon) which gives you DrawingContext, how one draws on that with DrawThemeBackground WINAPI call?

Best Answer

Well, DrawThemeBackground needs an device context handle, which is a pure Win32 concept... WPF doesn't use device contexts or window handles. However, a WPF app is hosted in a Win32 window, and you can retrieve the HWND of that window :

using System.Windows.Interop;

...

IntPtr hwnd = new WindowInteropHelper(this).Handle;

You can then obtain a DC for this window using the GetDC API :

[DllImport("user32.dll")]
static extern IntPtr GetDC(IntPtr hWnd);

...

IntPtr hdc = GetDC(hwnd);

You should then be able to use DrawThemeBackground with this DC.

Note that this is all purely theoretical, I didn't test it...