Mega Code Archive

 
Categories / Delphi / Forms
 

Using AlphaBlending on Forms with Delphi 5

Title: Using AlphaBlending on Forms with Delphi 5 Question: There are new properties on Delphi 6 Forms that allows to use alpha blending options. How to do it on Delphi 5? Answer: To enable alpha blending on Delphi 5 TForms, just use the following lines: ----------%unit Unit1; interface type TForm1 = class(TForm) ... members here ... public constructor Create(AOwner : TComponent); override; end; implementation {------------------------------------------------------- ALPHA_VALUE determines the level of transparency. 0 = full transparency, 255 = opaque --------------------------------------------------------} const ALPHA_VALUE = 128; {------------------------------------------------------- Missing Win32 Constants & Functions -------------------------------------------------------} const WS_EX_LAYERED = $00080000; const LWA_ALPHA = 2; function SetLayeredWindowAttributes(Handle : HWND; crKey : TColor; bAlpha : Byte; dwFlags : DWORD) : Boolean; stdcall; external 'user32.dll'; constructor TForm1.Create(AOwner: TComponent); var vi : _OSVERSIONINFO; oldStyle : DWORD; begin inherited Create(AOwner); GetVersionEx(vi); if vi.dwMajorVersion = 5 then { Win2000 or superior?! } begin oldStyle := GetWindowLong(Self.Handle, GWL_EXSTYLE); SetWindowLong(Self.Handle, GWL_EXSTYLE, oldStyle or WS_EX_LAYERED); SetLayeredWindowAttributes(Self.Handle, clBlack, ALPHA_VALUE, LWA_ALPHA); RedrawWindow(Self.Handle, nil, 0, RDW_ERASE or RDW_INVALIDATE or RDW_FRAME or RDW_ALLCHILDREN); end; end; end. ----------% Notice that this will work only on Win2k or XP.