Search code examples
delphiinheritancedelphi-7

How to access inherited of inherited Canvas of TImage?


My descendant TMyImage = class(ExtCtrls.TImage) needs to access the TImage inherited Canvas (of the TGraphicControl ancestor)

e.g.

procedure TMyImage.Paint;
var
  LCanvas: TCanvas;
begin
  // need "inherited inherited Canvas"
  LCanvas := inherited (inherited Canvas); // of the TGraphicControl

  inherited;
end;

The above wont compile obviously.
Is this can be done without hacking TGraphicControl and using the private member FCanvas?

This works:

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;

But I was wondering if there was a solution that will not be version dependent.


Solution

  • You can use a technique similar to what you already have, but access the Canvas property instead of the FCanvas field. The Canvas property is protected, which means it's technically part of the control's interface, so you shouldn't worry about it changing in a future version. (It might still change, but it's not something you should worry about.)

    type
      THackGraphicControl = class(TGraphicControl) end;
    
    LCanvas := THackGraphicControl(Self).Canvas;