Click here to Skip to main content
13,409,878 members (36,938 online)
Click here to Skip to main content
Add your own
alternative version


66 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

GeneralNot working on Windows Seven Pin
Dom306-May-11 22:19
memberDom306-May-11 22:19 
GeneralRe: Not working on Windows Seven Pin
Member 112345026-May-11 11:01
memberMember 112345026-May-11 11:01 
GeneralMy vote of 5 Pin
ryan20fun9-Feb-11 21:15
memberryan20fun9-Feb-11 21:15 
GeneralRotated Edit Box Pin
ZAKIR2-Sep-04 4:57
sussZAKIR2-Sep-04 4:57 
GeneralCreateRgnFromFile question Pin
rioan31-Jul-04 11:23
sussrioan31-Jul-04 11:23 
GeneralRe: CreateRgnFromFile question Pin
Yuriy_Zaporozhets5-Oct-04 14:59
memberYuriy_Zaporozhets5-Oct-04 14:59 
GeneralCustom controls ... Pin
Adrian Datcu10-Jun-04 1:58
memberAdrian Datcu10-Jun-04 1:58 
GeneralFantastic !!! Pin
HarleyDude28-Apr-04 17:37
sussHarleyDude28-Apr-04 17:37 
GeneralGreat example Pin
Ubersnack10-Jun-03 1:42
memberUbersnack10-Jun-03 1:42 
GeneralAdapting this Pin
Judd11-Dec-02 4:27
memberJudd11-Dec-02 4:27 
Generalfinally found it Pin
Kevin Smith1-Nov-02 10:10
memberKevin Smith1-Nov-02 10:10 
GeneralLarge Fonts Pin
Matt Philmon5-Feb-02 12:11
memberMatt Philmon5-Feb-02 12:11 
GeneralRegionToBitmap class... Pin
saturn5-Feb-02 2:53
membersaturn5-Feb-02 2:53 
GeneralArticle screwed up... no links Pin
Matt Philmon11-Dec-01 5:44
memberMatt Philmon11-Dec-01 5:44 
GeneralRe: Article screwed up... no links Pin
Chris Maunder11-Dec-01 11:44
memberChris Maunder11-Dec-01 11:44 
Generaldoesn't work in 16bits, look at this valid code ... Pin
F.Julien20-Sep-01 1:28
memberF.Julien20-Sep-01 1:28 
Generaldoesn't work when you want white as transparent color Pin
bene29-Aug-01 4:47
memberbene29-Aug-01 4:47 
GeneralI found here how to load HBITMAP from file... Pin
Pavel Kurochkin26-Aug-01 13:42
memberPavel Kurochkin26-Aug-01 13:42 
Questionit's not working .... why? Pin
scwpark30-Jul-01 21:15
memberscwpark30-Jul-01 21:15 
QuestionWhat it taje to get ::ExtCreateRegion work in NT Pin
Andy9-Oct-00 18:01
sussAndy9-Oct-00 18:01 
AnswerRe: What it taje to get ::ExtCreateRegion work in NT Pin
Anonymous2-Mar-01 0:04
memberAnonymous2-Mar-01 0:04 
GeneralRe: What it taje to get ::ExtCreateRegion work in NT Pin
Anonymous2-Apr-01 19:46
memberAnonymous2-Apr-01 19:46 
Generalbuggy, slow code Pin
Feng Yuan www,fengyuan.com8-Sep-00 20:37
sussFeng Yuan www,fengyuan.com8-Sep-00 20:37 
GeneralRe: buggy, slow code Pin
Lance Gordon29-Sep-00 5:04
sussLance Gordon29-Sep-00 5:04 
GeneralI need some help here... (rookie!) Pin
MatuX19-Jul-00 18:27
sussMatuX19-Jul-00 18:27 

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
Web01 | 2.8.180221.1 | Last Updated 11 Dec 2001
Article Copyright 2000 by Yuriy Zaporozhets
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid