R – Help getting .Net WinForms apps to support Vista Aero Glass


There are a couple of tricks for getting glass support for .Net forms.

I think the original source for this method is here: http://blogs.msdn.com/tims/archive/2006/04/18/578637.aspx


//reference Desktop Windows Manager (DWM API)
[DllImport( "dwmapi.dll" )]
static extern void DwmIsCompositionEnabled( ref bool pfEnabled );

[DllImport( "dwmapi.dll" )]
static extern int DwmExtendFrameIntoClientArea( IntPtr hWnd, ref MARGINS pMarInset );

//then on form load
//check for Vista
if ( Environment.OSVersion.Version.Major >= 6 )
    //check for support
    bool isGlassSupported = false;
    DwmIsCompositionEnabled( ref isGlassSupported );

    if ( isGlassSupported )
        DwmExtendFrameIntoClientArea( this.Handle, ref margins );

//finally on print draw a black box over the alpha-ed area
//Before SP1 you could also use a black form background

That final step is the issue – any sub controls drawn over that area seem to also treat black as the alpha transparency mask.

For instance a tab strip over the class area will have transparent text.

Is there a way around this?

Is there an easier way to do this?

The applications I'm working on have to work on both XP and Vista – I need them to degrade gracefully. Are there any best practices here?

Best Solution

There really isn't an easier way to do this. These APIs are not exposed by the .NET Framework (yet), so the only way to do it is through some kind of interop (or WPF).

As for working with both Windows versions, the code you have should be fine, since the runtime does not go looking for the entry point to the DLL until you actually call the function.