码迷,mamicode.com
首页 > Windows程序 > 详细

Delphi: TGraphicControl支持PaintTo方法

时间:2018-06-27 13:53:00      阅读:590      评论:0      收藏:0      [点我收藏+]

标签:set   调用   dsr   句柄   实现   div   windows   需要   span   

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方法

标签:set   调用   dsr   句柄   实现   div   windows   需要   span   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!