剪切区域也是区域的一种, 窗口在重绘时只会重绘剪切区域中的内容.
SelectClipRgn 和 ExtSelectClipRgn 是通过指定区域来定义剪切区域;
SelectClipPath 是通过指定路径来定义剪切区域;
ExtSelectClipRgn 和 SelectClipPath 比 SelectClipRgn 多出一个与原剪切区域混合方式的参数:
代码文件:
SelectClipRgn 和 ExtSelectClipRgn 是通过指定区域来定义剪切区域;
SelectClipPath 是通过指定路径来定义剪切区域;
ExtSelectClipRgn 和 SelectClipPath 比 SelectClipRgn 多出一个与原剪切区域混合方式的参数:
RGN_AND = 1; RGN_OR = 2; RGN_XOR = 3; RGN_DIFF = 4; RGN_COPY = 5;本例效果图:
代码文件:
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls; type TForm1 = class(TForm) RadioGroup1: TRadioGroup; procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); procedure FormPaint(Sender: TObject); procedure RadioGroup1Click(Sender: TObject); end; var Form1: TForm1; implementation {$R *.dfm} var bit: TBITMAP; procedure TForm1.FormCreate(Sender: TObject); begin RadioGroup1.Caption := '定义剪切区域'; RadioGroup1.Items.CommaText := '无剪切区域,根据区域,根据路径'; RadioGroup1.ItemIndex := 0; bit := TBitmap.Create; bit.LoadFromFile('c:\temp\Test.bmp'); end; procedure TForm1.FormPaint(Sender: TObject); var Rgn: HRGN; begin case RadioGroup1.ItemIndex of 1: begin Rgn := CreateEllipticRgn(30,30,140,140); SelectClipRgn(Canvas.Handle, Rgn); DeleteObject(Rgn); end; 2: begin Canvas.Font.Size := 160; Canvas.Font.Style := [fsBold]; SetBkMode(Canvas.Handle, TRANSPARENT); BeginPath(Canvas.Handle); Canvas.TextOut(-15,-30,'A'); EndPath(Canvas.Handle); SelectClipPath(Canvas.Handle, RGN_COPY); end; end; Canvas.Draw(0,0,bit); end; procedure TForm1.RadioGroup1Click(Sender: TObject); begin Repaint; end; procedure TForm1.FormDestroy(Sender: TObject); begin bit.Free; end; end.窗体文件:
object Form1: TForm1 Left = 366 Top = 307 Caption = 'Form1' ClientHeight = 206 ClientWidth = 309 Color = clBtnFace Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'Tahoma' Font.Style = [] OldCreateOrder = False Position = poDesigned OnCreate = FormCreate OnDestroy = FormDestroy OnPaint = FormPaint PixelsPerInch = 96 TextHeight = 13 object RadioGroup1: TRadioGroup Left = 209 Top = 93 Width = 95 Height = 105 Caption = 'RadioGroup1' TabOrder = 0 OnClick = RadioGroup1Click end end