Author: Jonas Bilinkevicius
I create several regions using CreatePolygonRgn function, passing an array of
several points (ex. 4). After that, under some condition, I have to test if the
user has clicked inside that region using PtInRegion. Now there are some problems:
Sometimes CreatePolygonRgn returns 0 (no region created). Why is that? Under any
circumstances I can not get any hits when passing points to PtInRegion.
Answer:
Here is a sample using a dynamic TPoint array:
1
2 procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
3 Shift: TShiftState; X, Y: Integer);
4 type
5 PPointArray = ^TPointArray;
6 TPointArray = array[0..MaxInt div SizeOf(TPoint) - 1] of TPoint;
7 var
8 Rgn: HRGN;
9 P: PPointArray;
10 begin
11 GetMem(P, SizeOf(TPoint) * 3);
12 P[0] := Point(0, 0);
13 P[1] := Point(50, 100);
14 P[2] := Point(100, 50);
15 Rgn := CreatePolygonRgn(P[0], 3, WINDING);
16 Canvas.Brush.Color := clRed;
17 FillRgn(Canvas.Handle, Rgn, Canvas.Brush.Handle);
18 if PtInRegion(Rgn, X, Y) then
19 Beep;
20 DeleteObject(Rgn);
21 FreeMem(P);
22 end;
|