-3

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

我已经让它工作到我可以通过覆盖 PaintWindow 过程来绘制一些东西的部分,但我似乎仍然无法在运行时使用 Canvas 属性,就好像我错过了一大块代码一样。

这是我到目前为止所做的:

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;

我假设我没有正确分配句柄或错过了一些绘画事件?

4

2 回答 2

5

最简单的方法是

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;
于 2013-03-08T12:54:34.560 回答
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).

于 2013-03-08T13:36:06.753 回答