Jump to content
Sign in to follow this  

Change Scroll bar color

Recommended Posts

Hi all,

I've already used Themes, with satisfaction, on some little applications.

In a big application, I haven't used the TStyleManager.Engine because too hard to re-design the entire application,

but I would like to have a way to style only the scrollbars colors of a TSynEdit component.


It is, in some way, possible to add ONLY the TScrollingStyleHook behavior in a program without the Style engine running?

Thank you for your suggenstions!

Share this post

Link to post

I've already added Style in TSynEdit and it is very simple to do.
For example, to add the Scrollbar style is only necessary to add this line:


{$R *.dfm}

procedure TForm8.FormCreate(Sender: TObject);
  TStyleManager.Engine.RegisterStyleHook(TCustomSynEdit, TScrollingStyleHook);


But I don't would to enable styling in all my project BUT only to SynEdit scrollbar so this is not a way to follow.

Perhaps I need to study a way to "extract" what Vcl.Theme manager does for TScrollingStyleHook and to apply only to my version of TCustomSynEdit...

Unfortunately, the entire project already living with an old theme system (Light/Dark), and now I can't change it.

Share this post

Link to post

Any idea on how to "force" the scrollbar colors?
With dark mode are very very ugly:

I've googled a lot searching for a way to use TThemeManager forcing it only to a component without results...

Share this post

Link to post
15 hours ago, shineworld said:

Any idea on how to "force" the scrollbar colors?
With dark mode are very very ugly:

I've googled a lot searching for a way to use TThemeManager forcing it only to a component without results...

try to add TScrollbar and make it related with that SynEdit and finally you will have a completely separated scrollbar with StyleElement property

don't forget to make Scrollbars property to ssNone for your SynEdit

Edited by bravesofts

Share this post

Link to post

Checking Vcl.Forms.pas, it registers both TCustomForm and TForm to the style engine.

Does it help if you call

TStyleManager.Engine.RegisterStyleHook(TSynEdit, TScrollingStyleHook);


Share this post

Link to post

In the end, I've solved placing two external TScrollbars (TAdvSmoothScroolbars from TMS), they permit

a deep control of colors and removing Windows SCROLLBAR created by TSynEdit.


- The SynEdit.ScrollBars := ssNone;

- The SynEdit.UpdateScrollbars from private to protected and of dynamic type.

- A helper class in my editor frame class:

unit osGCodeEditorFrame;


  TGCodeEditor = class(SynEdit.TSynEdit)
    procedure EditorVBarPositionChanged(Sender: TObject; Position: Integer);
    procedure EditorHBarPositionChanged(Sender: TObject; Position: Integer);
    procedure UpdateScrollBars; override;
    FInUpdateScrollBars: Boolean;
    EditorHBar: TAdvSmoothScrollBar;
    EditorVBar: TAdvSmoothScrollBar;

  // creates and sets gcode editor
  FGCodeEditor := TSynEdit.Create(Self);
  FGCodeEditor.Parent := Self;
  FGCodeEditor.Align := alClient;
  FGCodeEditor.Visible := True;
  FGCodeEditor.ScrollBars := ssNone;

  // creates and sets gcode editor horizontal scroll bar
  FGCodeEditor.EditorHBar := TAdvSmoothScrollBar.Create(Self);
  FGCodeEditor.EditorHBar.Parent := Self;
  FGCodeEditor.EditorHBar.Align := alBottom;
  FGCodeEditor.EditorHBar.Kind := sbHorizontal;
  FGCodeEditor.EditorHBar.OnPositionChanged := FGCodeEditor.EditorHBarPositionChanged;

  // creates and sets gcode editor vertical scroll bar
  FGCodeEditor.EditorVBar := TAdvSmoothScrollBar.Create(Self);
  FGCodeEditor.EditorVBar.Parent := Self;
  FGCodeEditor.EditorVBar.Align := alRight;
  FGCodeEditor.EditorVBar.Kind := sbVertical;
  FGCodeEditor.EditorVBar.OnPositionChanged := FGCodeEditor.EditorVBarPositionChanged;


{ TGCodeEditor }

procedure TGCodeEditor.EditorHBarPositionChanged(Sender: TObject; Position: Integer);
  if FInUpdateScrollBars then Exit;
  LeftChar := EditorHBar.Position;

procedure TGCodeEditor.EditorVBarPositionChanged(Sender: TObject; Position: Integer);
  if FInUpdateScrollBars then Exit;
  TopLine := EditorVBar.Position;

procedure TGCodeEditor.UpdateScrollBars;
  MaxScroll: Integer;
  ScrollInfo: TScrollInfo;

  // checks if standard scroll bars enabled
  if ScrollBars <> ssNone then Exit;

  // check if custom scroll bars enabled
  if (EditorVBar = nil) or (EditorHBar = nil) then Exit;
  FInUpdateScrollBars := True;
    // evaluates for custom horizontal scrollbar
    if EditorHBar <> nil then
      MaxScroll := Max(TSynEditStringList(Lines).LengthOfLongestLine, 1);
      ScrollInfo.nMin := 1;
      ScrollInfo.nMax := MaxScroll;
      ScrollInfo.nPage := CharsInWindow;
      ScrollInfo.nPos := LeftChar;
      EditorHBar.Min := ScrollInfo.nMin;
      EditorHBar.Max := ScrollInfo.nMax;
      EditorHBar.PageSize := ScrollInfo.nPage;
      EditorHBar.Position := ScrollInfo.nPos;
      EditorHBar.Visible := ScrollInfo.nMax > CharsInWindow;
      EditorHBar.Visible := False;

    // evaluates for custom vertical scrollbar
    if EditorVBar <> nil then
      MaxScroll := DisplayLineCount;
      ScrollInfo.nMin := 1;
      ScrollInfo.nMax := Max(1, MaxScroll);
      ScrollInfo.nPage := LinesInWindow;
      ScrollInfo.nPos := TopLine;
      EditorVBar.Min := ScrollInfo.nMin;
      EditorVBar.Max := ScrollInfo.nMax;
      EditorVBar.PageSize := ScrollInfo.nPage;
      EditorVBar.Position := ScrollInfo.nPos;
      EditorVBar.Visible := ScrollInfo.nMax > LinesInWindow;
      EditorVBar.Visible := False;
    FInUpdateScrollBars := False;

In this way is possible to use standard scrollbars or custom and use the already available and called in TSynEdit.UpdateScrollbars,

which does nothing is ScrollBars = ssNone to manage update of external custom scrollbars.

Works perfectly:

Edited by shineworld

Share this post

Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this