Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Useful functions for Region

0.00/5 (No votes)
11 Mar 2003 2  
Functions for tiling region and stretching region.

Sample Image - appspeed.gif

Introduction

When I wrote the software SkinMagic Toolkit for my company, I needed to tile a region or stretch a region within a bound rectangle. I implemented it in two functions CreateTileRegion and CreateStretchRegion.

Using the code

To use the code, just add rgntool.cpp and rgntool.h to your project. Call CreateTileRegion or CreateStretchRegion to create the new region. When you run the demo project, please double click mouse left button in the window's client, to show the effect.

HRGN CreateStretchRgn( HRGN hSrcRgn,  //the source region handle

                       float xScale , //x-axis scale

                       float yScale , //y-axis scale

                       int xOffset ,  //x-axis offset

                       int yOffset )  //y-axis offset

{
    XFORM xForm;
    xForm.eDx = 0;
    xForm.eDy = 0;
    xForm.eM11 = xScale;
    xForm.eM12 = 0;
    xForm.eM21 = 0;
    xForm.eM22 = yScale;
    HRGN hRgn = NULL ;
    
    DWORD dwCount = GetRegionData( hSrcRgn , 0 , NULL );
    BYTE* pRgnData =(BYTE*) malloc( dwCount );
    if( pRgnData )
    {
        dwCount = GetRegionData( hSrcRgn , dwCount , (RGNDATA*)pRgnData );
        hRgn = ExtCreateRegion( &xForm , dwCount , (RGNDATA*)pRgnData );
        free( pRgnData );
      if( hRgn )
      {
        OffsetRgn( hRgn , xOffset, yOffset );
        return hRgn;

      }
  }
  return NULL;
 }

HRGN CreateTitleRgn( HRGN hSrcRgn,    //the source region handle

                     SIZE szSize ,    //the source region rect size

                     RECT rcBound )   //the bound rectangle

{
  HRGN hRgn = CreateRectRgn( 0,0,10,10 );
  HRGN hTempRgn1 = CreateRectRgn( 0,0,10,10 );
  HRGN hTempRgn2 =CreateRectRgn( 0,0,10,10 );

  CombineRgn( hTempRgn2 , hSrcRgn , hTempRgn1 , RGN_COPY );
  CombineRgn( hRgn , hSrcRgn , hTempRgn1 , RGN_COPY );
  OffsetRgn( hRgn , rcBound.left , rcBound.top );
  int xOffset=0 , yOffset=0;
  for (yOffset = rcBound.top ; yOffset < rcBound.bottom ; yOffset += szSize.cy)
  {
    for (xOffset=rcBound.left ; xOffset < rcBound.right ; xOffset += szSize.cx)
    {
      CombineRgn( hTempRgn1 , hTempRgn2 , hSrcRgn , RGN_COPY );
      OffsetRgn( hTempRgn1 , xOffset , yOffset );
      CombineRgn( hRgn , hRgn , hTempRgn1 , RGN_OR );
    }
  }
  DeleteObject( hTempRgn1 );
  DeleteObject( hTempRgn2 );
  hTempRgn1 = CreateRectRgnIndirect( &rcBound );
  CombineRgn( hRgn , hRgn , hTempRgn1 , RGN_AND );
  DeleteObject( hTempRgn1 );
  return hRgn;
}

Visit here for my other Win32 software.

History

Date Posted: 03-13-2003

Copyright

The code is free, if you want to change the source code in order to improve the features, performance, etc., please send me the new source code so that I can have a look at it. The changed source code should contain descriptions of what you have changed, and of course your name. The only thing you MAY NOT CHANGE is the ORIGINAL COPYRIGHT INFORMATION.

Acknowledgements

First thanks to my boss who permitted me to post the code in this site.

Second thanks to the author (I'm sorry for not getting his/her name) who wrote CreateRgnFromBitmap.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here