Recent

Author Topic: use Gradientfill on component  (Read 1322 times)

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
use Gradientfill on component
« on: May 25, 2020, 04:41:06 pm »
Is it easy to use Gradientfill on components? I thought to use it on TShape or TPanel with this piece of code:
Code: Pascal  [Select][+][-]
  1. procedure TFrmInhaaledit.FormCreate(Sender: TObject);
  2. var MyRect : TRect;
  3. begin
  4.   MyRect := panel1.ReadBounds;
  5.   panel1.Canvas.GradientFill(MyRect,clblue, clwhite,gdVertical);
  6. end;
It doesn't give me the result on form.
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: use Gradientfill on component
« Reply #1 on: May 25, 2020, 05:26:28 pm »
Before anything else, check the result of ReadBounds. I've found, to my chagrin, that it usually doesn't return the real bounds until much later (say, in OnActivate or OnShow).

Note also that the normal place to "custom-draw" any control is in its OnPaint event, so you could try that.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: use Gradientfill on component
« Reply #2 on: May 25, 2020, 05:50:40 pm »
FormCreate is too early. And it does not result in a persistent image; the control must be able to draw itself correctly whenever it is requested to do so by the OS. The OnCreate event is long gone, however, when the control is painted. Therefore, the correct place is OnPaint, and if you want to have the gradient on the panel, it is the OnPaint of the panel.

Try this:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Panel1Paint(Sender: TObject);
  2. const
  3.   clSkyBlue_2 = $F8DAB6;
  4. var
  5.   R: TRect;
  6. begin
  7.   R := Rect(0, 0, Panel1.Width, Panel1.Height * 4 div 10);
  8.   Panel1.Canvas.GradientFill(R, clWhite, clSkyBlue, gdVertical);
  9.  
  10.   R.Top := R.Bottom;
  11.   R.Bottom := Panel1.Height;
  12.   Panel1.Canvas.GradientFill(R, clSkyBlue, clSkyBlue_2, gdVertical);
  13. end;
« Last Edit: May 26, 2020, 01:01:45 pm by wp »

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Re: use Gradientfill on component
« Reply #3 on: May 26, 2020, 10:08:41 am »
Thanks all :D
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

jamie

  • Hero Member
  • *****
  • Posts: 6133
Re: use Gradientfill on component
« Reply #4 on: May 26, 2020, 12:46:37 pm »
I've found getting and setting the bounds are not mutually equal..

One way is actual plot on the surface while the other reports the width and height's

 both left, top report the same but the Right, Bottom differ..

The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018