• Welcome to Jose's Read Only Forum 2023.
 

GDI+: GdipCombineRegionPath

Started by José Roca, June 25, 2008, 03:02:42 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

José Roca



The following example creates a region from a rectangle and then uses a path to update the region. The updated region is the union of the rectangular region and the path.

C++


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

   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;
   SolidBrush solidBrush(Color(255, 255, 0, 0));

   path.AddClosedCurve(points, 6);

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

   // Form the union of the region and the interior of a path.
   rectRegion.Union(&path);

   graphics.FillRegion(&solidBrush, &rectRegion);
}


PowerBASIC


SUB GDIP_CombineRegionPath (BYVAL hdc AS DWORD)

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

   hStatus = GdipCreateFromHDC(hdc, pGraphics)

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

   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 from a rectangle.
   hStatus = GdipCreateRegionRectI(rc, pRectRegion)

   ' // Form the union of the region and the interior of a path.
   hStatus = GdipCombineRegionPath(pRectRegion, pPath, %CombineModeUnion)

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

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

END SUB


The following illustration shows the output of the preceding code.



José Roca

#1
 
The following example creates a region from a rectangle and then uses a path to update the region.

C++


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

   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;
   SolidBrush solidBrush(Color(255, 255, 0, 0));

   path.AddClosedCurve(points, 6);

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

   // Perform an exclusive OR operation on the region and a path.
   rectRegion.Xor(&path);

   graphics.FillRegion(&solidBrush, &rectRegion);
}


PowerBASIC


SUB GDIP_CombineRegionPath (BYVAL hdc AS DWORD)

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

   hStatus = GdipCreateFromHDC(hdc, pGraphics)

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

   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 from a rectangle.
   hStatus = GdipCreateRegionRectI(rc, pRectRegion)

   ' // Perform an exclusive OR operation on the region and a path.
   hStatus = GdipCombineRegionPath(pRectRegion, pPath, %CombineModeXor)

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

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

END SUB


The following illustration shows the output of the preceding code.



José Roca

#2
 
The following example creates a path. Next, the code creates a region from a rectangle and then uses the path to update the region.

C++


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

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

   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 region(rect);

   // Update the region so that it consists of all points inside a path but
   // outside the rectangular region.
   region.Complement(&path);

   graphics.FillRegion(&solidBrush, &region);
}


PowerBASIC


SUB GDIP_CombineRegionPath (BYVAL hdc AS DWORD)

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

   hStatus = GdipCreateFromHDC(hdc, pGraphics)

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

   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 from a rectangle.
   hStatus = GdipCreateRegionRectI(rc, pRectRegion)

   ' // Update the region so that it consists of all points inside a path but
   ' // outside the rectangular region.
   hStatus = GdipCombineRegionPath(pRectRegion, pPath, %CombineModeComplement)

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

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

END SUB


The following illustration shows the output of the preceding code.



José Roca

#3
 
The following example creates a region from a rectangle and then uses a path to update the region.

C++


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

   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;
   SolidBrush solidBrush(Color(255, 255, 0, 0));

   path.AddClosedCurve(points, 6);

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

   // Exclude the intersecting portion of the path interior from the region.
   rectRegion.Exclude(&path);

   graphics.FillRegion(&solidBrush, &rectRegion);
}


PowerBASIC


SUB GDIP_CombineRegionPath (BYVAL hdc AS DWORD)

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

   hStatus = GdipCreateFromHDC(hdc, pGraphics)

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

   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 from a rectangle.
   hStatus = GdipCreateRegionRectI(rc, pRectRegion)

   ' // Exclude the intersecting portion of the path interior from the region.
   hStatus = GdipCombineRegionPath(pRectRegion, pPath, %CombineModeExclude)

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

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

END SUB


The following illustration shows the output of the preceding code.



José Roca

#4
 
The following example creates a region from a rectangle and then uses a path to update the region.

C++


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

   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;
   SolidBrush solidBrush(Color(255, 255, 0, 0));

   path.AddClosedCurve(points, 6);

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

   // Update the region to the portion that intersects with the path.
   rectRegion.Intersect(&path);

   graphics.FillRegion(&solidBrush, &rectRegion);
}


PowerBASIC


SUB GDIP_CombineRegionPath (BYVAL hdc AS DWORD)

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

   hStatus = GdipCreateFromHDC(hdc, pGraphics)

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

   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 from a rectangle.
   hStatus = GdipCreateRegionRectI(rc, pRectRegion)

   ' // Exclude the intersecting portion of the path interior from the region.
   hStatus = GdipCombineRegionPath(pRectRegion, pPath, %CombineModeIntersect)

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

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

END SUB


The following illustration shows the output of the preceding code.