Click here to Skip to main content
11,935,053 members (53,921 online)
Click here to Skip to main content
Add your own
alternative version


31 bookmarked

Transparent Windows Support in Win 2000 and Win ME

, 3 Aug 2002
Rate this:
Please Sign up or sign in to vote.
If you want to make your window transparent but don't know how, here is the answer to your question


I always wondered how to make a window transparent so that all the clicks on the window filter down to the window underneath .At last Microsoft has provided support in Windows Me and 2000 for creating transparent windows . And alpha-blending (This support is not there in previous platforms). In previous platforms (Win 9x ) one could use SetWindowRgn to make only specified part of a window visible but creating a really transparent window wasn’t so easy .

So Lets get on with the code . The support added by Microsoft Lies in a new function named SetLayeredWindowAttributes . But before you can use this function you have to tell Windows that your window is a layered window . This causes Windows to internally allocate the buffers required for creating layered, translucent effects. The code for doing this is

SetWindowLong (hWnd , GWL_EXSTYLE , 
    GetWindowLong (hWnd , GWL_EXSTYLE ) | WS_EX_LAYERED ) ;

Here The hWnd is the handle to the window which has to be set transparent. GWL_EXSTYLE parameter is used for accessing and modifying the extended style of the window. WS_EX_LAYERED parameter is the window style for a layered window. If the compiler gives you an error of undefined variable WS_EX_LAYERED then define it as

# define WS_EX_LAYERED 0x80000 

After you have done this you can now call the magic function SetLayeredWindowAttributes. Let me show you the code snippet from the sample.

HWND hWnd=this->m_hWnd; 
PSLWA pSetLayeredWindowAttributes;
HMODULE hDLL = LoadLibrary ("user32");
pSetLayeredWindowAttributes = 
    (PSLWA) GetProcAddress(hDLL,"SetLayeredWindowAttributes");
