• Welcome to Jose's Read Only Forum 2023.
 

GDI+: GdipCloneRegion

Started by José Roca, June 25, 2008, 02:18:36 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

José Roca



The following example creates two regions, one from a rectangle and the other from a path. Next, the code clones the region that was created from a path and uses a solid brush to fill the cloned region. Then, it forms the union of the two regions and fills it.

C++


VOID Example_Clone(HDC hdc)
{
   Graphics graphics(hdc);

   SolidBrush solidBrush(Color(255, 255, 0, 0));
   SolidBrush alphaBrush(Color(128, 0, 0, 255));

   Point points[] = {
      Point(110, 20),
      Point(120, 30),
      Point(100, 60),
      Point(120, 70),
      Point(150, 60),
      Point(140, 10)};

   Rect rect(65, 15, 70, 45);
   GraphicsPath path;

   path.AddClosedCurve(points, 6);

   // Create a region from a rectangle.
   Region rectRegion(rect);

   // Create a region from a curved path.
   Region pathRegion(&path);

   // Make a copy (clone) of the curved region.
   Region* pClonedRegion = pathRegion.Clone();

   // Fill the cloned region with a red brush.
   graphics.FillRegion(&solidBrush, pClonedRegion);

   // Form the union of the cloned region and the rectangular region.
   pClonedRegion->Union(&rectRegion);

   // Fill the union of the two regions with a semitransparent blue brush.
   graphics.FillRegion(&alphaBrush, pClonedRegion);
   
   delete pClonedRegion;
}


PowerBASIC


SUB GDIP_CloneRegion (BYVAL hdc AS DWORD)

   LOCAL hStatus AS LONG
   LOCAL pGraphics AS DWORD
   LOCAL pPath AS DWORD
   LOCAL pSolidBrush AS DWORD
   LOCAL pAlphaBrush AS DWORD
   LOCAL pRectRegion AS DWORD
   LOCAL pPathRegion AS DWORD
   LOCAL pClonedRegion AS DWORD
   LOCAL rc AS RECTL
   DIM   pts(5) AS POINTL

   hStatus = GdipCreateFromHDC(hdc, pGraphics)

   hStatus = GdipCreateSolidFill(GDIP_ARGB(255, 255, 0, 0), pSolidBrush)
   hStatus = GdipCreateSolidFill(GDIP_ARGB(128, 0, 0, 255), pAlphaBrush)

   pts(0).x = 110 : pts(0).y = 20
   pts(1).x = 120 : pts(1).y = 30
   pts(2).x = 100 : pts(2).y = 60
   pts(3).x = 120 : pts(3).y = 70
   pts(4).x = 150 : pts(4).y = 60
   pts(5).x = 140 : pts(5).y = 10

   SetRect rc, 65, 15, 70, 45
   hStatus = GdipCreatePath(%FillModeAlternate, pPath)

   hStatus = GdipAddPathClosedCurveI(pPath, pts(0), 6)

   ' // Create a Region object from a rectangle.
   hStatus = GdipCreateRegionRectI(rc, pRectRegion)

   ' // Create a region from a curved path.
   hStatus = GdipCreateRegionPath(pPath, pPathRegion)

   ' // Make a copy (clone) of the curved region.
   hStatus = GdipCloneRegion (pPathRegion, pClonedRegion)

   ' // Fill the cloned region with a red brush.
   hStatus = GdipFillRegion(pGraphics, pSolidBrush, pClonedRegion)

   ' // Form the union of the cloned region and the rectangular region.
   hStatus = GdipCombineRegionRectI(pClonedRegion, rc, %CombineModeUnion)

   ' // Fill the union of the two regions with a semitransparent blue brush.
   hStatus = GdipFillRegion(pGraphics, pAlphaBrush, pClonedRegion)

   ' // Cleanup
   IF pRectRegion THEN GdipDeleteRegion(pRectRegion)
   IF pPathRegion THEN GdipDeleteRegion(pPathRegion)
   IF pSolidBrush THEN GdipDeleteBrush(pSolidBrush)
   IF pAlphaBrush THEN GdipDeleteBrush(pAlphaBrush)
   IF pPath THEN GdipDeletePath(pPath)
   IF pGraphics THEN GdipDeleteGraphics(pGraphics)

END SUB


The following illustration shows the output of the preceding code.