January 27, 2021
Hot Topics:

Creating Odd Shaped Forms

  • By John Percival
  • Send Email »
  • More Articles »

Shifting Regions

Public Declare Function _ OffsetRgn _
   Lib "gdi32" _
   (ByVal hRgn As Long, _
   ByVal x As Long, _
   ByVal y As Long) As Long

This function allows you to shift the specified region by x, y pixels.
It can return the following constants:

  • COMPLEXREGION: if the region has borders that overlap each other
  • SIMPLEREGION: if the borders of the region do not overlap each other
  • NULLREGION: if the region is empty
  • ERRORAPI: if the combined region could not be created
  • Joining Regions

    Public Declare Function _
       CombineRgn Lib "gdi32" _
       (ByVal hDestRgn As Long, _
       ByVal hSrcRgn1 As Long, _
       ByVal hSrcRgn2 As Long, _
       ByVal nCombineMode As Long)_
       As Long

    This API provides the ability to join two regions using a variety of different methods. The source regions are specified in hSrcRgn1 and hSrcRgn2, and the combined region will be specified in hDestRgn. Note that the destination region must already be created as a region before this API is called. To get round this, create a rectangle, left, top, width and height dimensions of 0, then call the CombineRgn API with this region handle. The nCombineMode can be one of the following:

  • RGN_AND: hDestRgn is set to the intersection of the two source regions (the area common to both)
  • RGN_COPY: hDestRgn is set to a copy of hSrcRgn1
  • RGN_DIFF: hDestRgn is set to the area in hSrcRgn1 that is not present in hSrcRgn2.
  • RGN_OR: hDestRgn is set to the union of the two source regions (the area that appears in either source region)
  • RGN_XOR: hDestRgn is the exclusive OR of the two source regions (the area that appears in either the source region, but not both)
  • It returns the same values as above.

    Initializing the Window

    Public Declare Function SetWindowRgn _
       Lib "user32" _
       (ByVal hWnd As Long, ByVal hRgn _
       As Long, ByVal bRedraw As Boolean) _
       As Long

    Use this function to make the window into your custom shape. The hWnd is the window handle of the window on which to set the region. This can be anything that provides a hWnd, including Forms, Command Buttons, Picture Boxes, etc. Set bRedraw to True to automatically redraw the window when the new region has been applied.
    This will return 0 on failure.

    Tidying Up

    Public Declare Function DeleteObject _
       Lib "gdi32" _
       (ByVal hObject As Long)_
       As Long

    As with most GDI functions, the region handles must be "tidied up". This is done using the DeleteObject API. Once you have set the window's region, you can delete all the regions, except for the one that you used in SetWindowRgn. You should return the form to its original shape by passing hRgn=0 to the SetWindowRgn function, then deleting the region handle.

    Page 3 of 4

    This article was originally published on November 20, 2002

    Enterprise Development Update

    Don't miss an article. Subscribe to our newsletter below.

    Thanks for your registration, follow us on our social networks to keep up-to-date