Recent

Author Topic: [SOLVED] using TForm.OnPaint to read form colour - is this just wrong?!  (Read 747 times)

robert rozee

  • Full Member
  • ***
  • Posts: 210
i've been trying to solve an interesting 'problem', and would like a little guidance: i have a modal TForm in a project, with a TPanel sitting in the middle of it (along with an 'OK' button for closing the form). i want the panel to have the same background colour as the form it is sitting on - at the moment it does not, they are both slightly different shades of grey. both the form and the panel have the colour set to clDefault; i don't really want to override these defaults. clearly clDefault is not a specific colour.

this is all running on Linux, using the GTK2 widget set, but i am hoping for a fairly universal solution.

the form (Form6) is created with the code:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var f:TForm6;
  3. begin
  4.   f:=TForm6.Create(nil);
  5.   try
  6.     if f.ShowModal=mrOK then
  7.     begin
  8.  
  9.     end
  10.   finally
  11.     f.Release
  12.   end
  13. end;


so far, the most 'sensible' solution i've found to having both form and panel the same colour is this:

Code: Pascal  [Select][+][-]
  1. procedure TForm6.FormPaint(Sender: TObject);
  2. begin
  3.   Panel1.Color:=Self.Canvas.Pixels[Self.Width-5, Self.Height-5]
  4. end;

the above code just picks up the colour of a pixel near the bottom right of the form and sets the panel colour to match.

the OnPaint event handler seems to be just the wrong place to be doing this - but it is the only place where it works, apart from having that one line of code instead sitting in a TTimer event. if trying to pick up the colour too early, it is grabbed from a pixel behind where the form is about to be drawn.

can anyone think of a better and/or 'more proper' solution?


cheers,
rob   :-)
« Last Edit: January 21, 2025, 11:21:29 am by robert rozee »

wp

  • Hero Member
  • *****
  • Posts: 12592
Re: using TForm.OnPaint to read form colour - is this just wrong?!
« Reply #1 on: January 20, 2025, 05:38:56 pm »
The color "clDefault" may be different for each control. But, there is a TControl method GetDefaultColor(DefaultColorType: TDefaultColorType) where DefaultColorType has two values, dctBrush and dctFont, for fill color and text color; it returns the exact color used by the calling control when it wants a clDefault.

So, please try this:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.   Panel1.Color := Self.GetDefaultColor(dctBrush);
  4. end;

robert rozee

  • Full Member
  • ***
  • Posts: 210
Re: using TForm.OnPaint to read form colour - is this just wrong?!
« Reply #2 on: January 20, 2025, 06:14:18 pm »
brilliant, that works perfectly!    :D

many thanks,
rob   :-)

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1460
    • Lebeau Software
Re: using TForm.OnPaint to read form colour - is this just wrong?!
« Reply #3 on: January 21, 2025, 05:41:12 am »
Does setting TPanel.ParentBackground to True not work for you?

Quote
Indicates if the background from the parent control is used to draw the background for the control.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

robert rozee

  • Full Member
  • ***
  • Posts: 210
Re: using TForm.OnPaint to read form colour - is this just wrong?!
« Reply #4 on: January 21, 2025, 11:20:28 am »
Does setting TPanel.ParentBackground to True not work for you?
Quote
Indicates if the background from the parent control is used to draw the background for the control.

it would work IF the parent had an actual RGB colour specified (such as clGray or clRed), but because of how clDefault is handled, as pointed out by wp:

The color "clDefault" may be different for each control

clDefault resolves to an RGB value picked from a list, a list that varies between different types of control. the way to get the actual RGB value in this case is (ref wp's post above) using Self.GetDefaultColor(dctBrush) where Self in this situation refers to the form on which the panel sits.

from the Lazarus 3.6 f1 help system:

Quote
clDefault
The default color of a given control.
Declaration
Source position: graphics.pp line 293
const clDefault = TColor($20000000);
Description
The default color for a given control. This color alone does not have any meaning; it needs to be resolved using a control instance and one of the methods like: TControl.GetDefaultColor, TControl.GetColorResolvingParent or TControl.GetRGBColorResolvingParent.

(i tried finding the above online, but couldn't)


cheers,
rob   :-)
« Last Edit: January 21, 2025, 11:34:44 am by robert rozee »

dsiders

  • Hero Member
  • *****
  • Posts: 1342
Re: using TForm.OnPaint to read form colour - is this just wrong?!
« Reply #5 on: January 21, 2025, 11:31:17 am »
Does setting TPanel.ParentBackground to True not work for you?
Quote
Indicates if the background from the parent control is used to draw the background for the control.

it would work IF the parent had an actual RGB colour specified (such as clGray or clRed), but because of how clDefault is handled, as pointed out by wp:

The color "clDefault" may be different for each control

clDefault resolves to an RGB value picked from a list, a list that varies between different types of control. the way to get the actual RGB value in this case is using Self.GetDefaultColor(dctBrush) where Self in this situation refers to the form on which the panel sits.

from the Lazarus 3.6 'f1' help system:

Quote
clDefault
The default color of a given control.
Declaration
Source position: graphics.pp line 293
const clDefault = TColor($20000000);
Description
The default color for a given control. This color alone does not have any meaning; it needs to be resolved using a control instance and one of the methods like: TControl.GetDefaultColor, TControl.GetColorResolvingParent or TControl.GetRGBColorResolvingParent.

(i tried finding the above online, but couldn't)

Do you mean the docs? https://lazarus-ccr.sourceforge.io/docs/lcl/graphics/cldefault.html
Preview the next Lazarus documentation release at: https://dsiders.gitlab.io/lazdocsnext

robert rozee

  • Full Member
  • ***
  • Posts: 210
Re: [SOLVED] using TForm.OnPaint to read form colour - is this just wrong?!
« Reply #6 on: January 21, 2025, 11:36:16 am »
that would be the one!


cheers,
rob   :-)

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1460
    • Lebeau Software
Re: using TForm.OnPaint to read form colour - is this just wrong?!
« Reply #7 on: January 21, 2025, 05:34:54 pm »
it would work IF the parent had an actual RGB colour specified (such as clGray or clRed), but because of how clDefault is handled, as pointed out by wp:

The color "clDefault" may be different for each control

If that is the case, then I think ParentBackground=true should override the behavior of clDefault inside of the child control so that it uses its parent's default color and not its own default color.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

 

TinyPortal © 2005-2018