Mega Code Archive

 
Categories / Delphi / Forms
 

Create non rectangular forms

Title: Create non-rectangular forms Question: This sample code shows how to create custom shaped forms. By creating simple Regions and combining them into complex Regions you can give your form any shape you want. Answer: // Code to set the form to an Elliptic shape (use alt+F4 to close) procedure TForm1.FormCreate(Sender: TObject); var hRgn: THandle; begin // create a elliptic region 10 pixels smaller than the form hRgn := CreateEllipticRgn(10, 10, Width - 20, Height - 20); // set the new region SetWindowRgn(Handle, hRgn, False); end; // Code to create a form with a hole procedure TForm1.FormCreate(Sender: TObject); var hRgn1, hRgn2: THandle; begin // create a rectangular region with the same size as the form hRgn1 := CreateRectRgn(0, 0, Width, Height); // create a circular region 100 x 100 in the center of the form hRgn2 := CreateEllipticRgn((Width div 2) - 50, (Height div 2) - 50, (Width div 2) + 50, (Height div 2) + 50); // combine the regions by subtracting the circular region // from the rectangular one CombineRgn(hRgn1, hRgn1, hRgn2, RGN_DIFF); // set the new region SetWindowRgn(Handle, hRgn1, False); // delete region 2, region 1 is owned and deleted by windows DeleteObject(hRgn2); end;