2004. május 14., péntek

Create a polygon-shaped form using regions


Problem/Question/Abstract:

How to create a polygon-shaped form using regions

Answer:

To start with, we need to make an array of points of all corners of the form (there can be as many as you want). Next we use the Windows API call CreatePolygonRgn to get a handle to the region we have just defined. Finally we need to set this region the window we want to be that shape using another API call SetWindowRgn. To see this in effect create a new project and in the forms onCreate event have:

procedure TForm1.FormCreate(Sender: TObject);
var
  Region: HRgn;
  Points: array[0..11] of TPoint;
begin
  {Define the points of a W shape}
  Points[0] := Point(0, 0);
  Points[1] := Point(50, 0);
  Points[2] := Point(180, 200);
  Points[3] := Point(218, 100);
  Points[4] := Point(256, 200);
  Points[5] := Point(385, 0);
  Points[6] := Point(435, 0);
  Points[7] := Point(256, 300);
  Points[8] := Point(218, 200);
  Points[9] := Point(180, 300);
  {Define the region}
  Region := CreatePolygonRgn(Points, 10, ALTERNATE);
  {Set the window to have the above defined region}
  SetWindowRgn(Handle, Region, True);
end;

Nincsenek megjegyzések:

Megjegyzés küldése