Aero Glass

From Free Pascal wiki
Revision as of 02:42, 8 July 2011 by Lainz (talk | contribs) (→‎Bugs)
Jump to navigationJump to search

Deutsch (de) English (en) español (es) polski (pl)

Aero Glass effect on Lazarus Form

aero glass lazarus.png

First save the above code to a text file "glass.pas":

<delphi>unit glass;

{$mode delphi} //{$mode objfpc}{$H+}

interface

uses

 Windows, Forms, Graphics;

type

 _MARGINS = packed record
   cxLeftWidth    : Integer;
   cxRightWidth   : Integer;
   cyTopHeight    : Integer;
   cyBottomHeight : Integer;
 end;
 PMargins = ^_MARGINS;
 TMargins = _MARGINS;
 DwmIsCompositionEnabledFunc      = function(pfEnabled: PBoolean): HRESULT; stdcall;
 DwmExtendFrameIntoClientAreaFunc = function(destWnd: HWND; const pMarInset: PMargins): HRESULT; stdcall;
 SetLayeredWindowAttributesFunc   = function(destWnd: HWND; cKey: TColor; bAlpha: Byte; dwFlags: DWord): BOOL; stdcall;

const

 WS_EX_LAYERED = $80000;
 LWA_COLORKEY  = 1;

procedure GlassForm(frm: TForm; tmpMargins: TMargins; cBlurColorKey: TColor = clFuchsia); function WindowsAeroGlassCompatible: Boolean;

implementation

function WindowsAeroGlassCompatible: Boolean; var

 osVinfo: TOSVERSIONINFO;

begin

 ZeroMemory(@osVinfo, SizeOf(osVinfo));
 OsVinfo.dwOSVersionInfoSize := SizeOf(TOSVERSIONINFO);
 if (
 (GetVersionEx(osVInfo)   = True) and
 (osVinfo.dwPlatformId    = VER_PLATFORM_WIN32_NT) and
 (osVinfo.dwMajorVersion >= 6)
 )
 then Result:=True
 else Result:=False;

end;

procedure GlassForm(frm: TForm; tmpMargins: TMargins; cBlurColorKey: TColor = clFuchsia); var

 hDwmDLL: Cardinal;
 fDwmIsCompositionEnabled: DwmIsCompositionEnabledFunc;
 fDwmExtendFrameIntoClientArea: DwmExtendFrameIntoClientAreaFunc;
 fSetLayeredWindowAttributesFunc: SetLayeredWindowAttributesFunc;
 bCmpEnable: Boolean;
 mgn: TMargins;

begin

 { Continue if Windows version is compatible }
 if WindowsAeroGlassCompatible then begin
   { Continue if 'dwmapi' library is loaded }
   hDwmDLL := LoadLibrary('dwmapi.dll');
   if hDwmDLL <> 0 then begin
     { Get values }
     @fDwmIsCompositionEnabled        := GetProcAddress(hDwmDLL, 'DwmIsCompositionEnabled');
     @fDwmExtendFrameIntoClientArea   := GetProcAddress(hDwmDLL, 'DwmExtendFrameIntoClientArea');
     @fSetLayeredWindowAttributesFunc := GetProcAddress(GetModulehandle(user32), 'SetLayeredWindowAttributes');
     { Continue if values are <> nil }
     if (
     (@fDwmIsCompositionEnabled <> nil) and
     (@fDwmExtendFrameIntoClientArea <> nil) and
     (@fSetLayeredWindowAttributesFunc <> nil)
     )
     then begin
       { Continue if composition is enabled }
       fDwmIsCompositionEnabled(@bCmpEnable);
       if bCmpEnable = True then begin
         { Set Form Color same as cBlurColorKey }
         frm.Color := cBlurColorKey;
         { ... }
         SetWindowLong(frm.Handle, GWL_EXSTYLE, GetWindowLong(frm.Handle, GWL_EXSTYLE) or WS_EX_LAYERED);
         { ... }
         fSetLayeredWindowAttributesFunc(frm.Handle, cBlurColorKey, 0, LWA_COLORKEY);
         { Set margins }
         ZeroMemory(@mgn, SizeOf(mgn));
         mgn.cxLeftWidth    := tmpMargins.cxLeftWidth;
         mgn.cxRightWidth   := tmpMargins.cxRightWidth;
         mgn.cyTopHeight    := tmpMargins.cyTopHeight;
         mgn.cyBottomHeight := tmpMargins.cyBottomHeight;
         { Extend Form }
         fDwmExtendFrameIntoClientArea(frm.Handle,@mgn);
       end;
     end;
     { Free loaded 'dwmapi' library }
     FreeLibrary(hDWMDLL);
   end;
 end;

end;

end.</delphi>

Copy the "glass.pas" file to the main folder of your project:

 MyProject\glass.pas
 

In the "uses" section of your project you need to add "glass":

<delphi>unit form1;

{$mode objfpc}{$H+}

interface

uses

 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs
 glass; // This includes GlassForm procedure </delphi>

OnActivate event of each form call the procedure in this way:

<delphi>procedure TForm1.FormActivate(Sender: TObject); var

 tmpMargins: TMargins;

begin

 { If all margins are -1 the whole form will be aero glass}
 tmpMargins.cxLeftWidth    := -1;
 tmpMargins.cxRightWidth   := -1;
 tmpMargins.cyBottomHeight := -1;
 tmpMargins.cyTopHeight    := -1;
 { FormName ; Margins ; TransparentColor }
 GlassForm(Form1, tmpMargins, clFuchsia); 

end;</delphi>

Also you need to enable Themes to use this procedure, go to Options > Project Options > then select "Use Manifest to Enable Themes (Windows)".

Bugs

As you can see in the first image Labels aren't displayed fine in Aero Glass, there are links with components and code that show how to make "Glow Labels":

Also if you clic the transparent part of the window the back window / desktop is selected (focus is lost in the form with aero glass).

About

This was converted to Lazarus using "{$mode delphi}" from "Aero Glass Effekt für Delphi-Forms, Delphi-Unit von Daniel Mitte (2006)":

There is a Delphi component here (to be ported in Lazarus):