Mega Code Archive

 
Categories / Delphi / Forms
 

How to AlphaBlend your forms with a component!

Title: How to AlphaBlend your forms with a component! Question: Do you like the AlphaBlending of Windows 2000 menus? Use that and you'll implement in your applications. Answer: Unit uAlphaWindow; Interface Uses Windows, Messages, Classes, Controls, Forms; Type TAlphaPercent = 0..100; TAlphaWindow = Class( TComponent ) Protected User32 : HModule; Public Constructor Create( AOwner : TComponent ); OverRide; Destructor Destroy; OverRide; Procedure SetTransparentHWND( HWnd : THandle; Percent : TAlphaPercent ); Procedure SetTransparent( Percent : TAlphaPercent ); Procedure SetOpaqueHWND( HWnd : THandle ); Procedure SetOpaque; End; Procedure Register; Implementation Const LWA_ALPHA = $2; GWL_EXSTYLE = -20; WS_EX_LAYERED = $80000; WS_EX_TRANSPARENT = $20; Var SetLayeredWindowAttributes : Function( HWnd : LongInt; crKey : Byte; bAlpha : Byte; dwFlags : LongInt ) : LongInt; StdCall; Constructor TAlphaWindow.Create( AOwner : TComponent ); Begin Inherited; User32 := LoadLibrary( 'USER32.DLL' ); If ( User32 0 ) Then @SetLayeredWindowAttributes := GetProcAddress( User32, 'SetLayeredWindowAttributes' ) Else SetLayeredWindowAttributes := NIL; End; Destructor TAlphaWindow.Destroy; Begin If ( User32 0 ) Then FreeLibrary( User32 ); Inherited; End; Procedure TAlphaWindow.SetOpaqueHWND( HWnd : THandle ); Var Old : THandle; Begin If ( IsWindow( HWnd ) ) Then Begin Old := GetWindowLongA( HWnd, GWL_EXSTYLE ); SetWindowLongA( HWnd, GWL_EXSTYLE, Old And ( ( Not 0 ) - WS_EX_LAYERED ) ); End; End; Procedure TAlphaWindow.SetOpaque; Begin Self.SetOpaqueHWND( ( Self.Owner As TWinControl ).Handle ); End; Procedure TAlphaWindow.SetTransparentHWND( HWnd : THandle; Percent : TAlphaPercent ); Var Old : THandle; Begin If ( ( User32 0 ) And ( Assigned( SetLayeredWindowAttributes ) ) And ( IsWindow( HWnd ) ) ) Then If ( Percent = 0 ) Then SetOpaqueHWND( HWnd ) Else Begin Percent := 100 - Percent; Old := GetWindowLongA( HWnd, GWL_EXSTYLE ); SetWindowLongA( HWnd, GWL_EXSTYLE, Old Or WS_EX_LAYERED ); SetLayeredWindowAttributes( HWnd, 0, ( 255 * Percent ) Div 100, LWA_ALPHA ); End; End; Procedure TAlphaWindow.SetTransparent( Percent : TAlphaPercent ); Begin Self.SetTransparentHWND( ( Self.Owner As TForm ).Handle, Percent ); End; Procedure Register; Begin RegisterComponents( 'Christian', [ TAlphaWindow ] ); End; End.