首页 文章

TShape - 关于Paint事件的绘图问题

提问于
浏览
0

我被困在一个派生自 TShape 的 class .

Paint 方法中,我使用 Canvas 绘制一个矩形 . 在表单中,我有 TTrackBar ,允许更改 TShapeLeftTop 坐标 .

使用 TTrackBar 设置为 LeftTop 的值无关紧要,矩形不会相应移动 . 相反,当我通过代码设置这些值时,矩形出现在正确的位置 .

我正在使用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 回答

  • 0

    对不起伙计们!习惯使用 LeftTop 代替 Position.XPosition.Y . 我同意新的 Position 方式设置 LeftTop ,但是Embarcadero仍然提供此属性没有意义,但它们在设置控件的 LeftTop 方面没有任何作用 . 换句话说,因为那些旧的属性仍然可用,他们应该设置 LeftTop 属性与 Position.XPosition.Y 相同,否则会导致这种错误,你知道你在设置 LeftTop ,但控件不动 .

    设置 LeftTop 的正确方法是:

    Position.X := aLeft;
    Position.Y := aTop;
    

    除非Embarcadero改变 LeftTop 属性的行为(这是非常不可能的) .

相关问题