SetRgn... example for C++ 5.0 and later

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, then uses the fill color to create a new region.

// Declare local variables
float ZoomFactorX, ZoomFactorY; // For translating positioning information
float OffsetX, OffsetY, CurrentX, CurrentY; // To simulate mouse coordinates
float SimLeft, SimTop, SimWidth, SimHeight; // To simulate mouse coordinates
float LBRgnLeft, LBRgnTop, LBRgnWidth, LBRgnHeight, eW, eH;// For bitmap coordinates
int i; // Loop counter
// Set up some arrays to use as client area grid values for drawing a polygon.
int xPoint[] = {0, -1, 2, 1, 1, 2, -1, -2};
int yPoint[] = {0, -1, -1, 1, -1, 1, 1, 1};
ILEADRasterProcess *pRasterProc=NULL;
CoCreateInstance(CLSID_LEADRasterProcess, NULL, CLSCTX_ALL,
                 IID_ILEADRasterProcess, (void**)&pRasterProc);

// Initialize the variables that we will manipulate to simulate mouse coordinates.
OffsetX = m_LEADRasterView1.GetScaleWidth() / 15;
OffsetY = m_LEADRasterView1.GetScaleHeight() / 15;
CurrentX = OffsetX;
CurrentY = OffsetY;
// Set the zoom factors.
ZoomFactorX = m_LEADRasterView1.GetDstWidth() / m_LEADRasterView1.GetSrcWidth();
ZoomFactorY = m_LEADRasterView1.GetDstHeight() / m_LEADRasterView1.GetSrcHeight();
// 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) - (m_LEADRasterView1.GetDstLeft() / ZoomFactorX) 
                       + m_LEADRasterView1.GetSrcLeft();
LBRgnTop = (SimTop / ZoomFactorY) - (m_LEADRasterView1.GetDstTop() / ZoomFactorY)
                       + m_LEADRasterView1.GetSrcTop();
LBRgnWidth = SimWidth / ZoomFactorX;
LBRgnHeight = SimHeight / ZoomFactorY;
// Create the rectangular region, outline it, and display a message so that we can see what happened.
m_LEADRasterView1.GetRaster().SetRgnRect(LBRgnLeft, LBRgnTop,
                                         LBRgnWidth, LBRgnHeight,
                                         L_RGN_SET);
m_LEADRasterView1.SetRgnFrameType(RGNFRAME_STATIC);
MessageBox(TEXT("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) - (m_LEADRasterView1.GetDstLeft() / ZoomFactorX) 
                       + m_LEADRasterView1.GetSrcLeft();
LBRgnTop = (SimTop / ZoomFactorY) - (m_LEADRasterView1.GetDstTop() / ZoomFactorY)
                       + m_LEADRasterView1.GetSrcTop();
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.
m_LEADRasterView1.GetRaster().SetRgnEllipse(LBRgnLeft, LBRgnTop, LBRgnWidth, LBRgnHeight, L_RGN_OR);
m_LEADRasterView1.SetRgnFrameType(RGNFRAME_STATIC);
MessageBox(TEXT("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) - (m_LEADRasterView1.GetDstLeft() / ZoomFactorX) 
                       + m_LEADRasterView1.GetSrcLeft();
LBRgnTop = (SimTop / ZoomFactorY) - (m_LEADRasterView1.GetDstTop() / ZoomFactorY)
                       + m_LEADRasterView1.GetSrcTop();
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.
m_LEADRasterView1.GetRaster().SetRgnRoundRect(LBRgnLeft, LBRgnTop,
                                              LBRgnWidth, LBRgnHeight,
                                              eW, eH, L_RGN_XOR);
MessageBox(TEXT("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.
m_LEADRasterView1.GetRaster().SetPolygonSize(5);
// Fill in the array for the polygon. This simulates a user's mouse clicks.
for( i = 0; i <= 7; ++i)
{
    CurrentX = CurrentX + (xPoint[i] * OffsetX);
    CurrentY = CurrentY + (yPoint[i] * OffsetY);
    m_LEADRasterView1.GetRaster().SetPolygonX (i, (CurrentX / ZoomFactorX) 
                                              - (m_LEADRasterView1.GetDstLeft() / ZoomFactorX) 
                                              + m_LEADRasterView1.GetSrcLeft());
    m_LEADRasterView1.GetRaster().SetPolygonY (i, (CurrentY / ZoomFactorY) 
                                              - (m_LEADRasterView1.GetDstTop() / ZoomFactorY)
                                              + m_LEADRasterView1.GetSrcTop());
    if (i + 1 == m_LEADRasterView1.GetRaster().GetPolygonSize())
        m_LEADRasterView1.GetRaster().SetPolygonSize(m_LEADRasterView1.GetRaster().GetPolygonSize() + 5);
}
// Adjust the polygon size to the actual number of points.
m_LEADRasterView1.GetRaster().SetPolygonSize(i);
// Add the polygonal region, using a Boolean OR,
// and display a message so that we can see what happened.
m_LEADRasterView1.GetRaster().SetRgnPolygon(L_POLY_WINDING, L_RGN_OR);
MessageBox(TEXT("Here is the added polygon, with a Boolean OR"));
// Fill the resulting polygon with red, and display a message.
pRasterProc->Fill(m_LEADRasterView1.GetRaster(), RGB(255, 0, 0));
m_LEADRasterView1.SetRgnFrameType(RGNFRAME_NONE);
m_LEADRasterView1.ForceRepaint();
MessageBox(TEXT("Using SetRgnColor and L_RGN_SETNOT, all else will become blue."));
// Clean up memory
m_LEADRasterView1.GetRaster().FreeRgn();
// Do the final region.
m_LEADRasterView1.GetRaster().SetRgnColor(RGB(255, 0, 0), L_RGN_SETNOT);
pRasterProc->Fill(m_LEADRasterView1.GetRaster(), RGB(0, 0, 255));
m_LEADRasterView1.ForceRepaint();
// Clean up memory
m_LEADRasterView1.GetRaster().FreeRgn();
m_LEADRasterView1.GetRaster().SetPolygonSize(0);
pRasterProc->Release();