WinAPI: RectInRegion - 判断矩形是否位于区域中

本例效果图:

WinAPI: RectInRegion - 判断矩形是否位于区域中

unit Unit1;



interface



uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, StdCtrls;



type

  TForm1 = class(TForm)

    Button1: TButton;

    Button2: TButton;

    procedure FormCreate(Sender: TObject);

    procedure FormDestroy(Sender: TObject);

    procedure FormPaint(Sender: TObject);

    procedure Button1Click(Sender: TObject);

    procedure Button2Click(Sender: TObject);

  end;



var

  Form1: TForm1;



implementation



{$R *.dfm}



const

  BoolStr: array[Boolean] of string = ('矩形不在区域中', '矩形在区域中');



var

  Rgn: HRGN;

  Rect: TRect;



procedure TForm1.FormCreate(Sender: TObject);

const

  n = 50;

var

  x1,y1,x2,y2: Integer;

begin

  x1 := n;

  y1 := n div 2;

  x2 := ClientWidth - n;

  y2 := ClientHeight - n;

  Rgn := CreateEllipticRgn(x1, y1, x2, y2);



  Rect := Bounds(10,10,10,10);

end;



procedure TForm1.Button1Click(Sender: TObject);

begin

  OffsetRect(Rect, 10, 6);

  Repaint;

  Text := BoolStr[RectInRegion(Rgn, Rect)];

end;



procedure TForm1.Button2Click(Sender: TObject);

begin

  OffsetRect(Rect, -10, -6);

  Repaint;

  Text := BoolStr[RectInRegion(Rgn, Rect)];

end;



procedure TForm1.FormPaint(Sender: TObject);

begin

  Canvas.Brush.Color := clSilver;

  Canvas.Brush.Style := bsCross;

  FillRgn(Canvas.Handle, Rgn, Canvas.Brush.Handle);



  Canvas.Brush.Color := clRed;

  Canvas.Brush.Style := bsSolid;

  FrameRgn(Canvas.Handle, Rgn, Canvas.Brush.Handle, 2, 2);



  FillRect(Canvas.Handle, Rect, Canvas.Brush.Handle);

end;



procedure TForm1.FormDestroy(Sender: TObject);

begin

  DeleteObject(Rgn);

end;



end.


 
   

你可能感兴趣的:(api)