我需要一个可点击的控件,我可以根据自己的需要量身定制.从Delphi文档中我收集到了TRectangle会做的事情.我需要以编程方式创建我的TRectangles,因此我创建了一个Form,一个Panel和一个Button(参
我接下来尝试了FillRect并再次使用相同的结果,尽管使用了正确的填充.虽然调用来自Trectangle变量.Canvas.FillRect,它总是相对于树中的最高父节点,绕过TRectangle和TPanel.我一直认为绘画是相对于Canvas所属的TControl的坐标.
这是错的还是TRectangle有什么特别之处?在TRectangle上有更好的绘画方式吗?
unit rect_test_main; interface uses System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.StdCtrls, FMX.Objects; type TForm1 = class(TForm) Panel1: TPanel; Button1: TButton; procedure Button1Click (Sender: TObject); procedure FormCreate (Sender: TObject); protected FR: TRectangle; public procedure draw_paintto (rect: TRectangle); procedure draw_rect (rect: TRectangle); procedure draw_fill (rect: TRectangle); procedure draw (surface: TControl); end; var Form1: TForm1; implementation {$R *.fmx} procedure TForm1.FormCreate (Sender: TObject); begin FR := Trectangle.Create (Panel1); FR.Parent := Panel1; FR.Canvas.BeginScene; FR.Canvas.Fill.Color := TAlphaColors.Black; FR.Canvas.Stroke.Color := TAlphaColors.Green; FR.SetBounds (200, 200, 50, 300); FR.OnClick := Button1Click; FR.Canvas.EndScene; end; // FormCreate // procedure TForm1.Button1Click (Sender: TObject); begin draw_fill (FR); end; procedure TForm1.draw_paintto (rect: TRectangle); var r: TRectF; begin FR.Canvas.BeginScene; FR.Canvas.Fill.Color := TAlphaColors.Black; FR.Canvas.Stroke.Color := TAlphaColors.Green; FR.Canvas.Stroke.Thickness := 10; r := rect.ShapeRect; // r := TRectF.Create (TPointF.Create (FR.Position.X - 10, FR.Position.Y - 10), FR.Width, FR.Height); FR.PaintTo (FR.Canvas, r, FR.Parent); FR.Canvas.EndScene; end; // draw_paintto // procedure TForm1.draw_fill (rect: TRectangle); var r: TRectF; begin FR.Canvas.BeginScene; FR.Canvas.Fill.Color := TAlphaColors.Black; FR.Canvas.Stroke.Color := TAlphaColors.Green; FR.Canvas.Stroke.Thickness := 10; r := TRectF.Create (TPointF.Create (FR.Position.X, FR.Position.Y), FR.Width, FR.Height); FR.Canvas.FillRect (r, 0, 0, AllCorners, 1); FR.Canvas.EndScene; end; // draw_fill //在TRectangle上绘制时,应该在TRectangle级别而不是在Canvas上访问画笔和笔划更改.在这种情况下,所有工作都按预期工作,绘制了TRectangle的Canvas.
Key: TRectangle; ... Key.Fill.Color := TAlphaColors.White; Key.Fill.Kind := TBrushKind.bkSolid; // and not Key.Canvas.Fill ... Key.Stroke.Thickness := 1; Key.Stroke.Color := TAlphaColors.Darkgray; Key.Paint;
为什么TRectangle在画布上绘制其Parent.Parent(在这种情况下)在Canvas上画画时打败了我.也许它没有Canvas本身和TRectangle.Canvas只是对其Parent.Canvas的引用?