I have an issue writing a FireMonkey component. It is very basic! I cannot draw a rectangle with a line thickness of one pixel. Is always draw at least two pixels. On the bottom of the image is the rectangle drawn. Above is the lower left corner of a TTabControl. My rectangle has two pixels while the tabcontrol has a border of one pixel. How can I do that?


I have an issue writing a FireMonkey component. It is very basic! I cannot draw a rectangle with a line thickness of one pixel. Is always draw at least two pixels. On the bottom of the image is the rectangle drawn. Above is the lower left corner of a TTabControl. My rectangle has two pixels while the tabcontrol has a border of one pixel. How can I do that?

I wrote a simple component to reproduce the issue:
unit RectangleControl;

interface

uses
    System.SysUtils, System.Classes, System.UITypes,
    FMX.Types, FMX.Controls, FMX.Graphics;

type
    TRectControl = class(TControl)
    private
    protected
        procedure Paint; override;
    public
    published
        property Position;
        property Width;
        property Height;
    end;

procedure Register;

implementation

procedure Register;
begin
    RegisterComponents('Samples', [TRectControl]);
end;

procedure TRectControl.Paint;
begin
    Canvas.Stroke.Kind     := TBrushKind.bkSolid;
    Canvas.Stroke.Color    := TAlphaColors.Black;
    Canvas.StrokeThickness := 1;
    Canvas.DrawRect(BoundsRect, 0, 0, [], 1);
end;

end.

As you can see, it is difficult to find a simpler component. And yet it doesn't work as expected: the rectangle border is 2 pixels instead of only one. And beside that, the colour is also wrong. It looks like some antialiasing is in action.

What am I missing?

Comments

  1. ouch ! TLine.Paint calls GetShapeRect three times in this line !
    Canvas.DrawLine(GetShapeRect.TopLeft, PointF(GetShapeRect.Right, GetShapeRect.Top), AbsoluteOpacity, FStroke);

    ReplyDelete
  2. Hi François Piette  you have already solved it? 
    Seems that it is an anti-aliasing issue, one more small detail that seem FMX doesn't allow us to control. 
    Have you tried this undocumented functions?: 
    TCanvas.AlignToPixel public
    TCanvas.AlignToPixelHorizontally public
    TCanvas.AlignToPixelVertically

    ReplyDelete
  3. Denys Almaral Yes, I solved it and showed the solution in a previous comment, please read back the comments.

    ReplyDelete

Post a Comment