Click here to Skip to main content
15,878,970 members
Articles / Desktop Programming / MFC

CreateRegionFromFile

Rate me:
Please Sign up or sign in to vote.
5.00/5 (13 votes)
10 Dec 2001 164.2K   3.4K   70   31
Very primitive function that creates region from *.bmp files

Introduction

This very simple function creates a region from a bitmap (.bmp) file. 8, 16, 24 and 32 bit color modes are supported.

The function takes two parameters:

  • hBmp - a handle to the bitmap image
  • color - transparent color
C++
HRGN CreateRgnFromFile( HBITMAP hBmp, COLORREF color );

Using the function is very simple. For example, if you wish to set the window shape of your dialog, then in your OnInitDialog handler, do this:

C++
BOOL CFileRgnDlg::OnInitDialog()
{
    CDialog::OnInitDialog();

    // load image
    HBITMAP hBmp = (HBITMAP)LoadImage( AfxGetInstanceHandle(), 
                     "Rgn.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE );
    // if fail - nothing to do
    if ( hBmp == NULL ) return TRUE;
    // create region, let the RED color be transparent

    HRGN hRgn = CreateRgnFromFile( hBmp, RGB(255,0,0) );

    // build memory dc for background
    CDC* dc = GetDC();
    m_dcBkGrnd = CreateCompatibleDC( dc->m_hDC );
    ReleaseDC( dc );
    // select background image
    SelectObject( m_dcBkGrnd, hBmp );
    // set window size the same as image size
    SetWindowPos( NULL, 0, 0, m_dwWidth, m_dwHeight, 
                           SWP_NOZORDER | SWP_NOMOVE );
    // assign region to window
    SetWindowRgn( hRgn, FALSE );

    return TRUE;
}

History

  • December 08, 2001
    • Cut & paste bug for 16-bit mode (white color as transparent did not work)
    • Pixels in a last column has not been used
    • Now you can use non-aligned (by width) images
  • June 11, 2001
    • ExtCreateRegion NT-problem (personal thanks to Martin for reporting it)

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
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: I need some help here... (rookie!) Pin
Mikko Mononen26-Jul-00 1:53
Mikko Mononen26-Jul-00 1:53 
GeneralResource Leaks GALORE! Pin
realcodeguy13-Jul-00 14:50
realcodeguy13-Jul-00 14:50 
QuestionSample project? Pin
Member 91819-Jun-00 0:35
Member 91819-Jun-00 0:35 
Generalbroken link to source Pin
Darren Schroeder31-May-00 15:39
Darren Schroeder31-May-00 15:39 
GeneralRe: broken link to source Pin
Yuriy Zaporozhets2-Jun-00 11:30
Yuriy Zaporozhets2-Jun-00 11:30 
GeneralRe: broken link to source Pin
Uwe Keim4-Jun-00 12:08
sitebuilderUwe Keim4-Jun-00 12:08 

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.