TShape
から派生したクラスでスタックされています。私は、四角形を描画するためにCanvas
を使用Paint
方法では、ペイントイベントのTShape描画の問題
。フォームには、Left
とTop
の座標をTShape
に変更することができるTTrackBar
があります。
とTop
にどの値を設定しても、TTrackBar
を使用しても矩形はそれに応じて移動しません。代わりに、コードでこれらの値を設定すると、矩形が正しい位置に表示されます。
は、私は、Windows 10
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.
'ShapeRect'プロパティは読み取り専用ですが、変更することはできません。あなたがそれにアクセスするたびに、一時的な 'TRectF'が返されます。あなたのコードは 'TShape'に割り当てられていない一時的な' TRectF'を修正しています。そのため、長方形は動かない。 –
@Remy。あなたが気付かなかった場合、 'ShapeRect'は実装セクションの始めに宣言されたローカル変数です。私はこの宣言が 'tShape'の' ShapeRect'プロパティより優先されると思います。とにかく、上記のコードは実際のコードを抜粋したものに過ぎません。その変数は 'lvShapeRect'(' lv'は 'ローカル変数'を意味します)として宣言されているので、 'ShapeRect'プロパティと一緒にはいりません。その宣言を 'lvShapeRect'に改名します。 – user2383818
lとtの値を出力できますか? – loki