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

Win2K transparent dialogs

By , 20 Feb 2001
 

Sample Image - win2k_transparent.gif

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
Member
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   
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

GeneralRe: Transparency vs. Windows Media PlayermemberDave O'Rourke18 Jun '01 - 5:31 
Try disabling hardware acceleration. In WMP7 it's under Tools > Options > Performance tab. I haven't tried this myself, but I suspect that it will work.
 
Dave O'Rourke
Software Engineer
TechSmith Corporation
GeneralRe: Transparency vs. Windows Media PlayermemberAtif Mushtaq30 Jun '04 - 20:22 
now a days i am trying to do similar kind of stuff.
 
And got the same problems but the solutions to this problme is to set windowlessvideo property of windows media player.
 
After it window media player will directly render video into the client area.
but there is a worry that after setting this property consumes quite a bit of CPU cycle.(I dont know why)
 

Note:I am using windows media player 9 (tested on window 2000 and XP not sure about Window ME)
with nVidia Multi Monitor 128 MB VGA.

 
Unmanaged in a .NET world
GeneralRe: Transparency vs. Windows Media PlayermemberAtif Mushtaq30 Jun '04 - 20:28 
now a days i am trying to do similar kind of stuff.
 
And got the same problems but the solutions to this problme is to set windowlessvideo property of windows media player.
 
After it window media player will directly render video into the client area.
but there is a worry that after setting this property consumes quite a bit of CPU cycle.(I dont know why)
 

Note:I am using windows media player 9 (tested on window 2000 and XP not sure about Window ME)
with nVidia Multi Monitor 128 MB VGA.
 
Unmanaged in a .NET world
Generaldamn this looks sexy...memberlauren4 Mar '01 - 5:00 
could this be the ui of the future?
 
Suspicious | :suss:
 
---
"every year we invent better idiot proof systems and every year they invent better idiots"
GeneralRe: damn this looks sexy...memberNick Parker15 May '02 - 17:12 
lauren wrote:
every year we invent better idiot proof systems and every year they invent better idiots
 
So true, so true....Laugh | :laugh:
 
Nick Parker
GeneralRe: damn this looks sexy...memberpeterchen21 May '02 - 7:29 
lauren wrote:
could this be the ui of the future?
 
I hope not - too hard to read. However if it would come with an "fade to opaque" mouse over effect. or "look at" effect....
 

 

The earth is not dying. It is being killed.
QuestionIs the API function used available on WinME?memberBruce Duncan3 Mar '01 - 2:27 
I would check the user32.dll for SetLayeredWindowAttributes, but I don't have access to a WinME system.

AnswerRe: Is the API function used available on WinME?memberPer-Erik Nordlund3 Mar '01 - 2:34 
As far as I know, the SetLayeredWindowAttributes is NOT available on WinME. The API is only for Win2000 and above....
AnswerRe: Is the API function used available on WinME?memberNikhil Dabas24 May '01 - 9:33 
I checked user32.dll with dependency walker, and sure enough, the function is not there.
GeneralDrawing framememberAnonymous25 Feb '01 - 15:12 
Is there anyway of just drawing the client area alpha blended instead of the entire window? I'm guess you might have to use the LW_COLORREF to do the frame with a compatible DC. What gives?
 

GeneralRe: Drawing framememberPer-Erik Nordlund26 Feb '01 - 8:04 
I havent tried the LW_COLORREF yet... But i think you are on the right track....
The second parameter to SetLayeredWindowAttributes should point to a COLORREF that contains a RGB value.
If you figure it out please post it here telling how you did..
GeneralRe: Drawing framememberPer-Erik Nordlund26 Feb '01 - 9:56 
Ok... Got it...
Like this:
m_pSetLayeredWindowAttributes(m_hWnd, COLORREF RGB(255,255,255), 0, LWA_COLORKEY);
 
This will set all the black in the window to become transparent...
To make only the client area transparent one might set it to some odd color and making it transparent.
In a dialog based MFC program this can be done by adding this to the InitInstance in CXXXApp:
SetDialogBkColor(RGB(0, 0, 0), RGB(255, 255, 255));
This sets the background black, and the text white...
GeneralRe: Drawing framememberTERACytE7 May '02 - 14:18 
I have a similar problem, just reverse.
 
I want to hide the window frame ; either make it transparent or hide it entirely.   But I'
d like to keep the window contents just as they are.   My app uses CFrame and it is a layer ed app.   But I can only create transparency on the entire window, and not just the frame itself.
 
Can someone help me?
QuestionIs the os check necessary?memberAnonymous22 Feb '01 - 7:22 
Won't it just return NULL if you ask for SetLayeredWindowAttributes from a version of windows that doesn't have it? Sonebody correct me, will it return a random non-null pointer?
AnswerRe: Is the os check necessary?memberPer-Erik Nordlund22 Feb '01 - 8:35 
I dont know if the OS check really is needed, just thought better safe then sorry Smile | :)

GeneralRe: Is the os check necessary?memberanonymous26 Feb '01 - 17:30 
If you're doing an OS check, and you're not delay loading then it's not needed, as the program will crash under other Windows flavours anyhow. I have a program called Window Popper which allows me to put windows on top most from the status tray, and it delay loads MFC and so runs OK under W98, but has extra options for window transparency in Windows 2000.
 
Christian
 
P.S. - it appears that no matter what I type as my name, it tells me the name or email is taken. I tried Dammit, it's Christian - you can't tell me someone has that.
GeneralThanks! Just a small correction.memberFelix Rivera22 Feb '01 - 3:58 
Thanks for the article! Just a small correction...
 
In the following line, the transparency parameter is incorrect:
 
m_pSetLayeredWindowAttributes(m_hWnd, 0, (255 / 70) * 100, LWA_ALPHA);
 
the line should read:
 
m_pSetLayeredWindowAttributes(m_hWnd, 0, 255 * 70/100, LWA_ALPHA);
or
m_pSetLayeredWindowAttributes(m_hWnd, 0, 255 * 0.70, LWA_ALPHA);
 

GeneralRe: Thanks! Just a small correction.memberPiP22 Feb '01 - 8:32 
Why...?
GeneralRe: Thanks! Just a small correction.memberFelix Rivera22 Feb '01 - 9:07 
It is explained in the article that the third parameter of this function is the Alpha (or transparency level).
 
"...Alpha value used to describe the opacity of the layered window. 0 = Invisible, 255 = Fully visible...
 
m_pSetLayeredWindowAttributes(m_hWnd, 0, (255 / 70) * 100, LWA_ALPHA);
...
"
 
From that, it follows that 70% of 255 can be represented as
 
    255*(70/100)
 
or 
 
    255*0.70
 
rather than
 
   (255 / 70) * 100

GeneralRe: Thanks! Just a small correction.memberPer-Erik Nordlund22 Feb '01 - 8:49 
Of course... My mistake...
Thanx!
Blush | :O
GeneralA demo proj would be nice! :)memberAnonymous21 Feb '01 - 20:15 
Thank you. Smile | :)

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.130523.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