You may want to create a transparent form (for example: for a splash screen or for an About box) but you don’t know how. Well, there are 2 ways: for Windows 2000/XP and for the rest. First I’ll describe the non-Windows API version:
1. You can use this trick for making cool splash forms. Just add a TImage component to the form and set the form’s BorderStyle to bsNone and it’s Position property to poScreenCenter. Center the image in the form and add this code. This will make only the image visible (the image preserves it’s transparency!).
interface
...
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
protected
procedure CreateParams(var Params: TCreateParams); override;
end;
...
implementation
...
function SetLayeredWindowAttributes;
external 'user32.dll' name 'SetLayeredWindowAttributes';
...
procedure TForm1.CreateParams(var Params: TCreateParams);
begin
inherited;
Params.ExStyle := WS_EX_TRANSPARENT;
// Otherwise the form will not be repainted after a different window covers it
end;
// This is the main code that makes the windows transparent
procedure TForm1.FormCreate(Sender: TObject);
begin
Brush.Style := bsClear;
BorderStyle := bsNone;
end;
2. …and finally the code for Windows 2000/XP. This is a bit different version of
transparency due to the fact that these windows can have there transparency set.
They don’t have to be fully transparent.
interface
...
type
TForm1 = class(TForm)
...
procedure FormCreate(Sender: TObject);
protected
...
procedure CreateParams(var Params: TCreateParams); override;
end;
...
function SetLayeredWindowAttributes(
Handle: Windows.HWND; // Handle to the layered window
crKey: COLORREF; // Specifies the color key
bAlpha: Byte; // Value for the blend function
dwFlags: DWORD // Action
): BOOL; stdcall;
...
implementation
...
procedure TForm1.CreateParams(var Params: TCreateParams);
begin
inherited;
Params.ExStyle := Params.ExStyle or $00080000; // WS_EX_LAYERED
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
SetLayeredWindowAttributes(Handle, 0, 230, $00000002); // LWA_ALPHA
...
end;
Tags: brush style, color key, different window, dll name, implementation procedure, interface type, override, position property, procedure tform1, timage component, transparency, transparent interface, windows 2000 xp, windows api, windows transparent
Merlin’s Delphi Forge
Leave a comment