Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!



Share:
Notifications
Clear all

[Solved] Lazarus Pascal - Enable darkmode/darktheme in your application

1 Posts
1 Users
0 Reactions
10 Views
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2874
Topic starter  

I'm not a daily Windows user, and Lazarus (at the moment) does not follow the Windows settings when set to Darkmode.
Understandably, I have some applications where Windows users would like to see DarkMode, but the widgetset (LCL) does not support this at the time.

After a lot of searching, I did find a way that at least looks like it is darkmode - keep in mind that its pretty close but not perfect.
This most likely only works with Windows 10 (I believe it even requires certain Windows 10 updats) and Windows 11.
Tested with Win32 and Win64 binaries.

These are the steps (files attached - extract in project directory) to make your application start (!) in darkmode under Windows, when Windows is set to darkmode.

Note that changing dark/light mode while the application is running, is not working at the moment (suggestions/input most welcome).

 

So the steps I did: First the needed uses clause:

Uses ....
  {$IFDEF Windows}
  , uWin32WidgetSetDark, uDarkStyle
  {$ENDIF}  
  ... ;

 

To make it work fully, I've added this to my main forms "OnCreate" event - this fixes some minor details that for some reason are not done automatically:

procedure TForm1.FormCreate(Sender: TObject);
begin
  ...
  if IsDarkModeAppearance then ApplyDarkStyle; 
  ...
end;

 

The function "IsDarkModeAppearance" detects darkmode kind-a cross platform, this is the part needed for Windows:

function IsDarkModeAppearance:boolean;
const
  KEYPATH = '\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize';
  KEYNAME = 'AppsUseLightTheme';
var
  LightKey: boolean;
  Registry: TRegistry;
begin
  Result := false;
  Registry := TRegistry.Create;
  try
    Registry.RootKey := HKEY_CURRENT_USER;
    if Registry.OpenKeyReadOnly(KEYPATH) then
      begin
        if Registry.ValueExists(KEYNAME) then
          LightKey := Registry.ReadBool(KEYNAME)
        else
          LightKey := true;
      end
    else
      LightKey := true;
    Result := not LightKey
  finally
    Registry.Free;
  end;
end;    

 

Note: The modifications I made were done in "uDarkStyle.pas" (line 254):

// mod to make sure light mode works proper as well
// was: AppMode := TPreferredAppMode(pamDefault);   

if ShouldAppsUseDarkMode then
  AppMode := pamForceDark
else
  AppMode := TPreferredAppMode(pamDefault);

What this does is make sure things work properly in light mode (pamDefault) and dark mode (pamForceDark). The latter makes sure the titlebar is dark as well.

Based on slightly modified excellent and extensive work by Alexander Koblov (Double Commander).

This topic was modified 4 hours ago by Hans

   
ReplyQuote
Share: