Interactive PowerBasic Forum

Legacy Software (PBWIN 9.0+/PBCC 5.0+) => Source Code => Graphics and Multimedia => GDI+ (GDI Plus) => Topic started by: José Roca on June 26, 2008, 05:32:12 PM

Title: GDI+: GdipGetRegionHRgn
Post by: José Roca on June 26, 2008, 05:32:12 PM


The following example creates a Windows GDI+ region from a path and then uses the GDI+ region to create a GDI region. The code then uses a GDI function to display the GDI region.

C++


VOID Example_GetHRGN(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)};

   GraphicsPath path;
   path.AddClosedCurve(points, 6);

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

   // Get a handle to a GDI region.
   HRGN hRegion;
   hRegion = pathRegion.GetHRGN(&graphics);

   // Use GDI to display the region.
   HBRUSH hBrush = CreateSolidBrush(RGB(255, 0, 0));
   FillRgn(hdc, hRegion, hBrush);

   DeleteObject(hBrush);
   DeleteObject(hRegion);
}


PowerBASIC


SUB GDIP_GetRegionHRgn (BYVAL hdc AS DWORD)

   LOCAL hStatus AS LONG
   LOCAL pGraphics AS DWORD
   LOCAL pPath AS DWORD
   LOCAL pPathRegion AS DWORD
   DIM   pts(5) AS POINTL

   hStatus = GdipCreateFromHDC(hdc, pGraphics)

   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

   ' // Create a region from a path.
   hStatus = GdipCreatePath(%FillModeAlternate, pPath)
   hStatus = GdipAddPathClosedCurveI(pPath, pts(0), 6)

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

   ' // Get a handle to a GDI region.
   LOCAL hRegion AS DWORD
   hStatus = GdipGetRegionHRgn(pPathRegion, pGraphics, hRegion)

   ' // Use GDI to display the region.
   LOCAL hBrush AS DWORD
   hBrush = CreateSolidBrush(RGB(255, 0, 0))
   FillRgn(hdc, hRegion, hBrush)

   ' // Cleanup
   IF hBrush THEN DeleteObject(hBrush)
   IF hRegion THEN DeleteObject(hRegion)
   IF pPathRegion THEN GdipDeleteRegion(pPathRegion)
   IF pPath THEN GdipDeletePath(pPath)
   IF pGraphics THEN GdipDeleteGraphics(pGraphics)

END SUB


The following illustration shows the output of the preceding code.

(http://www.jose.it-berater.org/captures/GdipGetRegionHRgn.png)