Changeset 116 for trunk/Packages/Common


Ignore:
Timestamp:
Dec 11, 2024, 10:47:08 AM (2 weeks ago)
Author:
chronos
Message:
  • Fixed: Dark Windows title bar was not set in subwindows.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Packages/Common/Theme.pas

    r115 r116  
    2525  end;
    2626
     27  TDwmSetWindowAttribute = function(hwnd: HWND; dwAttribute: DWORD; pvAttribute: Pointer; cbAttribute: DWORD): HRESULT; stdcall;
     28
    2729  { TThemeManager }
    2830
     
    3133    FTheme: TTheme;
    3234    FActualTheme: TTheme;
     35    DwmapiLib: TLibHandle;
     36    DwmSetWindowAttribute: TDwmSetWindowAttribute;
    3337    function Gray(C: TColor): Byte;
    3438    procedure SetTheme(AValue: TTheme);
     
    5256  ThemeNameLight = 'Light';
    5357  ThemeNameDark = 'Dark';
     58  DwmapiLibName = 'dwmapi.dll';
     59  DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1 = 19;
     60  DWMWA_USE_IMMERSIVE_DARK_MODE = 20;
    5461
    5562procedure Register;
     
    158165
    159166procedure TThemeManager.SetThemedTitleBar(AForm: TForm; Active: Bool);
    160 type
    161   TDwmSetWindowAttribute = function(hwnd: HWND; dwAttribute: DWORD; pvAttribute: Pointer; cbAttribute: DWORD): HRESULT; stdcall;
    162 const
    163   DwmapiLibName = 'dwmapi.dll';
    164   DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1 = 19;
    165   DWMWA_USE_IMMERSIVE_DARK_MODE = 20;
    166 var
    167   DwmapiLib: TLibHandle;
    168   DwmSetWindowAttribute: TDwmSetWindowAttribute;
     167var
    169168  Attr: DWord;
    170169begin
     170  if Assigned(DwmSetWindowAttribute) and IsWindows10OrGreater(17763) then begin
     171    Attr := DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1;
     172    if IsWindows10OrGreater(18985) then Attr := DWMWA_USE_IMMERSIVE_DARK_MODE;
     173
     174    DwmSetWindowAttribute(AForm.Handle, Attr, @Active, SizeOf(Active));
     175  end;
     176end;
     177
     178procedure TThemeManager.SetTheme(AValue: TTheme);
     179begin
     180  if FTheme = AValue then Exit;
     181  FTheme := AValue;
     182  FActualTheme := FTheme;
     183  {$IFDEF WINDOWS}
     184  if Assigned(FTheme) and (FTheme = Themes.FindByName(ThemeNameSystem)) and IsDarkTheme then
     185    FActualTheme := Themes.FindByName(ThemeNameDark);
     186  {$ENDIF}
     187end;
     188
     189constructor TThemeManager.Create(AOwner: TComponent);
     190begin
     191  inherited;
     192  {$IFDEF WINDOWS}
    171193  DwmapiLib := LoadLibrary(DwmapiLibName);
    172194  if DwmapiLib <> 0 then DwmSetWindowAttribute := GetProcAddress(DwmapiLib, 'DwmSetWindowAttribute')
    173195    else DwmSetWindowAttribute := nil;
    174 
    175   if Assigned(DwmSetWindowAttribute) and IsWindows10OrGreater(17763) then begin
    176     Attr := DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1;
    177     if IsWindows10OrGreater(18985) then Attr := DWMWA_USE_IMMERSIVE_DARK_MODE;
    178 
    179     DwmSetWindowAttribute(AForm.Handle, Attr, @Active, SizeOf(Active));
    180   end;
    181   if DwmapiLib <> 0 then FreeLibrary(DwmapiLib);
    182 end;
    183 
    184 procedure TThemeManager.SetTheme(AValue: TTheme);
    185 begin
    186   if FTheme = AValue then Exit;
    187   FTheme := AValue;
    188   FActualTheme := FTheme;
    189   {$IFDEF WINDOWS}
    190   if Assigned(FTheme) and (FTheme = Themes.FindByName(ThemeNameSystem)) and IsDarkTheme then
    191     FActualTheme := Themes.FindByName(ThemeNameDark);
    192   {$ENDIF}
    193 end;
    194 
    195 constructor TThemeManager.Create(AOwner: TComponent);
    196 begin
    197   inherited;
     196  {$ENDIF}
     197
    198198  Themes := TThemes.Create;
    199199  with Themes.AddNew(ThemeNameSystem) do begin
     
    224224begin
    225225  FreeAndNil(Themes);
     226  {$IFDEF WINDOWS}
     227  if DwmapiLib <> 0 then FreeLibrary(DwmapiLib);
     228  {$ENDIF}
    226229  inherited;
    227230end;
Note: See TracChangeset for help on using the changeset viewer.