Click here to Skip to main content
12,823,725 members (64,688 online)
Click here to Skip to main content
Add your own
alternative version


65 bookmarked
Posted 26 May 2000


, 10 Dec 2001
Rate this:
Please Sign up or sign in to vote.
Very primitive function that creates region from *.bmp files.


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.
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:

BOOL CFileRgnDlg::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;


  • 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 about it).


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


About the Author

No Biography provided

You may also be interested in...

Comments and Discussions

GeneralRe: I need some help here... (rookie!) Pin
memon26-Jul-00 2:53
sussmemon26-Jul-00 2:53 
GeneralResource Leaks GALORE! Pin
RealCodeGuy13-Jul-00 15:50
sussRealCodeGuy13-Jul-00 15:50 
QuestionSample project? Pin
Chris Holt19-Jun-00 1:35
sussChris Holt19-Jun-00 1:35 
Generalbroken link to source Pin
Darren Schroeder31-May-00 16:39
sussDarren Schroeder31-May-00 16:39 
GeneralRe: broken link to source Pin
Yuriy2-Jun-00 12:30
sussYuriy2-Jun-00 12:30 
GeneralRe: broken link to source Pin
Uwe Keim4-Jun-00 13:08
sussUwe Keim4-Jun-00 13: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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170308.1 | Last Updated 11 Dec 2001
Article Copyright 2000 by Yuriy Zaporozhets
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid