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

六月ゝ 毕业季﹏ 提交于 2019-12-08 14:22:30

问题


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?


回答1:


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;



回答2:


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).



来源:https://stackoverflow.com/questions/15294099/using-tframe-how-do-i-properly-access-the-tcanvas-property-just-as-in-a-tform

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!