Layered Windows Tutorial by ultraschall |
Windows 2000 users will probably have noticed the support of a cool transparency effect. It is, of course, possible to use this effect in your programs and the goal of this tutorial is to demonstrate the reader how to achieve this. Microsoft calls all this stuff "layering", so that's why i have chosen that title. This is rather a short tutorial because it's really very very easy to make your windows transparent ;) |
Well, the first step to make a window transparent is to create it with the WS_EX_LAYERED extended window style. It is also possible to apply this style to the window after it has been created. To apply the style use the SetWindowLong function. Please note that you cannot use this style for child windows. That means that you cannot make a single button/edit/static/etc. in a window transparent, but the whole window. All child controls will also become transparent automatically. |
Windows 2000 offers two different effects: Creating a window with the WS_EX_LAYERED style, however, is not enough. All windows with this style will remain invisible until you set the level of transparency with the SetLayeredWindowAttributes function:
Well, the first parameter is obvious. You have to pass the handle of the window that has the WS_EX_LAYERED style to the function. The second parameter tells Windows which color you want to make transparent.
If you look up COLORREF you'll find out that this parameter is
a dword value with the following form: 0x00bbggrr.
I guess no further explanation is needed :) The third parameter specifies the transparency level of the entire window.
Set this parameter to 0 to make a window completely transparent or 255
to make it opaque. The dwFlags parameter specifies the action(s) to take. This parameter can be either LWA_COLORKEY or LWA_ALPHA or both. If you just want to make a single color in a window transparent specify LWA_COLORKEY. If you want to make an entire window transparent set dwFlags to LWA_ALPHA. To use both effects set it to LWA_COLORKEY + LWA_ALPHA. |
Now let's have a look on an example: I've created a dialog box which contains several controls including a checkbox. The windows becomes transparent if you select it and opaque if you deselect it. Here's the .rc file: (if you don't know what this is go and read Iczelion's great tutorials) #define DS_MODALFRAME 0x80L
Use rc.exe to compile it to a .res file. And now the .asm source: .586 GetModuleHandleA PROTO :DWORD WM_INITDIALOG equ 110h .DATA User32 db
'User32.dll',0 .DATA? pSLWA dd ? ; Offset of SetLayeredWindowAttributes will be saved here .CODE DlgProc PROC hWnd: DWORD,uMsg: DWORD,wParam:
DWORD,lParam: DWORD ; Get
current EX style of window and add WS_EX_LAYERED TurnOff:
.ELSE Start: END Start ENDS Assemble and link with: ml /c /coff /Cp filename.asm |
That was easy, huh? Now time for some greets: |