Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

delphi - Firemonkey TControl subclass cannot draw on component

I want to try to create a firemonkey visual component and I have seen online that TControl gives the basic needs. This is what I have done so far:

  TMyTest = class(TControl)
  strict private
    //code...
  protected
    procedure Paint; override;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    //code...
  end;

I have looked at the source code of a FMX component called PlotGrid and I have copied what it does. My class descends from TControl (like PlotGrid) and it overrides Paint (like PlotGrid). Look at the code:

constructor TMyTest.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  SetAcceptsControls(False);
end;

destructor TMyTest.Destroy;
begin
  inherited;
end;

procedure TMyTest.Paint;
var
  i: integer;
  a, b: TPointF;
begin
  Canvas.Fill.Color := TAlphaColorRec.White;
  Canvas.Stroke.Color := TAlphaColorRec.Black;
  Canvas.Stroke.Thickness := 2;

  a.X := 0; a.Y := Height/2;
  b.X := Width;  b.Y := Height/2;
  Canvas.DrawLine(a, b, 1);
end;

Given this code, I expect to have something like this (I have edited with paint the image, it's not the real one)

enter image description here

The problem is that I get this

enter image description here

The component is fine because I see all the methods and properties and they work. The component is functional BUT I cannot see it in the designer! If I run the FMX application I cannot see the colors:

enter image description here

Any idea?


I have set the Opacity := 1; at the beginning of the Paint event but still nothing.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your control is painting on shared canvas. By the time it reaches your control's Paint method value of Canvas.Stroke.Kind is TBrushKind.None so if you don't assign some other value to it, it will not actually paint anything.

You have to add

Canvas.Stroke.Kind := TBrushKind.Solid;

But, that will only paint horizontal line (you forgot to create points and make DrawLine call for vertical one) and it will not fill the background with white color.

The simplest way to do so is with

Canvas.ClearRect(ClipRect, TAlphaColorRec.White);

In general common canvas values can (and will) be changed by other controls. Better way to deal with those is to mimic code from TShape providing your own TFill and TStroke fields and assigning those to canvas before painting. That way you can be sure that you will not miss setting some particular Stroke or Fill value that can be changed outside your control.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...