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

Function to load a bitmap on a dialog

, 6 Apr 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
Here is a function to load a bitmap on any dialog.

Sample Image - codepr.jpg

Introduction

Here, I am introducing a function SBitdraw(CDC *pDC, UINT nIDResource, int i) to load any bitmap to a dialog. There are so many ways by which we can load a bitmap. Here, I am giving a function which you can just add to your project and get the result. No need to add any other header files or .cpp files.

Using the function

First, bring the bitmap to your project.

  1. Put the function SBitdraw() as a member function of your main dialog class. Example: bool CSBitDialogDlg::SBitdraw(CDC *pDC, UINT nIDResource, int i).
  2. Add Messagemap ON_WM_ERASEBKGND() using the MFC ClassWizard.
  3. Then, call SBitdraw() from OnEraseBkgnd(CDC* pDC) to load any bitmap on the dialog.
  4. (Or, you can call from anywhere, and then you use OnEraseBkgnd() and Invalidate() to force refreshing your dialog.

-------------------------------------------------------
Parameters of the function::
SBitdraw(CDC *pDC, UINT nIDResource, int i);
1. pDC  take the device context obj.
2. nIDResource  is the ID of the bitmap.
3. Int i  will take integer 1-3  
   (1 for center, 2 for Stretch, And 3 for title) 
    to get how bitmap will load.
---------------------------------------------------------------------
Function defination:
 bool SBitdraw(CDC *pDC, UINT nIDResource, int i) 
{
            CBitmap* m_bitmap;
            m_bitmap=new CBitmap();
            m_bitmap->LoadBitmap(nIDResource);
            if(!m_bitmap->m_hObject)
                        return true;
    CRect rect;
            GetClientRect(&rect);
            CDC dc;
            dc.CreateCompatibleDC(pDC);    
            dc.SelectObject(m_bitmap);
            int bmw, bmh ;
            BITMAP bmap;
            m_bitmap->GetBitmap(&bmap);
            bmw = bmap.bmWidth;
            bmh = bmap.bmHeight;
            int xo=0, yo=0;
            switch(i){
            case 1:
            	pDC->StretchBlt(xo, yo, rect.Width(),
                                    rect.Height(), &dc,
                                    0, 0,bmw,bmh, SRCCOPY);
                break;
            case 2:
                if(bmw < rect.Width())
                    xo = (rect.Width() - bmw)/2;
                else 
                    xo=0;
                if(bmh < rect.Height())
                    yo = (rect.Height()-bmh)/2;
                else
                    yo=0;
                pDC->BitBlt (xo, yo, rect.Width(),
                            rect.Height(), &dc,
                            0, 0, SRCCOPY);
                break;
             case 3:
                for (yo = 0; yo < rect.Height(); yo += bmh)
                {
                    for (xo = 0; xo < rect.Width(); xo += bmw)
                    {
                        pDC->BitBlt (xo, yo, rect.Width(),
                                    rect.Height(), &dc,
                                    0, 0, SRCCOPY);
                    }
                }
            }
            return true;
 
}
---------------------------------------------------------------------

Note: In my demo project, UserId & Password both are "trek". First login and then change the background.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Saday Sarkar
Web Developer
India India
This is Saday Chand Sarkar , New in MFC world.
Working in Trek Technology Pte.Ltd.

Comments and Discussions

 
GeneralIssues PinmemberYoSilver2-Mar-06 6:57 
LoadBitmap can correctly load bitmaps other than 4-bit/Windows palette only under Windows XP. You should use the LoadImage API instead.
 
And the main point - why on earth load image every time???? It is absolutely crazy. The image must be loaded once, and then cached. I would not recommend to use this code.
 
Consider this:
 
struct _ImageLoader
{
    _ImageLoader(UINT uResID)
    {
    	m_hbm = ::LoadImage(theApp, (LPCTSTR )uResID, 
                            IMAGE_BITMAP, 0, 0, 
                            LR_CREATEDIBSECTION);
    	ASSERT(m_hbm);
    }
 
    ~_ImageLoader()
    {
    	ASSERT(m_hbm);
		DeleteObject(m_hbm);
    }
 
    HBITMAP m_hbm;
};
 
_ImageLoader& getImageLoader()
{
	static _ImageLoader ldr(IDB_BITMAP);
	return ldr;
}
somewhere in the code:
 
{
    _ImageLoader& ldr = getImageLoader();
    PaintMyBitmap(ldr.m_hbm);
}
Good luck!

 
One always gets the deserved.

http://www.silveragesoftware.com/hffr.html
HandyFile Find And Replace

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
Web02 | 2.8.141223.1 | Last Updated 7 Apr 2006
Article Copyright 2006 by Saday Sarkar
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid