Click here to Skip to main content
11,414,234 members (69,814 online)
Click here to Skip to main content

Win2K transparent dialogs

, 20 Feb 2001
Rate this:
Please Sign up or sign in to vote.
How to make your windows transparent in Win2K without downloading the Platform SDK
<!-- Article image -->

Sample Image - win2k_transparent.gif

<!-- Main HTML starts here -->

This article shows how you can make your apps transparent using the new functions provided with Win2K. If you download the Platform SDK from Microsoft then these functions will be available, but those of you without fast Internet connections this article could be useful.

This is a mix of stuff I found on the net so if anyone feels that I have stolen something and should get the credit, sorry...

The functions you want are included in the USER32.DLL in Win2K, but the SDK provides the header files and the source code in libraries. But to use the functions one could just import the functions from the USER32.DLL. So here it goes...

First some constants must be declared:

#ifndef WS_EX_LAYERED
#define WS_EX_LAYERED           0x00080000
#define LWA_COLORKEY            0x00000001
#define LWA_ALPHA               0x00000002
#endif // ndef WS_EX_LAYERED

Then some declarations in the header-file:

// Preparation for the function we want to import from USER32.DLL
typedef BOOL (WINAPI *lpfnSetLayeredWindowAttributes)(HWND hWnd, 
                                  COLORREF crKey, BYTE bAlpha, DWORD dwFlags);

lpfnSetLayeredWindowAttributes m_pSetLayeredWindowAttributes

That is all for the header file, now to the implementation!

// Here we import the function from USER32.DLL
HMODULE hUser32 = GetModuleHandle(_T("USER32.DLL"));
m_pSetLayeredWindowAttributes = 
                       (lpfnSetLayeredWindowAttributes)GetProcAddress(hUser32, 
                       "SetLayeredWindowAttributes");

// If the import did not succeed, make sure your app can handle it!
if (NULL == m_pSetLayeredWindowAttributes)
	return FALSE; //Bail out!!!

If the function was imported correctly we must set the dialog we want to make transparent into "transparent-mode". E.G. Set the style for the dialog so that it can be transparent, and that is done with the flag WS_EX_LAYERED defined earlier.

// Check the current state of the dialog, and then add the 
// WS_EX_LAYERED attribute
SetWindowLong(m_hWnd, GWL_EXSTYLE, GetWindowLong(m_hWnd, GWL_EXSTYLE) 
              | WS_EX_LAYERED);

Now when that is done its time to describe the function we imported, and to tell you the truth I'm not 100% sure about all of the parameters...

hwnd [in] Handle to the layered window.

crKey [in] Pointer to a COLORREF value that specifies the transparency color key to be used. (When making a certain color transparent...)

bAlpha [in] Alpha value used to describe the opacity of the layered window. 0 = Invisible, 255 = Fully visible

dwFlags [in] Specifies an action to take. This parameter can be LWA_COLORKEY (When making a certain color transparent...) or LWA_ALPHA.

// Sets the window to 70% visibility.
m_pSetLayeredWindowAttributes(m_hWnd, 0, (255 / 70) * 100, LWA_ALPHA);

One thing you must make sure of is to disable this function if the app is running under any OS other then Win2K. And there is probably some very easy way to do that, but here is how I did it:

OSVERSIONINFO os = { sizeof(os) };
GetVersionEx(&os);
// use m_bWin2k before any call to the
// m_pSetLayeredWindowAttributes to make sure we are runninng Win2K
BOOL m_bWin2K = ( VER_PLATFORM_WIN32_NT == os.dwPlatformId && 
                  os.dwMajorVersion >= 5 ); 

That's about it!

License

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

Share

About the Author

Per-Erik Nordlund

United States United States
No Biography provided

