2

How do you detect that the user is running the Windows Aero theme on his operating system by code on Delphi 7?

Dmitry
  • 14,306
  • 23
  • 105
  • 189

1 Answers1

6

The function we need to use is Dwmapi.DwmIsCompositionEnabled, but that is not included in the Windows header translations that ship with Delphi 7 and was added in Vista, released after Delphi 7. Also it crashes the application on Windows XP - so call it after check if Win32MajorVersion >= 6.

function IsAeroEnabled: Boolean;
type
  TDwmIsCompositionEnabledFunc = function(out pfEnabled: BOOL): HRESULT; stdcall;
var
  IsEnabled: BOOL;
  ModuleHandle: HMODULE;
  DwmIsCompositionEnabledFunc: TDwmIsCompositionEnabledFunc;
begin
  Result := False;
  if Win32MajorVersion >= 6 then // Vista or Windows 7+
  begin
    ModuleHandle := LoadLibrary('dwmapi.dll');
    if ModuleHandle <> 0 then
    try
      @DwmIsCompositionEnabledFunc := GetProcAddress(ModuleHandle, 'DwmIsCompositionEnabled');
      if Assigned(DwmIsCompositionEnabledFunc) then
        if DwmIsCompositionEnabledFunc(IsEnabled) = S_OK then
          Result := IsEnabled;
    finally
      FreeLibrary(ModuleHandle);
    end;
  end;
end;
Dmitry
  • 14,306
  • 23
  • 105
  • 189
  • @TLama - What I posted is a VCL function: `DwmCompositionEnabled`. It calls DwmIsCompositionEnabled only on Vista+, returns false otherwise. – Sertac Akyuz Feb 06 '13 at 15:51
  • @Sertac, sorry. My mistake. I'll rather stop doing two things at once :-) Taking back my comment (deleting)... And since the question become tagged as Delphi 7, this is the right answer. – TLama Feb 06 '13 at 15:52
  • @TLama - My comment doesn't prove you wrong. I just wanted to be a little specific. – Sertac Akyuz Feb 06 '13 at 15:55
  • 2
    You need to `FreeLibrary` when `aDllHandle <> 0` – kobik Feb 06 '13 at 16:14
  • 2
    Otherwise FreeLibrary will fail with 'specified module could not be found', but no one is checking the return so doesn't matter much. – Sertac Akyuz Feb 06 '13 at 16:38