我的后代TMyImage = class(ExtCtrls.TImage)
需要访问TImage
继承的Canvas(来自TGraphicControl
祖先)
例如
procedure TMyImage.Paint;
var
LCanvas: TCanvas;
begin
// need "inherited inherited Canvas"
LCanvas := inherited (inherited Canvas); // of the TGraphicControl
inherited;
end;
上面的内容显然无法编译。
是否可以在不破解 TGraphicControl
并使用私有(private)成员 FCanvas
的情况下完成此操作?
这有效:
type
THackGraphicControl = class(TControl)
private
FCanvas: TCanvas;
end;
procedure TMyImage.Paint;
var
LCanvas: TCanvas;
begin
// need "inherited inherited Canvas"
LCanvas := THackGraphicControl(Self).FCanvas;
with LCanvas do
begin
Brush.Bitmap := FAlphaPattern;
FillRect(ClientRect);
Brush.Bitmap := nil;
end;
inherited;
end;
但我想知道是否有一个不依赖版本的解决方案。
最佳答案
您可以使用与现有技术类似的技术,但访问 Canvas
属性而不是 FCanvas
字段。 Canvas
属性受到保护,这意味着它在技术上是控件界面的一部分,因此您不必担心它在未来版本中发生更改。 (它可能仍然会改变,但这不是你应该担心的事情。)
type
THackGraphicControl = class(TGraphicControl) end;
LCanvas := THackGraphicControl(Self).Canvas;
关于delphi - 如何访问继承的Image或者继承的Canvas?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42313384/