Determining Coordinates of Rectangles in a Region

If a region consists of more than one rectangle, you can call GpiQueryRegionRects to retrieve the coordinates of the lower-left and upper-right corners of each rectangle.

If you use GpiQueryRegionRects to retrieve every rectangle requested to define a region, the function retrieves the coordinates of as many contiguous rectangles as required. GpiQueryRegionRects returns the coordinates of the rectangles that define a region to an array of RECTL structures, and returns the number of rectangles that were requested for the definition to the crcReturned field of the RGNRECT structure. Your RECTL array may not be large enough to hold all of the rectangles. Specify the maximum it can accept and request the remainder in subsequent functions if necessary.

To determine the coordinates of the rectangles that form a region, follow these steps:

The following figure shows how to determine the number of rectangles that define a region, the coordinates of the rectangles in that region, and how to create a new region using those coordinates.

#define INCL_GPIREGIONS#include <os2.h>
void fncREGN05(void){
    RGNRECT rgnrc;               /* Structure for region rectangles       */
    RECTL arcl1[5];              /* Array for determining rectangle count */
    PRECTL parcl;                /* Array for rectangle coordinates       */
    ULONG cRects = 0;            /* Total number of rectangles in region  */
    HPS hps;
    HRGN hrgn3;

    rgnrc.ircStart = 1;          /* Rectangle to start with               */
    rgnrc.crc = 5;               /* Number of rectangles to query         */
    rgnrc.ulDirection = RECTDIR_LFRT_BOTTOP;      /* Direction to query   */

    /**********************************************************************/
    /*  Determine the total number of rectangles in the region by         */
    /*  repeatedly calling GpiQueryRegionRects with an array of 5 RECTL   */
    /*  structures. The loop terminates when the function retrieves less  */
    /*  than 5 rectangles.                                                */
    /**********************************************************************/
    do {
        GpiQueryRegionRects(hps, /* Handle of presentation space          */
            hrgn3,               /* Region to query                       */
            (PRECTL) NULL,       /* Gets all rectangles in region         */
            &rgnrc,              /* Structure with rectangle data         */
            arcl1);              /* Array of structures for coordinates   */

        cRects += rgnrc.crcReturned;
    } while (rgnrc.crcReturned == rgnrc.crc); /* While 5 rects retrieved  */

    //cRects = rgnrc.crcReturned + (rgnrc.ircStart - 1);

    rgnrc.ircStart = 0;                  /* Rectangle to start with       */
    rgnrc.crc = cRects;                  /* Number of rectangles to query */

    /* Allocate enough memory for all RECTL structures.                   */
    parcl = (PRECTL) malloc(cRects * sizeof(RECTL));

    /* Fill array with coordinates of all rectangles.                     */
    GpiQueryRegionRects(hps,     /* Handle of presentation space          */
        hrgn3,                   /* Region to query                       */
        (PRECTL) NULL,           /* Gets all rectangles in region         */
        &rgnrc,                  /* Structure with rectangle data         */
        parcl);                  /* Array of structures for coordinates   */
} /* fncREGN05 */


[Back: Locating a Point with Respect to a Region]
[Next: Notices]