• Welcome to Jose's Read Only Forum 2023.
 

GDI+: GdipSetClipRegion

Started by José Roca, June 23, 2008, 07:31:32 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

José Roca



The following example uses a rectangle to update a clipping region and then draws a rectangle that demonstrates the updated clipping region.

C++


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

   // Create a Region object.
   Region clipRegion(Rect(100, 50, 200, 100));

   // Set the clipping region with hRegion.
   graphics.SetClip(&clipRegion);

   // Fill a rectangle to demonstrate the clipping region.
   graphics.FillRectangle(&SolidBrush(Color(255, 0, 0, 0)), 0, 0, 500, 500);
}


PowerBASIC


SUB GDIP_SetClipRegion (BYVAL hdc AS DWORD)

   LOCAL hStatus AS LONG
   LOCAL pGraphics AS DWORD
   LOCAL pBrush AS DWORD
   LOCAL pRegion AS DWORD
   LOCAL rcf AS RECTF

   hStatus = GdipCreateFromHDC(hdc, pGraphics)

   ' // Create a SolidBrush
   hStatus = GdipCreateSolidFill(GDIP_ARGB(255, 0, 0, 255), pBrush)

   ' // Create a Region object, and set the clipping region to its exclusion.
   rcf.x = 100 : rcf.y = 50 : rcf.Width = 200 : rcf.Height = 200
   hStatus = GdipCreateRegionRect(rcf, pRegion)
   hStatus = GdipSetClipRegion(pGraphics, pRegion, %CombineModeReplace)

   ' // Fill a rectangle to demonstrate the clipping region.
   hStatus = GdipFillRectangle(pGraphics, pBrush, 0, 0, 400, 340)

   ' // Cleanup
   IF pRegion THEN GdipDeleteRegion(pRegion)
   IF pBrush THEN GdipDeleteBrush(pBrush)
   IF pGraphics THEN GdipDeleteGraphics(pGraphics)

END SUB




José Roca

#1
 
The following example sets a clipping region and updates the clipping region. It then draws rectangles to demonstrate the effective clipping region.


SUB GDIP_SetClipRegion2 (BYVAL hdc AS DWORD)

   LOCAL hStatus AS LONG
   LOCAL pGraphics AS DWORD
   LOCAL pBrush AS DWORD
   LOCAL pClipRegion AS DWORD
   LOCAL pIntersectRegion AS DWORD
   LOCAL pBlackPen AS DWORD
   LOCAL pRedPen AS DWORD
   LOCAL rcf AS RECTF
   LOCAL cliprcf AS RECTF
   LOCAL intersectrcf AS RECTF

   hStatus = GdipCreateFromHDC(hdc, pGraphics)

   ' // Set the clipping region.
   rcf.x = 0 : rcf.y = 0 : rcf.Width = 200 : rcf.Height = 200
   hStatus = GdipCreateRegionRect(rcf, pClipRegion)
   hStatus = GdipSetClipRegion(pGraphics, pClipRegion, %CombineModeReplace)

   ' // Update the clipping region to the portion of the specified region that
   ' // intersects with the current clipping region of the graphics object.
   rcf.x = 100 : rcf.y = 100 : rcf.Width = 200 : rcf.Height = 200
   hStatus = GdipCreateRegionRect(rcf, pIntersectRegion)
   hStatus = GdipSetClipRegion(pGraphics, pIntersectRegion, %CombineModeReplace)

   ' // Fill a rectangle to demonstrate the effective clipping region.
   hStatus = GdipCreateSolidFill(GDIP_ARGB(255, 0, 0, 255), pBrush)
   hStatus = GdipFillRectangle(pGraphics, pBrush, 0, 0, 500, 500)

   ' // Reset the clipping region to infinite.
   hStatus = GdipResetClip(pGraphics)

   ' // Draw rectangles
   hStatus = GdipGetRegionBounds(pClipRegion, pGraphics, cliprcf)
   hStatus = GdipCreatePen1(GDIP_ARGB(255, 0, 0, 0), 1, %UnitWorld, pBlackPen)
   hStatus = GdipDrawRectangle(pGraphics, pBlackPen, cliprcf.x, cliprcf.y, cliprcf.Width, cliprcf.Height)
   hStatus = GdipGetRegionBounds(pIntersectRegion, pGraphics, intersectrcf)
   hStatus = GdipCreatePen1(GDIP_ARGB(255, 255, 0, 0), 1, %UnitWorld, pRedPen)
   hStatus = GdipDrawRectangle(pGraphics, pRedPen, intersectrcf.x, intersectrcf.y, intersectrcf.Width, intersectrcf.Height)

   ' // Cleanup
   IF pBlackPen THEN GdipDeletePen(pBlackPen)
   IF pRedPen THEN GdipDeletePen(pRedPen)
   IF pBrush THEN GdipDeleteBrush(pBrush)
   IF pIntersectRegion THEN GdipDeleteRegion(pIntersectRegion)
   IF pClipRegion THEN GdipDeleteRegion(pClipRegion)
   IF pGraphics THEN GdipDeleteGraphics(pGraphics)

END SUB