RegionData


Description:
This example shows how to get a byte array that is the data behind a region. It also shows how to take the byte array and create a region out of it.
 
Code:
Option Explicit

Declare Function GetRegionData Lib "gdi32" (ByVal hRgn As Long, _
   ByVal dwCount As Long, lpRgnData As Any) As Long
   
Declare Function ExtCreateRegion Lib "gdi32" (lpXform As Any, _
   ByVal nCount As Long, lpRgnData As Any) As Long
   
Declare Function CreateRectRgn Lib "gdi32" (ByVal X1 As Long, ByVal _
   Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
   
Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) _
   As Long
   
Declare Function SetWindowRgn Lib "user32" (ByVal hWnd As Long, _
   ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long

Public Sub Example()

    Dim hRgn As Long
    Dim nRegionSize As Long 'Size of region structure
    Dim nArray() As Byte 'Region structure
    
    'Create a simple region
    hRgn = CreateRectRgn(0, 0, 40, 40)
    
    'Found out the size of the region, and set the array
    'to the necessary size.
    nRegionSize = GetRegionData(hRgn, 0, ByVal 0)
    ReDim nArray(0 To nRegionSize - 1)
    
    'Copy the region data from the region to the byte array
    GetRegionData hRgn, nRegionSize, nArray(0)
    
    'Delete the region
    DeleteObject hRgn
            
    'nArray now contains the information for the region, and
    'nRegionSize is the number of elements in the array.
            
    'Recreate the region using the byte array
    hRgn = ExtCreateRegion(ByVal 0, nRegionSize, nArray(0))
    
    'Finally, set the region to a window just to make sure
    'this actually works
    SetWindowRgn Form1.hWnd, hRgn, True

End Sub
 
Sample Usage:
 
n/a