Comments and Discussions

 
GeneralAnother problem with OpenGL Pin
zergus, 26-Sep-04 0:24
memberzergus26-Sep-04 0:24 
GeneralRe: Another problem with OpenGL Pin
Dennis Radon, 18-Jun-05 1:37
sussDennis Radon18-Jun-05 1:37 
GeneralRe: Another problem with OpenGL Pin
Dennis Radon, 18-Jun-05 1:57
sussDennis Radon18-Jun-05 1:57 
GeneralTransparency with OpenGL.. Pin
S.L, 20-Jun-04 21:24
memberS.L20-Jun-04 21:24 
GeneralRe: Transparency with OpenGL.. Pin
zergus, 26-Sep-04 0:08
memberzergus26-Sep-04 0:08 
GeneralRe: Transparency with OpenGL.. Pin
twxs256, 11-May-06 12:30
membertwxs25611-May-06 12:30 
Generala new problem Pin
beavis, 2-Nov-03 17:31
memberbeavis2-Nov-03 17:31 
GeneralRe: a new problem Pin
Anonymous, 29-Jun-04 4:55
sussAnonymous29-Jun-04 4:55 
GeneralCool .... But Pin
Powerpeeltz, 2-Jul-03 7:03
memberPowerpeeltz2-Jul-03 7:03 
GeneralCheck your math ! Pin
Kochise, 2-Oct-03 1:07
memberKochise2-Oct-03 1:07 
QuestionHow transparecy works? Pin
Lucidity, 3-May-02 20:17
memberLucidity3-May-02 20:17 
GeneralGetModuleHandle or LoadLibrary Pin
Anonymous, 1-Mar-02 2:25
memberAnonymous1-Mar-02 2:25 
GeneralRe: GetModuleHandle or LoadLibrary Pin
Tim Smith, 1-Mar-02 3:12
memberTim Smith1-Mar-02 3:12 
GeneralRe: GetModuleHandle or LoadLibrary Pin
happycujo, 12-Aug-03 4:50
memberhappycujo12-Aug-03 4:50 
GeneralOrignial state Pin
Anonymous, 21-Feb-02 9:02
memberAnonymous21-Feb-02 9:02 
GeneralRe: Orignial state Pin
Anonymous, 25-Feb-02 7:31
memberAnonymous25-Feb-02 7:31 
GeneralRe: Setting window orignial WS_EX_LAYERED state not working Pin
mediamaster40, 9-Sep-03 8:32
membermediamaster409-Sep-03 8:32 
Generalproblem Pin
woodey, 30-Jan-02 22:44
memberwoodey30-Jan-02 22:44 
GeneralJust realized what I was reading in your status window. Pin
Mike Whitenton, 2-Jul-01 23:51
memberMike Whitenton2-Jul-01 23:51 
GeneralRe: Just realized what I was reading in your status window. Pin
Steve McLenithan, 30-Nov-02 17:37
memberSteve McLenithan30-Nov-02 17:37 
GeneralTransparency vs. Windows Media Player Pin
Mac, 21-May-01 8:51
memberMac21-May-01 8:51 
GeneralRe: Transparency vs. Windows Media Player Pin
Per-Erik Nordlund, 21-May-01 21:28
memberPer-Erik Nordlund21-May-01 21:28 
GeneralRe: Transparency vs. Windows Media Player Pin
Mac, 22-May-01 5:12
memberMac22-May-01 5:12 
GeneralRe: Transparency vs. Windows Media Player Pin
Per-Erik Nordlund, 24-May-01 12:07
memberPer-Erik Nordlund24-May-01 12:07 
ok... I have tried it on my home computer now, and sure enough there is a gray box instead of transparance. The graphics card is a Geforce2, and the one at work (where the tránsparancy worked) was a Matrox...
GeneralRe: Transparency vs. Windows Media Player Pin
Till Toenshoff, 17-Dec-01 9:55
memberTill Toenshoff17-Dec-01 9:55 
GeneralRe: Transparency vs. Windows Media Player Pin
Dave O'Rourke, 18-Jun-01 6:31
memberDave O'Rourke18-Jun-01 6:31 
GeneralRe: Transparency vs. Windows Media Player Pin
Atif Mushtaq, 30-Jun-04 21:22
memberAtif Mushtaq30-Jun-04 21:22 
GeneralRe: Transparency vs. Windows Media Player Pin
Atif Mushtaq, 30-Jun-04 21:28
memberAtif Mushtaq30-Jun-04 21:28 
Generaldamn this looks sexy... Pin
lauren, 4-Mar-01 6:00
memberlauren4-Mar-01 6:00 
GeneralRe: damn this looks sexy... Pin
Nick Parker, 15-May-02 18:12
memberNick Parker15-May-02 18:12 
GeneralRe: damn this looks sexy... Pin
peterchen, 21-May-02 8:29
memberpeterchen21-May-02 8:29 
QuestionIs the API function used available on WinME? Pin
Bruce Duncan, 3-Mar-01 3:27
memberBruce Duncan3-Mar-01 3:27 
AnswerRe: Is the API function used available on WinME? Pin
Per-Erik Nordlund, 3-Mar-01 3:34
memberPer-Erik Nordlund3-Mar-01 3:34 
AnswerRe: Is the API function used available on WinME? Pin
Nikhil Dabas, 24-May-01 10:33
memberNikhil Dabas24-May-01 10:33 
GeneralDrawing frame Pin
Anonymous, 25-Feb-01 16:12
memberAnonymous25-Feb-01 16:12 
GeneralRe: Drawing frame Pin
Per-Erik Nordlund, 26-Feb-01 9:04
memberPer-Erik Nordlund26-Feb-01 9:04 
GeneralRe: Drawing frame Pin
Per-Erik Nordlund, 26-Feb-01 10:56
memberPer-Erik Nordlund26-Feb-01 10:56 
GeneralRe: Drawing frame Pin
TERACytE, 7-May-02 15:18
memberTERACytE7-May-02 15:18 
QuestionIs the os check necessary? Pin
Anonymous, 22-Feb-01 8:22
memberAnonymous22-Feb-01 8:22 
AnswerRe: Is the os check necessary? Pin
Per-Erik Nordlund, 22-Feb-01 9:35
memberPer-Erik Nordlund22-Feb-01 9:35 
GeneralRe: Is the os check necessary? Pin
anonymous, 26-Feb-01 18:30
memberanonymous26-Feb-01 18:30 
GeneralThanks! Just a small correction. Pin
Felix Rivera, 22-Feb-01 4:58
memberFelix Rivera22-Feb-01 4:58 
GeneralRe: Thanks! Just a small correction. Pin
PiP, 22-Feb-01 9:32
memberPiP22-Feb-01 9:32 
GeneralRe: Thanks! Just a small correction. Pin
Felix Rivera, 22-Feb-01 10:07
memberFelix Rivera22-Feb-01 10:07 
GeneralRe: Thanks! Just a small correction. Pin
Per-Erik Nordlund, 22-Feb-01 9:49
memberPer-Erik Nordlund22-Feb-01 9:49 
GeneralA demo proj would be nice! :) Pin
Anonymous, 21-Feb-01 21:15
memberAnonymous21-Feb-01 21:15 
GeneralRe: A demo proj would be nice! :) Pin
Per-Erik Nordlund, 22-Feb-01 10:18
memberPer-Erik Nordlund22-Feb-01 10:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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
Web04 | 2.8.150427.2 | Last Updated 21 Feb 2001
Article Copyright 2001 by Per-Erik Nordlund
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid