* * *

Author Topic: Error in CreatePolygonRgn  (Read 1710 times)

edgarrod71

  • New member
  • *
  • Posts: 15
Error in CreatePolygonRgn
« on: February 23, 2016, 08:40:07 am »
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

  • Hero Member
  • *****
  • Posts: 4650
Re: Error in CreatePolygonRgn�
« Reply #1 on: February 23, 2016, 09:10:02 am »
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  [Select]
  1. var Pol: Array of TPoint;
  2. ButtonRgn: THandle;
  3.  
  4. ...
  5. begin
  6.  Setlength(Pol,6);
  7.   Pol[0].X := X-100; Pol[0].Y := Y-100;
  8.   Pol[1].X := X-100; Pol[1].Y := Y+100;
  9.   Pol[2].X := X+100; Pol[2].Y := Y+100;
  10.   Pol[3].X := X+100; Pol[3].Y := Y-100;
  11.   Pol[4].X := X; Pol[4].Y := Y-100;
  12.   Pol[5].X := X-100; Pol[5].Y := Y-100;
  13.  
  14.   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.
« Last Edit: February 23, 2016, 09:13:29 am by Thaddy »
"Logically, no number of positive outcomes at the level of experimental testing can confirm a scientific theory, but a single counterexample is logically decisive."

GetMem

  • Hero Member
  • *****
  • Posts: 2419
Re: Error in CreatePolygonRgn�
« Reply #2 on: February 23, 2016, 09:35:48 am »
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  [Select]
  1. uses LCLIntf, LCLType;
  2.  
  3. var
  4.   Rgn: THandle;
  5.   Pol : array of TPoint;
  6.  
  7. procedure TForm1.FormCreate(Sender: TObject);
  8. begin
  9.   SetLength(Pol, 4);
  10.   Pol[0].X := 120; Pol[0].Y := 75;
  11.   Pol[1].X := 250; Pol[1].Y := 25;
  12.   Pol[2].X := 300; Pol[2].Y := 200;
  13.   Pol[3].X := 75;  Pol[3].Y := 125;
  14.   Rgn := CreatePolygonRgn(@Pol[0], Length(Pol), WINDING);
  15. end;
  16.  
  17. procedure TForm1.FormPaint(Sender: TObject);
  18. begin
  19.   with Form1.Canvas do
  20.   begin
  21.     Pen.Width := 2;
  22.     Pen.Color := clBlack;
  23.     Brush.Color := clRed;
  24.     Canvas.Polygon(Pol);
  25.   end;
  26. end;
  27.  
  28. procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
  29.   Shift: TShiftState; X, Y: Integer);
  30. var
  31.   Res: Boolean;  
  32. begin
  33.   Res := PtInRegion(Rgn, X, Y);
  34.   if Res then
  35.     ShowMessage('Clicked inside the polygon.')
  36.   else
  37.     ShowMessage('Clicked outside the polygon.');
  38. end;
  39.  

Thaddy

  • Hero Member
  • *****
  • Posts: 4650
Re: Error in CreatePolygonRgn�
« Reply #3 on: February 23, 2016, 09:39:20 am »
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.
« Last Edit: February 23, 2016, 09:47:30 am by Thaddy »
"Logically, no number of positive outcomes at the level of experimental testing can confirm a scientific theory, but a single counterexample is logically decisive."

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus