Using TFrame, how do I properly access the TCanvas property just as in a TForm?

StackOverflow https://stackoverflow.com/questions/15294099

  •  18-03-2022
  •  | 
  •  

Вопрос

I need to draw on the frames Canvas at runtime just like you would do with a normal form but for some reason they decided not to add the Canvas property to the frame even tho both TCustomFrame and TCustomForm come from the same parent class that handles the Canvas.

I've made it work up to the part where I can draw something by overriding the PaintWindow procedure but I still can't seem to use the Canvas property at runtime as if I'm missing a big chunk of the code.

Here's what I've done up to now :

TCustomFrameEx = class(TCustomFrame)
  private
    FCanvas: TControlCanvas;
    function GetCanvas: TCanvas;
  public
  property Canvas: TCanvas read GetCanvas;
end;

TFrame = class(TCustomFrameEx)
  protected
    procedure PaintWindow(DC: HDC); override;        
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy(); override;
  published
    ...
  end;

constructor TFrame.Create(AOwner: TComponent);
begin
  inherited;
  FCanvas := TControlCanvas.Create();
end;

destructor TFrame.Destroy();
begin
  FreeAndNil(fCanvas);
  inherited;
end;

function TCustomFrameEx.GetCanvas : TCanvas;
begin
  Result := fCanvas;
end;

procedure TFrame.PaintWindow(DC: HDC);
begin
  inherited;
  FCanvas.Handle := DC;
  FCanvas.Control := Self;
  FCanvas.Brush.Color := clWhite;
  fCanvas.FillRect(GetClientRect);
  FCanvas.Handle := 0;
end;

I assume I'm not properly assigning the handle or missing some paint event?

Это было полезно?

Решение

The easiest way would be

procedure TFrame2.PaintWindow(DC: HDC);
Var
 c:TCanvas;
begin
  inherited;
  c := Tcanvas.Create;
  try
   c.Handle := DC;
   c.Brush.Color := clWhite;
   c.FillRect(GetClientRect);
   c.Brush.Color := clBlue;
   //c.Ellipse(0,0,200,200);
  finally
   c.Free;
  end;
end;

Другие советы

The PaintWindow method of a frame is only called if the frame has children. You'll need to add a paint box control (or similar) to your frame, or some children (perhaps invisible).

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top