使用TFrame,如何像在TForm中一样正确访问TCanvas属性? [英] Using TFrame, how do I properly access the TCanvas property just as in a TForm?

查看:119
本文介绍了使用TFrame,如何像在TForm中一样正确访问TCanvas属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在运行时绘制Canvas框架,就像您使用普通表单一样,但是由于某些原因,他们决定不向框架添加Canvas属性,即使TCustomFrame和TCustomForm都来自同一个父类,处理画布.

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.

我已经通过覆盖PaintWindow过程使它可以画一些东西,但是我似乎仍然无法在运行时使用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?

推荐答案

最简单的方法是

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;

这篇关于使用TFrame,如何像在TForm中一样正确访问TCanvas属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