SetRgn... example for Delphi
This example demonstrates the SetRgnEllipse, SetRgnRect, SetRgnRoundRect, and SetRgnPolygon methods by simulating client area coordinates. Normally, you would get the coordinates interactively from mouse events and would use Windows GDI functions to outline the shape of each region before it is created.
To create the polygonal region, this example uses the PolygonSize and PolygonPoint properties.
Also, to demonstrate the SetRgnColor method, it uses the Fill method to fill the region with a solid color, then uses the fill color to create a new region.
procedure TForm1.Button1Click(Sender: TObject);
const
{Set up arrays to use as client area grid values for drawing a polygon.}
xPoint : array[0..7] of integer = (0, -1, 2, 1, 1, 2, -1, -2);
yPoint : array[0..7] of integer = (0, -1, -1, 1, -1, 1, 1, 1);
var
{Declare local variables. }
ZoomFactorX: Single; {Used for translating positioning information }
ZoomFactorY: Single; {Used for translating positioning information }
OffsetX, OffsetY, CurrentX, CurrentY: Integer; {To simulate mouse coordinates }
SimLeft, SimTop, SimWidth, SimHeight: Integer; {To simulate mouse coordinates }
LBRgnLeft, LBRgnTop, LBRgnWidth, LBRgnHeight, eW, eH: integer; {For bitmap coordinates }
i: Integer; {Loop counter }
polyPoint : TPoint;
begin
{Initialize the variables that we will manipulate to simulate mouse coordinates. }
OffsetX := Round(Lead1.Width div 15);
OffsetY := Round(Lead1.Height div 15);
CurrentX := OffsetX;
CurrentY := OffsetY;
{Set the zoom factors. }
ZoomFactorX := Lead1.DstWidth / Lead1.SrcWidth;
ZoomFactorY := Lead1.DstHeight / Lead1.SrcHeight;
{Start by simulating the drawing of a rectangle. }
SimLeft := CurrentX;
SimTop := CurrentY;
SimWidth := OffsetX * 2;
SimHeight := OffsetY * 2;
{Translate the simulated client coordinates to bitmap coordinates. }
LBRgnLeft := Round((SimLeft / ZoomFactorX) - (Lead1.DstLeft / ZoomFactorX) + Lead1.SrcLeft);
LBRgnTop := Round((SimTop / ZoomFactorY) - (Lead1.DstTop / ZoomFactorY) + Lead1.SrcTop);
LBRgnWidth := Round(SimWidth / ZoomFactorX);
LBRgnHeight := Round(SimHeight / ZoomFactorY);
{Create the rectangular region, outline it, and display a message so that we can see what happened. }
Lead1.SetRgnRect(LBRgnLeft, LBRgnTop, LBRgnWidth, LBRgnHeight, L_RGN_SET);
Lead1.RgnFrameType := ftStatic;
ShowMessage('Here is the rectangular region');
{Move the current mouse position so that we overlap the existing region. }
CurrentX := CurrentX + OffsetX;
CurrentY := CurrentY + OffsetY;
{Next, simulate the drawing of an ellipse. }
SimLeft := CurrentX;
SimTop := CurrentY;
SimWidth := OffsetX * 3;
SimHeight := OffsetY * 2;
{Translate the simulated client coordinates to bitmap coordinates. }
LBRgnLeft := Round((SimLeft / ZoomFactorX) - (Lead1.DstLeft / ZoomFactorX) + Lead1.SrcLeft);
LBRgnTop := Round((SimTop / ZoomFactorY) - (Lead1.DstTop / ZoomFactorY) + Lead1.SrcTop);
LBRgnWidth := Round(SimWidth / ZoomFactorX);
LBRgnHeight := Round(SimHeight / ZoomFactorY);
{Add the elliptical region, using a Boolean OR, }
{and display a message so that we can see what happened. }
Lead1.SetRgnEllipse(LBRgnLeft, LBRgnTop, LBRgnWidth, LBRgnHeight, L_RGN_OR);
Lead1.RgnFrameType := ftStatic;
ShowMessage('Here is the added ellipse, with a Boolean OR');
{Move the current mouse position so that we overlap the existing region. }
CurrentX := CurrentX + OffsetX;
CurrentY := CurrentY + OffsetY;
{Next, simulate the drawing of a rounded rectangle. }
SimLeft := CurrentX;
SimTop := CurrentY;
SimWidth := OffsetX * 3;
SimHeight := OffsetY * 2;
{Translate the simulated client coordinates to bitmap coordinates. }
LBRgnLeft := Round((SimLeft / ZoomFactorX) - (Lead1.DstLeft / ZoomFactorX) + Lead1.SrcLeft);
LBRgnTop := Round((SimTop / ZoomFactorY) - (Lead1.DstTop / ZoomFactorY) + Lead1.SrcTop);
LBRgnWidth := Round(SimWidth / ZoomFactorX);
LBRgnHeight := Round(SimHeight / ZoomFactorY);
{Define an ellipse to use for the corners. }
eW := LBRgnWidth div 4;
eH := LBRgnHeight div 4;
{Add the rounded rectangular region, using a Boolean exclusive OR, }
{and display a message so that we can see what happened. }
Lead1.SetRgnRoundRect(LBRgnLeft, LBRgnTop, LBRgnWidth, LBRgnHeight, eW, eH,
L_RGN_XOR);
ShowMessage('Here is the rounded rectangle, with a Boolean XOR');
{Move the current mouse position so that we overlap the existing region. }
CurrentX := CurrentX + (2 * OffsetX);
CurrentY := CurrentY + (2 * OffsetY);
{Initialize the polygon size using an arbitrary number. }
{This number will be increased dynamically. }
Lead1.PolygonSize := 5;
{Fill in the array for the polygon. This simulates a user's mouse clicks. }
for i := 0 to 7 do
begin
CurrentX := CurrentX + (xPoint[i] * OffsetX);
CurrentY := CurrentY + (yPoint[i] * OffsetY);
polyPoint.x := Round((CurrentX / ZoomFactorX) - (Lead1.DstLeft / ZoomFactorX) + Lead1.SrcLeft);
polyPoint.y := Round((CurrentY / ZoomFactorY) - (Lead1.DstTop / ZoomFactorY) + Lead1.SrcTop);
Lead1.PolygonPoint[i] := polyPoint;
if i + 1 = Lead1.PolygonSize Then Lead1.PolygonSize := Lead1.PolygonSize + 5;
end;
{Adjust the polygon size to the actual number of points. }
Lead1.PolygonSize := i;
{Add the polygonal region, using a Boolean OR, }
{and display a message so that we can see what happened. }
Lead1.SetRgnPolygon(L_POLY_WINDING, L_RGN_OR);
ShowMessage('Here is the added polygon, with a Boolean OR');
{Fill the resulting polygon with red, and display a message. }
Lead1.Fill(RGB(255, 0, 0));
Lead1.RgnFrameType := ftNone;
Lead1.ForceRepaint;
ShowMessage('Using SetRgnColor and L_RGN_SETNOT, all else will become blue.');
{Clean up memory }
Lead1.FreeRgn;
{Do the final region. }
Lead1.SetRgnColor(RGB(255, 0, 0), L_RGN_SETNOT);
Lead1.Fill(RGB(0, 0, 255));
Lead1.ForceRepaint;
{Clean up memory }
Lead1.FreeRgn;
Lead1.PolygonSize := 0;
end;