【问题标题】:Custom draw FMX control position is wrong自定义绘制 FMX 控制位置错误
【发布时间】:2014-12-18 10:56:40
【问题描述】:

我最近开始将应用程序转换为 FireMonkey,并从简单的控件开始。出于某种原因,与TPanel 或TButton 等表单上放置的组件相比,它们的位置是关闭的。从我的测试来看,位置似乎翻了一番。

我的测试项目很简单:(在Delphi XE5中)

  • 创建一个新的 firemonkey HD 应用程序
  • 在窗体的 (100,100) 位置放置一个面板,右键单击它并“发送到后面”
  • 为自定义组件粘贴以下代码(根据需要调整名称)

代码:

type
  TTest = class(TPaintBox)
  private
    FBitmap: TBitmap;
  public
    Constructor Create(AOwner:TComponent); override;
    Destructor Destroy; override;
    procedure Paint; override;
  end;

{ TTest }

constructor TTest.Create(AOwner: TComponent);
begin
  inherited;
  FBitmap := TBitmap.Create;
  FBitmap.LoadFromFile('c:\test.png');
  Width := FBitmap.Width;
  Height := FBitmap.Height;
end;

destructor TTest.Destroy;
begin
  FreeAndNil(FBitmap);
  inherited;
end;

procedure TTest.Paint;
begin
  Canvas.DrawBitmap(FBitmap,
     TRectf.Create(0, 0, FBitmap.Width, FBitmap.Height),
     AbsoluteRect,
     1);
end;
  • 粘贴以下代码动态创建上述组件

代码:

procedure TForm2.FormCreate(Sender: TObject);
var t: TTest;
begin
  t := TTest.Create(self);
  t.Parent := self;
  t.Position.X := 50;
  t.Position.Y := 50;
end;

为 Win32 构建它。

在我这边,图像出现在面板的左上角,位于 100,100,但控件显然设置为将自身定位在 50,50

调试显示位置和矩形的正确值。

我不知道发生了什么。也许有人有一些建议/解释。

谢谢。

【问题讨论】:

    标签: delphi firemonkey


    【解决方案1】:

    AbsoluteRect 是控件相对于窗体的矩形。如果你想画一些东西,你必须使用局部坐标,在这种情况下是LocalRect

    Canvas.DrawBitmap(FBitmap, TRectf.Create(0, 0, FBitmap.Width, FBitmap.Height), LocalRect, 1);
    

    【讨论】:

    • 这对 TPaintBox 的后代来说是完美的。谢谢
    • 用 TTest = class(TPanel) 替换 TTest = class(TPaintBox) 它停止工作。面板显示正确,但其上的自定义绘图未显示在任何地方。有什么想法吗?
    • TPanel 是 TStyledControl 的后代,这就是为什么它被画成它的样式。我更喜欢 TControl 的后代。
    • 好的,但是有什么方法可以为 TPanel 修复它吗?我不喜欢我的硬编码解决方案 div-ing 的东西 2。也许从某种样式中获取此信息?
    猜你喜欢
    • 1970-01-01
    • 2013-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多