Recent

Author Topic: Form Canvas Strange  (Read 220 times)

milen_prg

  • Newbie
  • Posts: 4
Form Canvas Strange
« on: February 17, 2025, 07:18:46 pm »
Why this code:

Code: Pascal  [Select][+][-]
  1. Procedure TForm1.FormPaint(Sender: TObject);
  2. Begin
  3.      Brush.Color := clRed;
  4.      Canvas.Rectangle(0, 0, Width div 2, Height);
  5.      Brush.Color := clGreen;
  6.      Canvas.Rectangle(Width div 2, 0, Width, Height);
  7. end;          

Do not draw window with left half Red and right half Green?
After refresh it draws just one vertical green line at the Form middle.

And if I change the last row (for example)
from
Canvas.Rectangle(Width div 2, 0, Width, Height);
to
Canvas.Rectangle(Width div 2, 0, Width div 2, Height);

The right half become Green, but, the left is White, no Red at all. And this is not what the Rectangle procedure must do!

Generally, Help, please!

Nimbus

  • New Member
  • *
  • Posts: 20
Re: Form Canvas Strange
« Reply #1 on: February 17, 2025, 07:39:56 pm »
Maybe try assigning the color to the Canvas' brush, that is
Code: Pascal  [Select][+][-]
  1. Canvas.Brush.Color := clRed;
  2. ..
  3. Canvas.Brush.Color := clGreen;
  4. ..
  5.  

cdbc

  • Hero Member
  • *****
  • Posts: 1964
    • http://www.cdbc.dk
Re: Form Canvas Strange
« Reply #2 on: February 17, 2025, 07:46:54 pm »
Hi
Try this:
Code: Pascal  [Select][+][-]
  1. Procedure TForm1.FormPaint(Sender: TObject);
  2. Begin
  3.      Brush.Color := clRed;
  4.      Canvas.Rectangle(1, 1, (Width div 2)-1, Height-1);
  5.      Brush.Color := clGreen;
  6.      Canvas.Rectangle((Width div 2)+1, 1, Width-1, Height-1);
  7. end;
There are bevels to take into account and if you draw 2 lines with the same coordinates after another, the last one wins...
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 3.6 up until Jan 2024 from then on it's both above &: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 4.99

wp

  • Hero Member
  • *****
  • Posts: 12695
Re: Form Canvas Strange
« Reply #3 on: February 17, 2025, 07:58:06 pm »
Nimbus is right. This works (tested):
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormPaint(Sender: TObject);
  2. begin
  3.   Canvas.Brush.Color := clRed;
  4.   Canvas.Rectangle(0, 0, Width div 2, Height);
  5.   Canvas.Brush.Color := clGreen;
  6.   Canvas.Rectangle(Width div 2, 0, Width, Height);
  7. end;

milen_prg

  • Newbie
  • Posts: 4
Re: Form Canvas Strange
« Reply #4 on: February 17, 2025, 08:07:10 pm »
Maybe try assigning the color to the Canvas' brush, that is
Code: Pascal  [Select][+][-]
  1. Canvas.Brush.Color := clRed;
  2. ..
  3. Canvas.Brush.Color := clGreen;
  4. ..
  5.  

Yes, Nimbus is right!
Thank, you!
But WHY the compiler allows this?! Or what actually did the Brush of the Form itself (not its Canvas)?!

 

TinyPortal © 2005-2018