Click here to Skip to main content
Click here to Skip to main content

Win2K transparent dialogs

By , 20 Feb 2001
 
<!-- 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

About the Author

Per-Erik Nordlund

United States United States
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralAnother problem with OpenGLmemberzergus25-Sep-04 23:24 
I'm using graphics engiene Vega Prime and need to run another window above it with some graphics, which cutted by using SetLayeredWindowAttributes() with LWA_COLORKEY. The problem is that Layered window is flickering. I've tried it with DirectX and it works fine, so seems like OpenGL rendering of Vega is the origin of the problem. I understand, that it's drawing in different levels (i.e. soft & hardware rendering), but in Direct3D it somehow works completly clear.
If someone can say something useful related to this topic - I'll happy to hear.
GeneralRe: Another problem with OpenGLsussDennis Radon18-Jun-05 0:37 
I have the same problem and I figured out, that moving the layered window around the desktop isn't also that nice. It leaves the rendered picture on the same place (on the app in the background - desktop for example) and the content of the window is transparent (even if I don't render in the transparent layercolor).
GeneralRe: Another problem with OpenGLsussDennis Radon18-Jun-05 0:57 
Have you set WNDCLASS.hbrBackground != NULL? This causes flickering in my program, too. Buth there's still the problem, that I can't draw the window around Frown | :-(
GeneralTransparency with OpenGL..memberS.L20-Jun-04 20:24 
I tried to use the SetLayeredWindowAttributes() with an openGL window, but it doesn't work! The OpenGl window doesn't display anymore, but the transparency works fine with what remains of the dialog..
What might be very interesting, if it is possible to do, is to make for example a 3D cube (openGL cube), that spins on the desktop not inside a window. Of course, that's a possibility, but we can have a lot of fun applications..
 
Does anyone know if that's possible? can anyone provide a simple example?
 
Thanks a lot.
WTF | :WTF:
 
Satchel
GeneralRe: Transparency with OpenGL..memberzergus25-Sep-04 23:08 
It's Ok. The function SetLayeredWindowAttributes(), working on GDI level, and can not affect as you expect OpenGL context, which drown by hardware. Possible solution is drawing OpenGL in the buffer, reading of that buffer to array, and drawing using standard Windows GDI. Something like this I've already wrote for fun. It works good, but slow.
GeneralRe: Transparency with OpenGL..membertwxs25611-May-06 11:30 
I have also try, using 2 ways. the first one is to do a render to bitmap but it very slow. The second, using a pbuffer is faster. The framerate is not incredible but sufficient for interactive purpose in screen resolution.
you can try it with my mp3 player available
here
Generala new problemmemberbeavis2-Nov-03 16:31 
I failed to set a child window to GWL_EX_LAYERED attributes, so is GWL_EX_LAYERED only valid to main window?
GeneralRe: a new problemsussAnonymous29-Jun-04 3:55 
MSDN: Note that this cannot be used for child windows. Also, this cannot be used if the window has a class style of either CS_OWNDC or CS_CLASSDC.
GeneralCool .... ButmemberPowerpeeltz2-Jul-03 6:03 
It looks fine but...
 
-> // Sets the window to 70% visibility.
-> m_pSetLayeredWindowAttributes(m_hWnd, 0, (255 / 70) * 100, LWA_ALPHA);
 
Thats not 70% !!!!!!
 
It must be ....
m_pSetLayeredWindowAttributes(m_hWnd, 0, (255 / 100) * 70, LWA_ALPHA);
 

GeneralCheck your math !memberKochise2-Oct-03 0:07 
255 * 0.7 = 178.5
 
(255 / 100) * 70 = 140 // Don't forget you're working with integer !
(255 * 70) / 100 = 178
 
Kochise
 
In Code we trust !
QuestionHow transparecy works?memberLucidity3-May-02 19:17 
I saw the posts on trans and WMP and it got me curious. How does windows handle the overlay? I know WMP is a special case because it may in fact be changing bit in video memory, but what about other windows the use GDI? For example if I have a window with animation and a transparent window over that does the Windows(tm) render process invalidate any windows that are under a transparent window or is there some virtual bitmap of the window available in memory that is used to blend drawing?
 

GeneralGetModuleHandle or LoadLibrarymemberAnonymous1-Mar-02 1:25 
Hello, is there a difference between :
 
HMODULE hUser32 = GetModuleHandle(_T("USER32.DLL"));
m_pSetLayeredWindowAttributes = (lpfnSetLayeredWindowAttributes)GetProcAddress(hUser32, "SetLayeredWindowAttributes");
 
(code in this article)
 
and
 
HINSTANCE hLibrary = LoadLibrary("USER32.DLL");
if (!hLibrary)
   return FALSE;
 
pSetLayeredWindowAttributes = (lpfnSetLayeredWindowAttributes)GetProcAddress(hLibrary, "SetLayeredWindowAttributes");
 
FreeLibrary(hLibrary);
 
Which methode is better ?
Thanks !
GeneralRe: GetModuleHandle or LoadLibrarymemberTim Smith1-Mar-02 2:12 
GetModuleHandle will not load the library if it isn't already in the address space. It also doesn't increment the reference count on the library.
 
Tim Smith
 
I know what you're thinking punk, you're thinking did he spell check this document? Well, to tell you the truth I kinda forgot myself in all this excitement. But being this here's CodeProject, the most powerful forums in the world and would blow your head clean off, you've got to ask yourself one question, Do I feel lucky? Well do ya punk?
GeneralRe: GetModuleHandle or LoadLibrarymemberhappycujo12-Aug-03 3:50 
Does it mean when I am done using this, I should NOT Free the handle using FreeLibrary?
 
Quick sample:
 
// Does not increment the library reference count ?
HMODULE hUser32 = GetModuleHandle(_T("USER32.DLL"));
m_pSetLayeredWindowAttributes = (lpfnSetLayeredWindowAttributes)GetProcAddress(hUser32, "SetLayeredWindowAttributes");
 
// decrement the library reference count
if (hUser32)
FreeLibrary(hUser32);
 
Dead | X| Dead | X|

GeneralOrignial statememberAnonymous21-Feb-02 8:02 
Hi,
 
I use
 
SetWindowLong(hWnd, GWL_EXSTYLE, GetWindowLong(hWnd, GWL_EXSTYLE) | WS_EX_LAYERED);
 
and m_pSetLayeredWindowAttributes(hWnd, 0, 255 * 0.5, LWA_ALPHA);
 
to make a window transparent. But when I set
 
m_pSetLayeredWindowAttributes(hWnd, 0, 255, LWA_ALPHA);
 
to remove the transparancy, the drawing of the window is very slow... is there a there way to set the original state of the window ? or to remove the WS_EX_LAYERED style (I tried SetWindowLong(hWnd, GWL_EXSTYLE, GetWindowLong(hWnd, GWL_EXSTYLE) | ~WS_EX_LAYERED); but it didn't work).
 
Thanks for help !
GeneralRe: Orignial statememberAnonymous25-Feb-02 6:31 
try SetWindowLong(hWnd, GWL_EXSTYLE, GetWindowLong(hWnd, GWL_EXSTYLE) & ~WS_EX_LAYERED)
GeneralRe: Setting window orignial WS_EX_LAYERED state not workingmembermediamaster409-Sep-03 7:32 
yes I have been having the same problem and am trying everything to get it to work but it just wont take. using
 
SetWindowLong(hWnd, GWL_EXSTYLE, GetWindowLong(hWnd, GWL_EXSTYLE) & ~WS_EX_LAYERED)
 
should clear the WS_EX_LAYERED flag, but the window still renders very slowly as if it were layered, i have also tried calling
 
SetWindowPos(g_hwndWindow, HWND_TOP, 0,0,0,0, SWP_FRAMECHANGED|SWP_NOMOVE|SWP_NOOWNERZORDER|SWP_NOREPOSITION|SWP_NOSENDCHANGING|SWP_NOSIZE|SWP_NOZORDER);
RedrawWindow(hwnd, NULL, NULL, RDW_ERASE | RDW_INVALIDATE | RDW_FRAME | RDW_ALLCHILDREN);
 
to flush the windowlong flags into the system but it doesn't do anything. the only crutch i have found is to just create a new window (without the layered style) over the old one and swap it out when my blending is done, but this is very ugly and undesirable as you see quick flash of the new window being created.
 
does anyone else have any ideas on how to fix this issue
Generalproblemmemberwoodey30-Jan-02 21:44 
if I change the color of dialog
 
how can i let it trans
GeneralJust realized what I was reading in your status window.memberMike Whitenton2-Jul-01 22:51 
Dude! Get some smaller units or a faster PC! Sheesh! Poke tongue | ;-P
GeneralRe: Just realized what I was reading in your status window.memberSteve McLenithan30-Nov-02 16:37 
SETI@HOME rules![^^^]
 
********************
* SteveMcLenithan
* steve@steve-mac.com
* http://steve-mac.com
********************
GeneralTransparency vs. Windows Media PlayermemberMac21-May-01 7:51 
(This is a slightly edited cross-post of my question at http://www.codeproject.com/useritems/trans.asp?&forumid=957&select=31147#xx31147xx)
 
Bernhard Hammer provides a neat sample program showing transparency at http://www.codeproject.com/useritems/trans.asp (thanks Bernhard!).
 
I have need to build translucent windows like this in my application. The sample works great in all the places I need except the most important for my program. It does not work properly with Windows Media Player. I'm running version 7.00.00.1956, but can run another version if that would help. Of course, I'm running on Win2000.
 
What I need is a window that is about 50% translucent, floating above the Windows Media Player (WMP), so the running movie shows through. When WMP is running in its browser-like mode, it shows through the overlaying translucent window just fine. Once I start playing a video in WMP, the rectangle of the video goes dark in the overlaying window. I've tried this playing an MPEG and an AVI, with similar results in both cases.
 
For my application, I don't need to make the WMP window itself translucent. But, as an additional data point, if you use Bernhard Hammer's sample application to set the WMP translucent, WMP itself turns the rectangle of the playing video dark. And, once you quit WMP, you get some odd remnants left over on the screen.
 
So, does anyone know how to make a translucent window that overlays WMP and properly shows WMP's playing video through the overlaying window? Is there something special I need to do in my use of WMP? I'm using WMP as an Active X control in my MFC Visual C++ application.

GeneralRe: Transparency vs. Windows Media PlayermemberPer-Erik Nordlund21-May-01 20:28 
Hi.
I tried to use windows media player (same version as you) with the transparancy code provided in the article, and it seamed to work...
But how it will act when WMP is an ActiveX is another story... Havent tested... Maybe its a video driver issue?
On my home computer (the above I tried on my work computer with win2000) I'm running whistler beta and there I have some problem with WMP and transparency. Havent tried with my program, but MWP has som own transparacy stuff that fades away play-controls when running in fullscren. The transparay there flickers. That started when I upgraded to the newest Nvidia drivers....
GeneralRe: Transparency vs. Windows Media PlayermemberMac22-May-01 4:12 
My application is using WMP via ActiveX, but my tests were also done with Microsoft's stand-alone WMP player. I had the same solid-gray-box results there.
 
One theory I've heard is WMP is using DirectShow, writing the video directly to hardware. So the Windows graphic space has no pixels in the video to alpha blend with the overlaying translucent window.
 
If this is correct, I suppose it's possible you could get different behavior on different hardware.
 
I don't claim to be an expert in this area - just bumbling my way through. Smile | :)
GeneralRe: Transparency vs. Windows Media PlayermemberPer-Erik Nordlund24-May-01 11: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 PlayermemberTill Toenshoff17-Dec-01 8:55 
Hi Guyz,
 
The WMP (through DS) uses various techniques to display videos. The options depend very much on your video-card (e.g. Overlay-Mode).
 
Transparency for Movies (if thats what you want) is not possible with Windows2k (unless you have very advanced video-drivers) or you provide a plugin (rendering filter) that displays the movie using standard GDI-BitBlts on a standard DC (slow and ugly).
 
Alternative1:
Provide a filter that emulates transparency for direct show - that could be done and the results do not have to be ugly or slow since you dont need to replace the rendering filter. You could supply an additional transformation filter. But, thats some work to do since its not too easy writing such filter... DS for 2K doesnt provide on the fly mixing, you would have to create it on your own - wouldnt try.
 
Alternative2:
Use WindowsXP and its DS features. DS for XP provides on the fly mixing. Its very handy and requires just a few lines of code for you to mix a video with a bitmap. Thats how I would emulate transparency for a video.
 
Hope that helps...
 
Greetings,
Till

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130617.1 | Last Updated 21 Feb 2001
Article Copyright 2001 by Per-Erik Nordlund
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid