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, PolygonX, and PolygonY properties.
Also, to demonstrate the SetRgnColor method, it uses the Fill method to fill the region with a solid color, and then uses the fill color to create a new region.
Const
//Arrays for polygon points
xPoint: Array[1..8]of integer = (0, -1, 2, 1, 1, 2, -1, -2);
yPoint: Array[1..8]of integer = (0, -1, -1, 1, -1, 1, 1, 1);
var
ZoomFactorX: Single ;//Used for translating positioning information
ZoomFactorY: Single ;//Used for translating positioning information
OffsetX, OffsetY, CurrentX, CurrentY: Single;//To simulate mouse coordinates
SimLeft, SimTop, SimWidth, SimHeight: Single;//To simulate mouse coordinates
LBRgnLeft, LBRgnTop, LBRgnWidth, LBRgnHeight, eW, eH: Single;//For bitmap coordinates
i: Integer; //Loop counter
sRet: Smallint;
RasterProc: LEADRasterProcess;
begin
RasterProc:= CreateComObject (CLASS_LEADRasterProcess) as LEADRasterProcess;
//Initialize the variables that we will manipulate to simulate mouse coordinates.
LEADRasterView1.ScaleMode:= 3;
OffsetX := LEADRasterView1.Raster.ScaleWidth / 15;
OffsetY := LEADRasterView1.Raster.ScaleHeight / 15;
CurrentX := OffsetX;
CurrentY := OffsetY;
//Set the zoom factors.
ZoomFactorX := LEADRasterView1.DstWidth / LEADRasterView1.SrcWidth;
ZoomFactorY := LEADRasterView1.DstHeight / LEADRasterView1.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 := (SimLeft / ZoomFactorX) -
(LEADRasterView1.DstLeft / ZoomFactorX)
+ LEADRasterView1.SrcLeft;
LBRgnTop := (SimTop / ZoomFactorY) -
(LEADRasterView1.DstTop / ZoomFactorY)
+ LEADRasterView1.SrcTop;
LBRgnWidth := SimWidth / ZoomFactorX;
LBRgnHeight := SimHeight / ZoomFactorY;
//Create the rectangular region, outline it, and display a message so that we can see what happened.
LEADRasterView1.Raster.SetRgnRect (LBRgnLeft, LBRgnTop, LBRgnWidth, LBRgnHeight, L_RGN_SET);
LEADRasterView1.RgnFrameType := RGNFRAME_STATIC;
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 := (SimLeft / ZoomFactorX) -
(LEADRasterView1.DstLeft / ZoomFactorX)
+ LEADRasterView1.SrcLeft;
LBRgnTop := (SimTop / ZoomFactorY) -
(LEADRasterView1.DstTop / ZoomFactorY)
+ LEADRasterView1.SrcTop;
LBRgnWidth := SimWidth / ZoomFactorX;
LBRgnHeight := SimHeight / ZoomFactorY;
//Add the elliptical region, using a Boolean OR,
//and display a message so that we can see what happened.
LEADRasterView1.Raster.SetRgnEllipse (LBRgnLeft, LBRgnTop, LBRgnWidth, LBRgnHeight, L_RGN_OR);
LEADRasterView1.RgnFrameType := RGNFRAME_STATIC;
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:= (SimLeft / ZoomFactorX)
- (LEADRasterView1.DstLeft / ZoomFactorX)
+ LEADRasterView1.SrcLeft;
LBRgnTop:= (SimTop / ZoomFactorY)
- (LEADRasterView1.DstTop / ZoomFactorY)
+ LEADRasterView1.SrcTop;
LBRgnWidth:= SimWidth / ZoomFactorX;
LBRgnHeight:= SimHeight / ZoomFactorY;
//Define an ellipse to use for the corners.
eW := LBRgnWidth / 4;
eH := LBRgnHeight / 4;
//Add the rounded rectangular region, using a Boolean exclusive OR,
//and display a message so that we can see what happened.
LEADRasterView1.Raster.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.
LEADRasterView1.Raster.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+1] * OffsetX);
CurrentY:= CurrentY + (yPoint[i+1] * OffsetY);
LEADRasterView1.Raster.PolygonX [i]:= (CurrentX / ZoomFactorX)
- (LEADRasterView1.DstLeft / ZoomFactorX)
+ LEADRasterView1.SrcLeft;
LEADRasterView1.Raster.PolygonY [i]:= (CurrentY / ZoomFactorY)
- (LEADRasterView1.DstTop / ZoomFactorY)
+ LEADRasterView1.SrcTop;
if i + 1 = LEADRasterView1.Raster.PolygonSize then
LEADRasterView1.Raster.PolygonSize:= LEADRasterView1.Raster.PolygonSize + 5;
end;
//Adjust the polygon size to the actual number of points.
LEADRasterView1.Raster.PolygonSize := 8;
//Add the polygonal region, using a Boolean OR,
//and display a message so that we can see what happened.
LEADRasterView1.Raster.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.
RasterProc.Fill (LEADRasterView1.Raster, RGB(255, 0, 0));
LEADRasterView1.RgnFrameType:= RGNFRAME_NONE;
LEADRasterView1.ForceRepaint (sRet);
ShowMessage ('Using SetRgnColor and L_RGN_SETNOT, all else will become blue.');
//Clean up memory
LEADRasterView1.Raster.FreeRgn ();
//Do the final region.
LEADRasterView1.Raster.SetRgnColor (RGB(255, 0, 0), L_RGN_SETNOT);
RasterProc.Fill (LEADRasterView1.Raster, RGB(0, 0, 255));
LEADRasterView1.ForceRepaint(sRet);
//Clean up memory
LEADRasterView1.Raster.FreeRgn();
LEADRasterView1.Raster.PolygonSize:= 0;
end;