Click here to Skip to main content
11,932,168 members (64,645 online)
Click here to Skip to main content
Add your own
alternative version


39 bookmarked

Producing translucent dialog boxes and windows without flickering

, 6 Aug 2002
Rate this:
Please Sign up or sign in to vote.
Enhancing window fade-in to render common controls correctly and to allow the window to remain transparent after the fade-in


The AnimateWindow() API with the AW_BLEND parameter is supposed to fade in and out windows smoothly. Basically, it has two drawbacks:
  1. Text and ListView controls (and several others) aren't rendered correctly during fade-in.
  2. You cannot specify a translucency for the window, i.e., after the animation is over, the window is opaque.
Clicking 'No' in the sample project demonstrates both problems.


There seemed to be a simple workaround for this: Adding the WS_EX_LAYERED extended style to the window and calling SetLayeredWindowAttributes() with the desired alpha value, as described in the MSDN Library and in various other sources. Unfortunately, this didn't work either, at least on my XP Pro system. The code produced a very nasty flickering: the window area was initially black.


The following code shows how to fade in a dialog to a predefinded translucency without flickering:
  HWND hwndDlg,  // handle to dialog box
  UINT uMsg,     // message
  WPARAM wParam, // first message parameter
  LPARAM lParam  // second message parameter
   RECT rcDesktop;
   RECT rcMe;

   BYTE bTranslucency;

   const BYTE TRANSLUCENCY = 192;

   switch (uMsg) {
      // Make it a layered window.
      ::SetWindowLong(hwndDlg, GWL_EXSTYLE,
         ::GetWindowLong(hwndDlg, GWL_EXSTYLE) | WS_EX_LAYERED);

      // Completely transparent window - note the third parameter
      ::SetLayeredWindowAttributes(hwndDlg, 0, 0, LWA_ALPHA);

      // Show it _first_
      ::ShowWindow(hwndDlg, SW_SHOW);

      // Redraw contents NOW - no flickering since the window's not visible
      ::RedrawWindow(hwndDlg, NULL, NULL, RDW_UPDATENOW); 

      // Normally, you would use a timer here...
      for (bTranslucency = 0; bTranslucency < TRANSLUCENCY;
      bTranslucency+=TRANSLUCENCY_STEP) {
         // Adjust the translucency
         ::SetLayeredWindowAttributes(hwndDlg, 0, bTranslucency, LWA_ALPHA);

         // Wait

      // Set the final translucency
      ::SetLayeredWindowAttributes(hwndDlg, 0, bTranslucency, LWA_ALPHA);

   return 0;
Note that the call to ShowWindow() is usually done by the CreateDialogXXX() functions if the dialog template has the WS_VISIBLE style set. For our purposes, we have to do it right here in response to the WM_INITDIALOG message.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
Germany Germany
Kirill lives in Berlin (Germany). The name comes from Russia where he was born and where he spent about eight years.

He started with a C64. Soon, he found out that BASIC is as slow as it is simple, and started to learn Assembler which revealed the "true power" of this computer.

After a while, a 486 was bought. He started to learn C/C++ and Pascal and to learn about algorithms and data structures.

Currently, he is studying computer sciences at TU Karlsruhe and working for orgAnice Software GmbH as developer.

He enjoys playing badminton and pool billards.

You may also be interested in...

Comments and Discussions

GeneralVery Nice, it even works with MSDev 4.2b Pin
binyo6628-Jun-09 18:37
memberbinyo6628-Jun-09 18:37 
GeneralIf the background application is windows media player,it can not transparent Pin
zseasoft13-Oct-07 23:22
memberzseasoft13-Oct-07 23:22 
GeneralWell done Pin
Dr.Luiji29-Aug-07 23:17
memberDr.Luiji29-Aug-07 23:17 
QuestionBug in this approach - is there a work around Pin
venki_rtz17-Jan-07 23:03
membervenki_rtz17-Jan-07 23:03 
QuestionCan have 100% opaque window text while 70% transparent window background? [modified] Pin
Alexandru31-May-06 0:40
memberAlexandru31-May-06 0:40 
GeneralProb w/ Translucent window overlapped with DirectX Window Pin
surmitha20-Dec-05 9:17
membersurmitha20-Dec-05 9:17 
GeneralRe: Prob w/ Translucent window overlapped with DirectX Window Pin
-Kirill-20-Dec-05 10:58
member-Kirill-20-Dec-05 10:58 
Doesn't DirectX itself provide some sort of translucent objects?
Generalwhy SetLayeredWindowAttributes can't used in 8 bits(256) for Win2k Pin
whizzkid@ms28.hinet.net11-Aug-04 13:02
memberwhizzkid@ms28.hinet.net11-Aug-04 13:02 
QuestionHow can this be implemented in VB.NET? Pin
PC3700DDR4-May-04 14:43
memberPC3700DDR4-May-04 14:43 
AnswerRe: How can this be implemented in VB.NET? Pin
-Kirill-5-May-04 12:08
member-Kirill-5-May-04 12:08 
GeneralRe: How can this be implemented in VB.NET? Pin
PC3700DDR23-May-04 11:44
memberPC3700DDR23-May-04 11:44 
Generalit can't do anything for a subwindow(control) in a dialog Pin
Anonymous27-Sep-02 17:22
sussAnonymous27-Sep-02 17:22 
GeneralRe: it can't do anything for a subwindow(control) in a dialog Pin
-Kirill-27-Sep-02 17:49
member-Kirill-27-Sep-02 17:49 
GeneralRe: it can't do anything for a subwindow(control) in a dialog Pin
skygg9-Jun-03 3:35
memberskygg9-Jun-03 3:35 
GeneralRe: it can't do anything for a subwindow(control) in a dialog Pin
-Kirill-9-Jun-03 6:47
member-Kirill-9-Jun-03 6:47 
GeneralWin Ver... Pin
CDotNetUser14-Aug-02 14:45
memberCDotNetUser14-Aug-02 14:45 
GeneralRe: Win Ver... Pin
CDotNetUser15-Aug-02 6:09
memberCDotNetUser15-Aug-02 6:09 
GeneralSome restrictions... Pin
Mykel7-Aug-02 22:12
sussMykel7-Aug-02 22:12 
GeneralRe: Some restrictions... Pin
CDotNetUser14-Aug-02 14:37
memberCDotNetUser14-Aug-02 14:37 
GeneralRe: Some restrictions... Pin
-Kirill-14-Aug-02 16:00
suss-Kirill-14-Aug-02 16:00 
QuestionDo I need Platform SDK ? Pin
Anthony_Yio7-Aug-02 18:28
memberAnthony_Yio7-Aug-02 18:28 
AnswerRe: Do I need Platform SDK ? Pin
-Kirill-14-Aug-02 15:48
suss-Kirill-14-Aug-02 15:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.151126.1 | Last Updated 7 Aug 2002
Article Copyright 2002 by -Kirill-
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid