Click here to Skip to main content
Click here to Skip to main content

CreateRegionFromFile

, 10 Dec 2001
Rate this:
Please Sign up or sign in to vote.
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.
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()
{
    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 about 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

Share

About the Author

No Biography provided

Comments and Discussions

 
GeneralGreat example PinmemberUbersnack10-Jun-03 1:42 
GeneralAdapting this PinmemberJudd11-Dec-02 4:27 
Generalfinally found it PinmemberKevin Smith1-Nov-02 10:10 
GeneralLarge Fonts PinmemberMatt Philmon5-Feb-02 12:11 
This works great for me under 95/98/ME/NT/2000/XP under all resolutions and color depths that I've tested (640x480 up through 1280x1024 from 256 color through 32 bit). However, the killer problem is Large Fonts. I don't personally use large fonts but someone here does and pointed it out to me. I have a dialog with 4 buttons on it and it drops all 4 buttons severly down and to the left of where they should be. Any ideas how to deal with this?
GeneralRegionToBitmap class... Pinmembersaturn5-Feb-02 2:53 
GeneralArticle screwed up... no links PinmemberMatt Philmon11-Dec-01 5:44 
GeneralRe: Article screwed up... no links PinmemberChris Maunder11-Dec-01 11:44 
Generaldoesn't work in 16bits, look at this valid code ... PinmemberF.Julien20-Sep-01 1:28 
Generaldoesn't work when you want white as transparent color Pinmemberbene29-Aug-01 4:47 
GeneralI found here how to load HBITMAP from file... PinmemberPavel Kurochkin26-Aug-01 13:42 

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

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

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