首页 > Windows开发 > 详细

Delphi: TGraphicControl支持PaintTo方法

时间:2018-06-27 12:49:42      阅读:342      评论:0      收藏:0      [点我收藏+]

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, 1, 1);
    Canvas.CopyRect(Self.ClientRect, bmp.Canvas, sr);
  finally
    bmp.Free;
  end;
end;

 

完美支持

Delphi: TGraphicControl支持PaintTo方法

原文:https://www.cnblogs.com/crwy/p/9233230.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!