Recent

Author Topic: Can I add a dark theme to my form?  (Read 5310 times)

AMJF

  • New Member
  • *
  • Posts: 48
Can I add a dark theme to my form?
« on: April 25, 2022, 11:43:30 pm »
Is there a relatively simple way of making my own designed form on Lazarus IDE match the dark theme of Windows 10?

I hate the way that the form currently stands out as looking bright, in stark contrast. I have tried changing the Colour setting for each component, but it doesn't look right and the text in the dialogs is still dark, and not light on dark as in the dark theme.

I would imagine a single setting would be needed, but is there more to it than that?

AlexTP

  • Hero Member
  • *****
  • Posts: 2401
    • UVviewsoft
Re: Can I add a dark theme to my form?
« Reply #1 on: April 25, 2022, 11:46:03 pm »

AMJF

  • New Member
  • *
  • Posts: 48
Re: Can I add a dark theme to my form?
« Reply #2 on: April 26, 2022, 06:27:32 am »
I'm not sure how to add this to my own code.

AlexTP

  • Hero Member
  • *****
  • Posts: 2401
    • UVviewsoft
Re: Can I add a dark theme to my form?
« Reply #3 on: April 26, 2022, 09:34:12 am »
Import the unit: "uses win32titlestyler". Then call its procedure in your form's OnShow.

AMJF

  • New Member
  • *
  • Posts: 48
Re: Can I add a dark theme to my form?
« Reply #4 on: April 26, 2022, 10:58:33 am »
Import the unit: "uses win32titlestyler". Then call its procedure in your form's OnShow.

I copied the raw text of the .pas unit from github to a .pas file, added "win32titlestyler" to the "uses" part at the start of my program's unit, then added the call command to OnShow, with my program's form (fADL) as a variable and True for the Boolean variables. Nothing happened.

What should I do now?

wp

  • Hero Member
  • *****
  • Posts: 11915
Re: Can I add a dark theme to my form?
« Reply #5 on: April 26, 2022, 11:29:24 am »
I can confirm AMJF's observation that unit win32titlestyler does nothing (except for making the diffuse form shadow a bit darker in Win 11).

Zoran

  • Hero Member
  • *****
  • Posts: 1830
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Can I add a dark theme to my form?
« Reply #6 on: April 26, 2022, 12:23:00 pm »
Instead of adding win32titlestyler unit directly, you can add this unit to your project.

1. It can be safely added to a cross-platform project, it just does nothing on non-windows systems.

2. On windows os, this unit takes care of adding a call to ApplyFormDarkTitle function when each form is shown the first time.

Code: Pascal  [Select][+][-]
  1. unit TitleStylerHelper;
  2.  
  3. {$mode ObjFPC}{$H+}
  4.  
  5. interface
  6.  
  7. // On non-windows systems, this is empty unit!
  8.                            
  9. implementation
  10.                                        
  11. {$ifdef MSWINDOWS}
  12.  
  13. uses
  14.   SysUtils, Forms, win32titlestyler;
  15.  
  16. type
  17.  
  18.   TTitleStyler = class(TObject)
  19.   private
  20.     class var
  21.       TitleStyler: TTitleStyler;
  22.   public
  23.     class procedure Init;
  24.     class procedure Final;
  25.  
  26.     procedure NewFormAdded(Sender: TObject; Form: TCustomForm);
  27.     procedure FormFirstShow(Sender: TObject);
  28.   end;
  29.  
  30. class procedure TTitleStyler.Init;
  31. begin
  32.   TitleStyler := TTitleStyler.Create;
  33.   Screen.AddHandlerFormAdded(@TitleStyler.NewFormAdded);
  34. end;
  35.  
  36. class procedure TTitleStyler.Final;
  37. begin
  38.   Screen.RemoveAllHandlersOfObject(TitleStyler);
  39.   TitleStyler.Free;
  40. end;
  41.  
  42. procedure TTitleStyler.NewFormAdded(Sender: TObject; Form: TCustomForm);
  43. begin
  44.   Form.AddHandlerFirstShow(@TitleStyler.FormFirstShow);
  45. end;
  46.  
  47. procedure TTitleStyler.FormFirstShow(Sender: TObject);
  48. var
  49.   F: TForm;
  50. begin
  51. // Unfortunately, as currently implemented ApplyFormDarkTitle
  52. // requires TForm, not TCustomForm.
  53. // So, this cannot work for forms not derived from TForm.
  54.   if Sender is TForm then begin
  55.     F := TForm(Sender);
  56.     F.HandleNeeded;
  57.     ApplyFormDarkTitle(F, True, True);
  58.   end;
  59. end;
  60.  
  61. initialization
  62.   TTitleStyler.Init;
  63.  
  64. finalization
  65.   TTitleStyler.Final;
  66.  
  67. {$endif}
  68.  
  69. end.
  70.  

