Firemonkey TControl子类无法在组件上绘制 [英] Firemonkey TControl subclass cannot draw on component

查看:65
本文介绍了Firemonkey TControl子类无法在组件上绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想尝试创建一个firemonkey可视组件,并且我在网上看到 TControl 满足了基本需求。到目前为止,这是我所做的:

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;

我看过一个名为 PlotGrid的FMX组件的源代码,我已经复制了它的功能。我的课程来自TControl(如PlotGrid),它覆盖Paint(如PlotGrid)。看一下代码:

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)

问题是我得到了这个

该组件很好因为我看到了所有的方法和属性,它们都起作用。该组件正常运行,但我无法在设计器中看到它!如果我运行FMX应用程序,则看不到颜色:

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:

有什么想法吗?

我已将不透明度设置为== 1; Paint 事件开始时的code>,但仍然没有。

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

推荐答案

您的控件正在共享画布上绘画。到达到控件的 Paint 方法值 Canvas.Stroke.Kind 的时间为 TBrushKind 。 c。因此,如果您不为其分配其他值,则实际上不会绘制任何内容。

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.

您必须添加

Canvas.Stroke.Kind := TBrushKind.Solid;

但是,这只会绘制水平线(您忘记创建点并生成 DrawLine 调用垂直的一个),它不会用白色填充背景。

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.

最简单的方法是使用

Canvas.ClearRect(ClipRect, TAlphaColorRec.White);

通常,常用画布值可以(并且将)由其他控件更改。处理这些问题的更好方法是模仿 TShape 中的代码,提供自己的 TFill TStroke 字段,然后在绘画之前将其分配给画布。这样,您可以确保不会错过设置某些可以在控制范围之外更改的特定笔划或填充值。

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.

这篇关于Firemonkey TControl子类无法在组件上绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