2010. június 4., péntek

How to detect a mouse click in a polygon region


Problem/Question/Abstract:

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:

procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
type
  PPointArray = ^TPointArray;
  TPointArray = array[0..MaxInt div SizeOf(TPoint) - 1] of TPoint;
var
  Rgn: HRGN;
  P: PPointArray;
begin
  GetMem(P, SizeOf(TPoint) * 3);
  P[0] := Point(0, 0);
  P[1] := Point(50, 100);
  P[2] := Point(100, 50);
  Rgn := CreatePolygonRgn(P[0], 3, WINDING);
  Canvas.Brush.Color := clRed;
  FillRgn(Canvas.Handle, Rgn, Canvas.Brush.Handle);
  if PtInRegion(Rgn, X, Y) then
    Beep;
  DeleteObject(Rgn);
  FreeMem(P);
end;

Nincsenek megjegyzések:

Megjegyzés küldése