Delphi Tutorial - How to Implement Weird Shaped Windows


This is probably one of the lesser known Delphi and windows functions. The ability to have windows of shapes other than rectangular. This may sound difficult to do but in practice it is very easy. The only obvious place I can see any use in this (and even then its purely cosmetic) is an applications splash screen or about box. Please let me know if you ever find a different shaped window to be more useful than the standard rectangular one.

On to the implementation, 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);
  Region: HRgn;
  Points: array [0..11] of TPoint;

(* 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,
Points:    This a pointer to the previously defined points
10:        The number of points
ALTERNATE: The mode to be used for filling the polygon 

(* Set the window to have the above defined region *)
  SetWindowRgn(Handle, // The handle of our form
               Region, // The handle of our defined region
               True);  // The window is to be redrawn now


When you run the program your window should look like this:

Weird Shaped Window in Delphi using CreatePolygonRgn and SetWindowRgn

And that's all there is to it, now you just need to find a use for it, possibly in a splash screen.

We also have a tutorial explaining how to make a form the shape of a bitmap, again using Borland Delphi.

Delphi Central - Delphi Programming Tutorials, Hints and Tips