PtInRegion
API说明:
BOOL PtInRegion(
HRGN hrgn, // handle of region
int X, // x-coordinate of point
int Y // y-coordinate of point
);
Parameters
hrgn
Identifies the region to be examined.
X
Specifies the x-coordinate of the point.
Y
Specifies the y-coordinate of the point.
Return Values
If the specified point is in the region, the return value is nonzero.
Delphi代码:
unit Unit2;
interface
//{$DEFINE LEFTBOTTOM} //注释调此编译开关 PtInRect不能正确判断在该区域的点,PtInRegion能够正确判断
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;
type
TForm2 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormPaint(Sender: TObject);
procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
p1, p2: TPoint; //p1, p2构成矩形
FRect: TRect;
FRgn: HRGN;
public
{ Public declarations }
end;
var
Form2: TForm2;
implementation
{$R *.dfm}
procedure TForm2.FormCreate(Sender: TObject);
begin
//从p1点向p2点方向画矩形(两个点画矩形有4中可能的情况:即p1为左上,左下,右下,右上坐标点)
{$IFDEF LEFTBOTTOM}
//p1为左下坐标,此时PtInRect能够正确判断在该区域的点
p1.X := 30;
p1.Y := 30;
p2.X := 200;
p2.Y := 110;
{$ELSE}
//p1为右上坐标,此时PtInRect不能正确判断在该区域的点
p1.X := 200;
p1.Y := 110;
p2.X := 30;
p2.Y := 30;
{$ENDIF}
FRect.Left := p1.X;
FRect.Right := p2.x;
FRect.Top := p1.y;
FRect.Bottom:= p2.y;
FRgn := CreateRectRgnIndirect(FRect);
end;
procedure TForm2.FormDestroy(Sender: TObject);
begin
DeleteObject(FRgn);
end;
procedure TForm2.FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if PtInRegion(FRgn, X, Y) then {判断鼠标当前点是否在区域内}
ShowMessage('该点在矩形区域内');
end;
procedure TForm2.FormPaint(Sender: TObject);
begin
Canvas.Rectangle(FRect);
end;
end.