Forum > Unix

Error in CreatePolygonRgn

(1/1)

edgarrod71:
there is a bug in the GetMem of the Gtk2Winapi, it simply does not allocate memory when I try to create a Polygonal Region... here's the code, maybe I'm doing something wrong?

var Pol: PPoint;
ButtonRgn: THandle;

...
begin
  Pol[0].X := X-100; Pol[0].Y := Y-100;
  Pol[1].X := X-100; Pol[1].Y := Y+100;
  Pol[2].X := X+100; Pol[2].Y := Y+100;
  Pol[3].X := X+100; Pol[3].Y := Y-100;
  Pol[4].X := X; Pol[4].Y := Y-100;
  Pol[5].X := X-100; Pol[5].Y := Y-100;

  ButtonRgn := CreatePolygonRgn(Pol, 6, WINDING);
...
end;

Thaddy:
You are just using a pointer, but the structure at the pointer is nowhere to be found.
You need an array of TPoint, or sufficient raw memory allocated through Getmem(Pol, NumberOf Points * SizeOf(TPoint)).
You have to allocate the memory first. You can do that is several ways, but:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var Pol: Array of TPoint;ButtonRgn: THandle; ...begin Setlength(Pol,6);  Pol[0].X := X-100; Pol[0].Y := Y-100;  Pol[1].X := X-100; Pol[1].Y := Y+100;  Pol[2].X := X+100; Pol[2].Y := Y+100;  Pol[3].X := X+100; Pol[3].Y := Y-100;  Pol[4].X := X; Pol[4].Y := Y-100;  Pol[5].X := X-100; Pol[5].Y := Y-100;   ButtonRgn := CreatePolygonRgn(@Pol[0], 6, WINDING); I am not sure it works out of the box, but you get the point.
This should get you started anyway.

balazsszekely:
CreatePolygonRgn works fine both under GTK2 and win32/64! The following example will draw a polygon to the form, then check if the user clicks inside/outside the shape:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---uses LCLIntf, LCLType; var  Rgn: THandle;  Pol : array of TPoint; procedure TForm1.FormCreate(Sender: TObject);begin  SetLength(Pol, 4);  Pol[0].X := 120; Pol[0].Y := 75;  Pol[1].X := 250; Pol[1].Y := 25;  Pol[2].X := 300; Pol[2].Y := 200;  Pol[3].X := 75;  Pol[3].Y := 125;  Rgn := CreatePolygonRgn(@Pol[0], Length(Pol), WINDING);end; procedure TForm1.FormPaint(Sender: TObject);begin  with Form1.Canvas do  begin    Pen.Width := 2;    Pen.Color := clBlack;    Brush.Color := clRed;    Canvas.Polygon(Pol);  end;end; procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;  Shift: TShiftState; X, Y: Integer);var  Res: Boolean;  begin  Res := PtInRegion(Rgn, X, Y);  if Res then    ShowMessage('Clicked inside the polygon.')  else    ShowMessage('Clicked outside the polygon.');end; 

Thaddy:
Yup. Works fine. He simply forgot to allocate memory.

[edit]
Because the button probably has a fixed shape he could also use a fixed sized array, not necessary to use a dynamic array.

edgarrod71:
This doesn't work in Mac, what is the option for that?

Navigation

[0] Message Index

Go to full version