AMJF

  • New Member
  • *
  • Posts: 48
Re: Can I add a dark theme to my form?
« Reply #7 on: April 26, 2022, 01:10:08 pm »
I replaced win32titlestyler with titlestylehelper in the "uses" part of my project, and removed the procedure in OnShow for my form, but still nothing happened.

Could I try modifying win32titlestyler instead?

Zoran

  • Hero Member
  • *****
  • Posts: 1830
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Can I add a dark theme to my form?
« Reply #8 on: April 26, 2022, 01:42:28 pm »
I replaced win32titlestyler with titlestylehelper in the "uses" part of my project, and removed the procedure in OnShow for my form, but still nothing happened.

Of course the helper can work only if the original solution work. All it does is what I said in my previous post.

Could I try modifying win32titlestyler instead?

As I see it's MIT licensed. It is a permissive license, so you can, just make sure you comply with mit license.

dbannon

  • Hero Member
  • *****
  • Posts: 2792
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Can I add a dark theme to my form?
« Reply #9 on: April 26, 2022, 02:03:45 pm »
AMJF, sorry, a silly question, your Windows box is switched to Dark Theme isn't it ?   And its not an un-updated, early Windows 10 that did not support Dark Themes they way they do now ?

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

AMJF

  • New Member
  • *
  • Posts: 48
Re: Can I add a dark theme to my form?
« Reply #10 on: April 26, 2022, 02:20:35 pm »
AMJF, sorry, a silly question, your Windows box is switched to Dark Theme isn't it ?   And its not an un-updated, early Windows 10 that did not support Dark Themes they way they do now ?

Davo

Well, Windows Explorer is on Dark Theme, yes. As far as I know, it's the latest Windows 10.

wp

  • Hero Member
  • *****
  • Posts: 11915
Re: Can I add a dark theme to my form?
« Reply #11 on: April 26, 2022, 03:33:46 pm »
Just my two cents: It is my impression that the Windows dark mode still is not finished, even in Win 11. Just switched to dark mode in the system settings -> the explorer becomes dark. I delete a large folder -> the message form showing the progress of the process is bright. Excel 2016's main form is dark, but any dialog which I open is bright. A variety of applications, even by Microsoft, are bright to begin with: Task manager, Computer administration, Device manager, RegEdit, Write...

AMJF

  • New Member
  • *
  • Posts: 48
Re: Can I add a dark theme to my form?
« Reply #12 on: April 26, 2022, 05:19:01 pm »
In that case, It's a shame I can't manually change all the colours, invert all the colours of the text and background, etc so that they'll look as good as Dark Theme. Unless there's a guide somewhere?

AMJF

  • New Member
  • *
  • Posts: 48
Re: Can I add a dark theme to my form?
« Reply #13 on: April 26, 2022, 08:35:54 pm »
So is there a way I can change the colours on my form to mimic a dark theme and invert the text colours so that my whole form looks like the Steam interface?

AlexTP

  • Hero Member
  • *****
  • Posts: 2401
    • UVviewsoft
Re: Can I add a dark theme to my form?
« Reply #14 on: April 26, 2022, 10:54:15 pm »
Yes, if you use ATFlatControls or another lib like it.

 

TinyPortal © 2005-2018