Recent

Author Topic: Canvas.Rectangle wrong [Solved]  (Read 601 times)

lainz

  • Hero Member
  • *****
  • Posts: 3463
    • Lainz
Canvas.Rectangle wrong [Solved]
« on: December 21, 2019, 01:12:46 pm »
Hi, try this code on Windows:

Code: Pascal  [Select]
  1. procedure TForm1.PaintBox1Paint(Sender: TObject);
  2. begin
  3.   PaintBox1.Canvas.Pen.Width:=2;
  4.   PaintBox1.Canvas.Rectangle(0, 0, PaintBox1.Width, PaintBox1.Height);
  5. end;

You will notice that the top and left borders has 1px wide only, instead of 2.

Also try this:

Code: Pascal  [Select]
  1. procedure TForm1.PaintBox1Paint(Sender: TObject);
  2. begin
  3.   PaintBox1.Canvas.Pen.Width:=2;
  4.   PaintBox1.Canvas.Rectangle(1, 1, PaintBox1.Width, PaintBox1.Height);
  5. end;

This fixes the width, but there are 2 missing pixels, at the top left and top right.

Tested on Windows 10.

There are more properties I need to set to get a proper drawing? (I mean 2px width in each border, without missing pixels).

Thanks  :)

Edit: On Delphi I get the first test the same (missing 1px at left and top), and the second is without missing pixels (the output is the desired).
« Last Edit: December 21, 2019, 01:36:54 pm by lainz »

winni

  • Hero Member
  • *****
  • Posts: 883
Re: Canvas.Rectangle wrong
« Reply #1 on: December 21, 2019, 01:26:19 pm »
Hi!

This error was already there in Delphi 1. In Lazarus with gtk you get the same result.

The problem is that it should be the same result for all 4 sides.

Either: (0,0,width,height) draws 1 pixel - the other pixel is "outside" the component
Or : this draws 2 pixel per line -  then you run in trouble in the very center of the paintbox.

Winni

lainz

  • Hero Member
  • *****
  • Posts: 3463
    • Lainz
Re: Canvas.Rectangle wrong
« Reply #2 on: December 21, 2019, 01:36:42 pm »
Ok thanks.

Problem solved, just adding these properties:

Code: Pascal  [Select]
  1. PaintBox1.Canvas.Pen.Width:=2; // or any width, the settings will respect
  2. PaintBox1.Canvas.Pen.JoinStyle:=pjsMiter;// remove rounded borders
  3. PaintBox1.Canvas.Pen.Style:=psInsideframe; // draw border inside rectangle
  4. PaintBox1.Canvas.Rectangle(0, 0, PaintBox1.Width, PaintBox1.Height);

If I not set pjsMiter it draw rounded borders. If I not set psInsideframe it draws the line centered in the border (half inside, half outside). Setting these two properties solves the problem, I get perfect borders for any pen width values.