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

CreateRegionFromFile

Rate me:
Please Sign up or sign in to vote.
5.00/5 (13 votes)
10 Dec 2001 163.8K   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

 
GeneralNot working on Windows Seven Pin
Dom306-May-11 21:19
Dom306-May-11 21:19 
GeneralRe: Not working on Windows Seven Pin
yzaz26-May-11 10:01
yzaz26-May-11 10:01 
GeneralMy vote of 5 Pin
ryan20fun9-Feb-11 20:15
ryan20fun9-Feb-11 20:15 
GeneralRotated Edit Box Pin
zakir1232-Sep-04 3:57
zakir1232-Sep-04 3:57 
GeneralCreateRgnFromFile question Pin
ralne31-Jul-04 10:23
ralne31-Jul-04 10:23 
please, tell me, in your function "CreateRgnFromFile" why did you allocate with "+8" in the follwing line:

LPBITMAPINFO bi = (LPBITMAPINFO) new BYTE[sizeof(BITMAPINFO) + 8];

and "+4" in:
LPBYTE pBits = new BYTE[bi->bmiHeader.biSizeImage + 4];

thanks,
rioan
GeneralRe: CreateRgnFromFile question Pin
yzaz5-Oct-04 13:59
yzaz5-Oct-04 13:59 
GeneralCustom controls ... Pin
kla_boot10-Jun-04 0:58
kla_boot10-Jun-04 0:58 
GeneralFantastic !!! Pin
Judge Gazza28-Apr-04 16:37
Judge Gazza28-Apr-04 16:37 
GeneralGreat example Pin
Chris Eatough10-Jun-03 0:42
Chris Eatough10-Jun-03 0:42 
GeneralAdapting this Pin
Kyudos11-Dec-02 3:27
Kyudos11-Dec-02 3:27 
Generalfinally found it Pin
Kevin Smith1-Nov-02 9:10
Kevin Smith1-Nov-02 9:10 
GeneralLarge Fonts Pin
Matt Philmon5-Feb-02 11:11
Matt Philmon5-Feb-02 11:11 
GeneralRegionToBitmap class... Pin
saturn5-Feb-02 1:53
saturn5-Feb-02 1:53 
GeneralArticle screwed up... no links Pin
Matt Philmon11-Dec-01 4:44
Matt Philmon11-Dec-01 4:44 
GeneralRe: Article screwed up... no links Pin
Chris Maunder11-Dec-01 10:44
cofounderChris Maunder11-Dec-01 10:44 
Generaldoesn't work in 16bits, look at this valid code ... Pin
20-Sep-01 0:28
suss20-Sep-01 0:28 
Generaldoesn't work when you want white as transparent color Pin
29-Aug-01 3:47
suss29-Aug-01 3:47 
GeneralI found here how to load HBITMAP from file... Pin
26-Aug-01 12:42
suss26-Aug-01 12:42 
Questionit's not working .... why? Pin
30-Jul-01 20:15
suss30-Jul-01 20:15 
QuestionWhat it taje to get ::ExtCreateRegion work in NT Pin
discoboyq9-Oct-00 17:01
discoboyq9-Oct-00 17:01 
AnswerRe: What it taje to get ::ExtCreateRegion work in NT Pin
1-Mar-01 23:04
suss1-Mar-01 23:04 
GeneralRe: What it taje to get ::ExtCreateRegion work in NT Pin
2-Apr-01 18:46
suss2-Apr-01 18:46 
Generalbuggy, slow code Pin
Feng Yuan www,fengyuan.com8-Sep-00 19:37
sussFeng Yuan www,fengyuan.com8-Sep-00 19:37 
GeneralRe: buggy, slow code Pin
Lance Gordon29-Sep-00 4:04
Lance Gordon29-Sep-00 4:04 
GeneralI need some help here... (rookie!) Pin
matux19-Jul-00 17:27
matux19-Jul-00 17: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.