Click here to Skip to main content
15,867,308 members
Articles / Desktop Programming / MFC

Useful Functions for Region

Rate me:
Please Sign up or sign in to vote.
3.00/5 (8 votes)
11 Mar 20031 min read 50.3K   1.5K   21   1
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.

C++
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

  • 03-13-2003: Date posted

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.


Written By
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralSeem not function Pin
Gianfranco13-Mar-03 2:10
Gianfranco13-Mar-03 2:10 
Confused | :confused:
Hi,
I tried it on WIN/NT but it seems doesn't function.
Why?

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.