GetRegionData (gdi32)
Last changed: -167.127.24.25

.
Summary

C# Signature:

[DllImport("gdi32.dll")]
static extern int GetRegionData(IntPtr hRgn, uint dwCount,
   IntpTR lpRgnData);

User-Defined Types:

None.

Notes:

None.

Tips & Tricks:

Please add some!

Sample Code:

// First get the size in bytes of the region data for the

  using System;
  using System.Runtime.InteropServices;

  unsafe RECT[] RectsFromRegion(IntPtr hRgn)
  {
    RECT [] rects = null;
    int dataSize = GetRegionData(hRgn, 0, IntPtr.Zero);

    if (dataSize != 0)
    {
     IntPtr bytes = IntPtr.Zero;

      // Allocate as much space as the GetRegionData call
      // said was needed
      bytes = Marshal.AllocCoTaskMem(dataSize);

      // Now, make the call again to actually get the data
      int retValue = GetRegionData(hRgn, dataSize, bytes);

      // From here on out, we have the data in a buffer, and we
      // just need to convert it into a form that is more useful
      // Since pointers are used, this whole routine is 'unsafe'
      // It's a small sacrifice to make in order to get this to work.
      RGNDATAHEADER * header = (RGNDATAHEADER)bytes;

      if (header->iType == RDH_RECTANGLES)
      {
    rects = new RECT[header->nCount];

    int rectOffset = header->cbSizeOfStruct;
    for (int i=0; i<header->nCount; i++)
    {
      rects[i] = *((RECT *)((byte *)bytes+rectOffset+(Marshal.SizeOf(typeof(RECT)) *i)));
    }
      }

    }

    // Return the rectangles
    return rects;

  }

Alternative Managed API:

Do you know one? Please contribute it!

Documentation