我被困在一个派生自 TShape
的 class .
在 Paint
方法中,我使用 Canvas
绘制一个矩形 . 在表单中,我有 TTrackBar
,允许更改 TShape
的 Left
和 Top
坐标 .
使用 TTrackBar
设置为 Left
和 Top
的值无关紧要,矩形不会相应移动 . 相反,当我通过代码设置这些值时,矩形出现在正确的位置 .
我正在使用Windows 10上的Delphi 10.1 Berlin编写FireMonkey应用程序 .
unit frmShapeStudy;
interface
type
tMy_Shape = class (tShape)
protected
procedure Paint; override;
public
constructor Create (aOwner: tComponent); override;
procedure Draw;
end;
tformShapeStudy = class (tForm)
trkBarLeft: TTrackBar;
trkBarTop: TTrackBar;
procedure FormCreate (Sender: tObject);
procedure TrackBarChange (Sender: tObject);
end;
var
formShapeStudy: tformShapeStudy;
implementation
{$R *.fmx}
var
My_Shape : tMy_Shape;
lvShapeRect : tRectF ;
procedure tformShapeStudy.FormCreate (Sender: tObject);
begin
My_Shape := tMy_Shape.Create (Self);
with My_Shape do begin
Parent := Self;
TrackBarChange (Self);
end;
end;
procedure tformShapeStudy.TrackBarChange (Sender: TObject);
begin
My_Shape.Draw;
end;
constructor tMy_Shape.Create (aOwner: tComponent);
begin
inherited;
with lvShapeRect do begin
Left := Self.Left;
Top := Self.Top ;
Height := 20;
Width := 20;
end;
end;
procedure tBS_Shape.Draw;
begin
l := formShapeStudy.trkBarLeft.Value;
t := formShapeStudy.trkBarTop .Value;
{`Left & Top` are set with `l & t` or with `120 & 150`
and tested separately, by commenting the propper code lines}
lvShapeRect.Left := l; // this does no work
lvShapeRect.Top := t; // this does no work
lvShapeRect.Left := 120; // this works
lvShapeRect.Top := 150; // this works
Repaint;
end;
procedure tMy_Shape.Paint;
begin
inherited;
with Canvas do begin
Fill .Color := tAlphaColorRec.Aqua;
Stroke.Color := tAlphaColorRec.Blue;
BeginScene;
FillRect (lvShapeRect, 0, 0, Allcorners, 1, tCornerType.Bevel);
DrawRect (lvShapeRect, 0, 0, Allcorners, 1, tCornerType.Bevel);
EndScene;
end;
end;
end.
1 回答
对不起伙计们!习惯使用
Left
&Top
代替Position.X
&Position.Y
. 我同意新的Position
方式设置Left
&Top
,但是Embarcadero仍然提供此属性没有意义,但它们在设置控件的Left
和Top
方面没有任何作用 . 换句话说,因为那些旧的属性仍然可用,他们应该设置Left
&Top
属性与Position.X
&Position.Y
相同,否则会导致这种错误,你知道你在设置Left
&Top
,但控件不动 .设置
Left
&Top
的正确方法是:除非Embarcadero改变
Left
&Top
属性的行为(这是非常不可能的) .