if (pSetLayeredWindowAttributes != NULL) {
    * Second parameter RGB(255,255,255) sets the colorkey 
    * to white LWA_COLORKEY flag indicates that color key 
    * is valid LWA_ALPHA indicates that ALphablend parameter 
    * (factor) is valid
    pSetLayeredWindowAttributes (hWnd, 
        RGB(255,255,255), factor, LWA_COLORKEY|LWA_ALPHA);

The Code first loads the library User32.lib which defines the SetLayeredWindowAttributes function. It then gets the address of the function SetLayeredWindowattributes by using GetProcAddress. It checks if it has got a valid function pointer to ensure that it is not going to crash if run on platform other than Win 2000 and Win Me. It then calls the SetLayeredWindowAttributes Function .

Color Keys

Color Keys are a way of telling the Windows that which color to take as being transparent. Say if you set the Color Key to Blue and select LWA_COLORKEY flag in the SetLayeredWindowAttributes then wherever in you window Color Blue appears it will be taken as transparent and Windows will show whatever lies beneath that color.

Alpha Blend Factor

Alpha Blend factor that is the third parameter in SetLayeredWindowAttributes indicates and controls the degree of transparency. It has to be within range of 0 to 255 and you have to specify LWA_ALPHA flag in the fourth parameter to say that the Alpha Blend Factor is valid. When Alpha Blend factor is 0 then window is completely transparent and when it is 255 window is Opaque.

NOTE : You may have to define the two flags LWA_ALPHA and LWA_COLORKEY as

#define LWA_COLORKEY 1 
#define LWA_ALPHA 2 

in case of a compiler error. When Window is transparent all the clicks on the window go down to the window underneath it .

The Sample

The Sample program provided is very simple. It sets the Layered attribute of windows in OnInitDialog Function and then sets the timer to send WM_TIMER Messages to the window. In the OnTimer Function (The handler of WM_TIMER Message) Color key is set to white and Alpha-blend factor is varied from 0 to 255 and back to 0 to make the image appear and then disappear .


I really am thankful to all the articles written on transparent windows that I read before writing this one for CodeProject.


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
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralNo need for LoadLibrary/GetProcAddress Pin
raufatay9-Nov-08 17:30
memberraufatay9-Nov-08 17:30 
GeneralIf the bakground application is windows media player,it can not be transparent Pin
zseasoft13-Oct-07 23:24
memberzseasoft13-Oct-07 23:24 
GeneralThanks Pin
momomonet12-Mar-06 8:24
membermomomonet12-Mar-06 8:24 
QuestionFloodFill(...) doesn't work? Pin
qdzhulf16-Aug-05 18:04
memberqdzhulf16-Aug-05 18:04 
AnswerRe: FloodFill(...) doesn't work? Pin
Y-30330-Jan-06 7:53
memberY-30330-Jan-06 7:53 
Generalwin 2000 problem Pin
G.A.12-Jun-05 23:09
memberG.A.12-Jun-05 23:09 
AnswerRe: win 2000 problem Pin
Y-30330-Jan-06 7:00
memberY-30330-Jan-06 7:00 
GeneralStop saying in Win ME Pin
GreenEye2oo419-Mar-05 11:28
memberGreenEye2oo419-Mar-05 11:28 
JokeRe: Stop saying in Win ME (correction) Pin
Y-30330-Jan-06 6:38
memberY-30330-Jan-06 6:38 
Generalwhy SetLayeredWindowAttributes can't used in 8bits(256) color quality Pin
whizzkid@m28.hinet.net11-Aug-04 4:36
memberwhizzkid@m28.hinet.net11-Aug-04 4:36 
AnswerRe: why SetLayeredWindowAttributes can't used in 8bits(256) color quality Pin
Y-30330-Jan-06 6:44
memberY-30330-Jan-06 6:44 
GeneralRe: why SetLayeredWindowAttributes can't used in 8bits(256) color quality Pin
asrelu30-Nov-07 7:47
memberasrelu30-Nov-07 7:47 
GeneralTransparent in Rich Edit Control Pin
zphuong29-Jun-04 19:41
memberzphuong29-Jun-04 19:41 
GeneralChild windows Pin
Neo4E656F2-Feb-03 17:54
memberNeo4E656F2-Feb-03 17:54 
GeneralRe: Child windows Pin
Anonymous7-May-03 14:11
sussAnonymous7-May-03 14:11 
GeneralRe: Child windows Pin
KonMan9-May-03 10:35
memberKonMan9-May-03 10:35 
GeneralRe: Child windows Pin
peterburng726-Jun-05 17:13
memberpeterburng726-Jun-05 17:13 
GeneralSkinned windows Pin
M.Mugunth Kumar11-Dec-02 15:58
memberM.Mugunth Kumar11-Dec-02 15:58 
GeneralRe: Skinned windows Pin
Neo4E656F2-Feb-03 22:26
memberNeo4E656F2-Feb-03 22:26 
QuestionCan stop Mouse clicks .? Pin
DILLIP6-Nov-02 19:22
sussDILLIP6-Nov-02 19:22 
AnswerRe: Can stop Mouse clicks .? Pin
haha62425-Jan-05 3:40
susshaha62425-Jan-05 3:40 
QuestionCan stop Mouse clicks .? Pin
DILLIP6-Nov-02 19:21
sussDILLIP6-Nov-02 19:21 
GeneralWow!! Nice work!! Pin
Aisha Ikram18-Sep-02 19:53
memberAisha Ikram18-Sep-02 19:53 
GeneralNot in ME... Pin
Anonymous7-Aug-02 5:04
sussAnonymous7-Aug-02 5:04 
GeneralEasier in VB Pin
Anonymous4-Aug-02 12:39
sussAnonymous4-Aug-02 12:39 
In VB it is easy to make a window transparent.

Just use:
Form1.Visible = False

Poke tongue | ;-P
GeneralRe: Easier in VB Pin
Sonu_P4-Aug-02 18:32
memberSonu_P4-Aug-02 18:32 
GeneralRe: Easier in VB Pin
huangshaojun29-Jan-03 18:49
memberhuangshaojun29-Jan-03 18:49 
GeneralRe: Easier in VB Pin
Computrius6-Nov-03 14:08
sussComputrius6-Nov-03 14:08 

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
Web02 | 2.8.151126.1 | Last Updated 4 Aug 2002
Article Copyright 2002 by Quaker
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid