Jump to content
Sign in to follow this  
shineworld

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:

uses
  Vcl.Themes;

{$R *.dfm}

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

image.thumb.png.3432c7fddc76e5605d665454f99b33da.png
 

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:
image.thumb.png.c5166c7dc2bdbc9d527dff5ae41c3f3a.png

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:
image.thumb.png.c5166c7dc2bdbc9d527dff5ae41c3f3a.png

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;

interface

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

  // 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);
begin
  if FInUpdateScrollBars then Exit;
  LeftChar := EditorHBar.Position;
end;

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

procedure TGCodeEditor.UpdateScrollBars;
 var
  MaxScroll: Integer;
  ScrollInfo: TScrollInfo;
begin
  inherited;

  // 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;
  try
    // evaluates for custom horizontal scrollbar
    if EditorHBar <> nil then
    begin
      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;
    end
    else
      EditorHBar.Visible := False;

    // evaluates for custom vertical scrollbar
    if EditorVBar <> nil then
    begin
      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;
    end
    else
      EditorVBar.Visible := False;
  finally
    FInUpdateScrollBars := False;
  end;
end;

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:
image.thumb.png.57448ef9815c6574eea33d66c31790dc.png

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  

×