Delphi: TGraphicControl支持PaintTo方法

Delphi之TWinControl支持PaintTo方法,可以方便的Paint有句柄控件,而此方法,TGraphicControl没有。

这使得有时需要Paint无句柄控件诸如TLabel时颇为费事,能否扩充它?

可以。使用class helper for技术,通过其Parent,仍然调用TWinControl支持PaintTo方法,中转一下。

直上代码吧,以做备忘:

  TGraphicControlHelper = class helper for TGraphicControl
procedure PaintTo(Canvas: TCanvas; X, Y: Integer);
end;

实现:

{ TGraphicControlHelper }

procedure TGraphicControlHelper.PaintTo(Canvas: TCanvas; X, Y: Integer);
var
bmp: TBitmap;
sr: TRect;
begin
if Self.Parent = nil then Exit; bmp := TBitmap.Create;
try
bmp.SetSize(Parent.Width, Parent.Height);
Self.Parent.PaintTo(bmp.Canvas.Handle, X, Y);
sr := Self.BoundsRect;
Windows.OffsetRect(sr, , );
Canvas.CopyRect(Self.ClientRect, bmp.Canvas, sr);
finally
bmp.Free;
end;
end;

完美支持

上一篇:解决IE6下DIV无法实现1px高度问题


下一篇:Activity not started, its current task has been brought to the front