Title: Using Windows 2000 transparency effects.
Question: Windows 2000 has a new transparency graphical effect that can be used in your applications (just look at the way Windows 2000 menus fade in and out) - it`s just a case of knowing how.
Answer:
If you have used Windows 2000, you will have noticed the new tranparency effects that are present in the OS (just look at the way the menus fade in and out) - but how do you use these effects in your delphi programs? It is surprisingly easy to use transparency effects on your forms, but Delphi doesn't define the required constants and function calls for you (it came out before Windows 2000).
There are two distinct effects that can be used - firstly you can make an entire form transparent, and second you can make a specific colour transparent. I will show an example of each of these effects below, but first an overview of the process.
To make use of the new transparent effects in Windows 2000 just requires three function calls. We use GetWindowLong to get the a forms extended style information:
ExtStyle := GetWindowLong(Handle, GWL_EXSTYLE);
We then have to use a new constant, WS_EX_LAYERED, which we have to define ourselves:
WS_EX_LAYERED = $00080000;
The value that is returned from GetWindowLong is ORed with WS_EX_LAYERED to tell Windows 2000 to turn on the layering (the 'proper' name for the transparency effect):
SetWindowLong(Handle, GWL_EXSTYLE, ExtStyle or ES_EX_LAYERED);
If we just left it at that the form would be completely invisible! So we need to set the level of transparency. This is done by calling SetLayeredWindowAttributes, which we have to define ourselves as it is not present in Delphi's libraries:
function SetLayeredWindowAttributes(hwnd : HWND; crKey : COLORREF; bAlpha : BYTE; dwFlags : DWORD) : boolean; stdcall; external user32 name 'SetLayeredWindowAttributes';
It looks confusing but take some time to read through and it all makes sense. This is simply declaring a function that is present in one of Windows DLL's.
The parameter dwFlags sets whether we are making the form transparent, making a colour transparent or both. We need two new constants for this:
LMA_COLORKEY = $00000001;
LMA_ALPHA = $00000002;
LMA_COLORKEY is used to make a certain colour transparent. LMA_ALPHA makes the form transparent. You can OR them together to have both effects working at the same time.
The parameter crKey is the value of the colour to make transparent, use the RGB function to set this.
bAlpha is the level of transparency for the form, in the range 0 to 255 (with 0 being invisible).
Finally, hwnd is the handle of the form.
If all this has been a bit overwhelming, then the example below will clear it up for you. Create a new application in Delphi and then place a TSHAPE component on the form (from the 'Additional' palette). Set the brush color for the TSHAPE in the object inspector to CLBLACK.
Once this is done create an ONCREATE event for the form and cut and paste the code below.
procedure TForm1.FormCreate(Sender: TObject);
var
ExtStyle : LongInt;
begin
ExtStyle := GetWindowLong(Handle, GWL_EXSTYLE);
SetWindowLong(Handle, GWL_EXSTYLE, ExtStyle or WS_EX_LAYERED);
SetLayeredWindowAttributes(Handle, RGB(0,0,0), 150, LMA_ALPHA or LMA_COLORKEY);
end;
Now in the unit, declare these new constants (cut and paste them above the IMPLEMENTATION keyword).
const
WS_EX_LAYERED = $00080000;
LMA_COLORKEY = $00000001;
LMA_ALPHA = $00000002;
Finally, just below the implementation keyword cut and paste this decalaration:
function SetLayeredWindowAttributes(hwnd : HWND; crKey : COLORREF; bAlpha : BYTE; dwFlags : DWORD) : boolean; stdcall; external user32 name 'SetLayeredWindowAttributes';
When you run the program, you should see a transparent form with a hole in it! Experiment with the value 150 in the call to SetLayeredWindowAttributes to change the level of transparency for the form. RGB(0,0,0) sets the colour black to be transparent, and creates the hole in the form where the TSHAPE is.
An obvious point, but one that should be mentioned, this code will only work on Windows 2000 and will create an access violation in any other version of Windows.
Have fun with this effect!