How to set Transparent Dialogs?

7 10 2008

The Invisible Man” by HG Wells. I still remember reading the translated version of that classic, when i was a kid. And even thought to conduct some experiments to become invisible. You could guess, what happened then. 🙂 I couldn’t. But now I feel happy that atleast I could find a magic portion which can make dialogs invisible. 😉  Well, how to change the transparency of dialogs?

The secret is Layered windows. For that you’ve to enable WS_EX_LAYERED style and set the alpha of dialog by calling SetLayeredWindowAttributes(). See the code snippet below.

// Enable WS_EX_LAYERED window extended style.
LONG ExtendedStyle = GetWindowLong( GetSafeHwnd(),
                                    GWL_EXSTYLE );
SetWindowLong( GetSafeHwnd(),
               ExtendedStyle | WS_EX_LAYERED );

// Select the transparency percentage.
// The alpha will be calculated accordingly.
double TransparencyPercentage = 50.0;

// Set the alpha for transparency.
// 0 is transparent and 255 is opaque.
double fAlpha = TransparencyPercentage * ( 255.0 /100 );
BYTE byAlpha = static_cast<BYTE>( fAlpha );
SetLayeredWindowAttributes( GetSafeHwnd(),
                            LWA_ALPHA );

Layered windows are available from Windows 2000 onwards. So don’t forget to add _WIN32_WINNT=0x0500 to project settings for preparing the dialog invisible portion. 😉

Targeted Audience – Intermediate.