Author Topic: Background color of a TTrackBar  (Read 1446 times)

the_magik_mushroom

  • New Member
  • *
  • Posts: 23
Background color of a TTrackBar
« on: January 26, 2025, 10:19:05 pm »
Hello,

Simple question:

I have a form with a TrackBar1.
When I paint my form in red, the surrounding of the TrackBar1 stay to default color, until I click on it or it get the focus, then the surrounding becomes red.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.   form1.Color:= clRed;
  4. end;  

Any idea how to fix this?
The only solution I've found is to do a loop with SetFocus on all TTrackBars of my form, and I found it not clean.
Thanks
« Last Edit: January 26, 2025, 10:25:11 pm by the_magik_mushroom »

ASerge

  • Hero Member
  • *****
  • Posts: 2514
Re: Background color of a TTrackBar
« Reply #1 on: January 27, 2025, 04:53:32 pm »
When I paint my form in red, the surrounding of the TrackBar1 stay to default color, until I click on it or it get the focus, then the surrounding becomes red.
Confirm on Windows. Lazarus 3.8 (and Delphi 7 also). It's seems OS bug.

Quote
Any idea how to fix this?
The only solution I've found is to do a loop with SetFocus on all TTrackBars of my form, and I found it not clean.
It is safer to use ActiveControl:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   LastActive: TWinControl;
  4.   C: TControl;
  5. begin
  6.   LastActive := ActiveControl;
  7.   Color := clRed;
  8.   for C in GetEnumeratorControls do
  9.     if C is TTrackBar then
  10.       ActiveControl := TTrackBar(C);
  11.   ActiveControl := LastActive;
  12. end;

the_magik_mushroom

  • New Member
  • *
  • Posts: 23
Re: Background color of a TTrackBar
« Reply #2 on: January 28, 2025, 04:14:14 am »
Thank you I did this:
Code: Pascal  [Select][+][-]
  1. var
  2.   LastActive: TWinControl;
  3.   C: TControl;
  4. begin
  5.   LastActive := ActiveControl;
  6.  
  7.   for C in GetEnumeratorControls do
  8.   begin
  9.     if C is TTrackBar then
  10.     begin
  11.       ActiveControl := TTrackBar(C); // Set focus to each TTrackBar.
  12.     end;
  13.   end;
  14.   ActiveControl := LastActive; // Restore the original active control.
  15. end;      

the_magik_mushroom

  • New Member
  • *
  • Posts: 23
Re: Background color of a TTrackBar
« Reply #3 on: February 02, 2025, 07:50:57 am »

Well my problem is it does not work on my app with the ttrackbars been on different panels and tabs.

Code: Pascal  [Select][+][-]
  1. [Break]
  2. Project1 raised exception class 'EInvalidOperation' with message:
  3. Cannot focus a disabled or invisible window
  4.  

ASerge

  • Hero Member
  • *****
  • Posts: 2514
Re: Background color of a TTrackBar
« Reply #4 on: February 02, 2025, 06:24:05 pm »
Project1 raised exception class 'EInvalidOperation' with message:
Cannot focus a disabled or invisible window
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2.  
  3.   procedure TouchTrackBars(AWinControl: TWinControl);
  4.   var
  5.     C: TControl;
  6.   begin
  7.     if (AWinControl is TTrackBar) and AWinControl.CanFocus then
  8.       ActiveControl := AWinControl;
  9.     for C in AWinControl.GetEnumeratorControls do
  10.       if C is TWinControl then
  11.         TouchTrackBars(TWinControl(C));
  12.   end;
  13.  
  14. var
  15.   LastActive: TWinControl;
  16. begin
  17.   LastActive := ActiveControl;
  18.   Panel1.Color := clRed;
  19.   Panel2.Color := clRed;
  20.   Panel2.Enabled := False;
  21.   TouchTrackBars(Self);
  22.   ActiveControl := LastActive;
  23. end;

the_magik_mushroom

  • New Member
  • *
  • Posts: 23
Re: Background color of a TTrackBar
« Reply #5 on: February 02, 2025, 09:42:08 pm »
That works, thanks ASerge  :-*

 

TinyPortal © 2005-2018