Add Transparent Forms in Delphi 5 and 6


Many commercial applications (mostly games) uses (semi) transparent forms to attract the users. Under Windows 2000 or better you can make your Delphi forms transparent by calling SetLayeredWindowAttributes with the desired constant alpha value and/or the color-key.
Note that the third parameter of SetLayeredWindowAttributes is a value that ranges from 0 to 255, with 0 making the window completely transparent and 255 making it completely opaque.

For using this function you have to add following code to your Delphi application:

const
WS_EX_LAYERED = $80000;
LWA_COLORKEY = 1;
LWA_ALPHA = 2;

type
TSetLayeredWindowAttributes = function (
hwnd : HWND; // handle to the layered window
crKey : TColor; // specifies the color key
bAlpha : byte; // value for the blend function
dwFlags : DWORD // action
): BOOL; stdcall;

procedure SetTransparentForm(AHandle : THandle; AValue : byte = 0);
var
Info: TOSVersionInfo;
SetLayeredWindowAttributes: TSetLayeredWindowAttributes;
begin
//Check Windows version
Info.dwOSVersionInfoSize := SizeOf(Info);
GetVersionEx(Info);
if (Info.dwPlatformId = VER_PLATFORM_WIN32_NT) and
(Info.dwMajorVersion >= 5) then
begin
SetLayeredWindowAttributes := GetProcAddress(GetModulehandle(user32), 'SetLayeredWindowAttributes');
if Assigned(SetLayeredWindowAttributes) then
begin
SetWindowLong(AHandle, GWL_EXSTYLE, GetWindowLong(AHandle, GWL_EXSTYLE) or WS_EX_LAYERED);
//Make form transparent
SetLayeredWindowAttributes(AHandle, 0, AValue, LWA_ALPHA);
end;
end;
end;

Now you can create OnCreate event handler and put next code:

procedure TForm1.FormCreate(Sender: TObject);
begin
SetTransparentForm(Handle, 100);
end;

Run the application. Just remember, that SetLayeredWindowAttributes
function not available for Windows 98 or Windows NT 4.0
If you want to setup transparency in percents you can use following formula: (255 * Percents) / 100.

You don't need to write a lot of code to fade a window in or out.
The following example shows how to create fade out effect.
First you form become slowly transparent and after disappear from screen completely.

procedure TForm1.Button1Click(Sender: TObject);
var I : integer;
begin
for i := 100 downto 0 do
begin
SetTransparentForm(Handle,i);
Application.ProcessMessages;
end;
Close;
end;

It is also possible to make a transparent standard Windows dialog like TFontDialog. To have a dialog box come up as a translucent window,
first create the dialog as usual. Then, create OnShow event handler and make your dialog transparent in the same way as a form.

procedure TForm1.FontDialogShow(Sender: TObject);
begin
SetTransparentForm(FontDialog.Handle, 100);
end;

You can use an examples from this article in Delphi 5 as well as in Delphi 6.
Related Discussions
  • HOW TO KILL ANYOTHER PROGRAMS WITH DELPHI? (2001-01-02 19:04:42)
    Find the handle of the window using FindWindow or EnumWindows and use PostMessage(ProgramHandle, WM_CLOSE, 0, 0); I have not tried it, but it...
  • HOW TO GREP CHARACTERS IN DELPHI 3 (2001-01-03 03:29:32)
    Var Grep : String; Begin Grep := Copy('2000-46',6,2); End; The first parameter is a string or a dynamic array, the second is the index of...
  • HOW CAN I DEBUG DLL (2001-01-03 02:48:24)
    Do you mean how to find internal errors while writing a DLL file or how to get the exact error while calling a DLL Function ? /Filip
  • USING DLLS (2001-01-04 01:47:50)
    Hi again Goober ... :o) Let's take NETAPI32.DLL as an example. This DLL is created by Microsoft and the explanation of its use is described in...
  • DOT ON SCREEN (2001-01-03 05:42:55)
    Delphi does not support a Pixel function with a HDC, you have to use the Windows-API-function COLORREF SetPixel( HDC hdc, // handle...
  • HOW TO RUN DATABASE APPLICATION ON THE INTERNET (2017-06-09 10:54:22)
    Well, in design I think there is already an issue. Backend is more then just a database with which you connect. There should be a complete layer...
  • HELP WITH CHDIR !!!! TURBO PASCAL VS DELPHI (2001-01-03 08:48:49)
    This is because the ChDir Procedure, the SetCurrentDir and SetCurrentDirectory Functions are changing the directory in the active process. Try...
  • FRIENDS IN DELPHI ?? (OR OTHER ACCESS WORKAROUNDS) (2001-01-04 11:08:37)
    One of the quirks of Delphi is that classes in the same unit can access protected and private methods and variables. Borland does this a lot in...
  • SENDING ICQ MSGS FROM DELPHI (2001-01-03 16:54:03)
    I tried the ICQ Api, but never used it. I've added a simple ICQPager component to delphipages (Winsock/TCPIP).. it might help... Q
  • RUNNING A DATABASE APPLICATION ON ANOTHER COMPUTER (2001-01-07 07:36:33)
    In the BDE on the other PC (the one without the database), you must add an alias to point to the location of the database in the format of:...
Latest News
Submit News Form Past News
Latest Forum Entries