CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2006
    Location
    Mumbai, India
    Posts
    292

    CBitmapButton:LoadBitmaps() question....

    Hello...

    I am using CBitmapButton class for creating buttons and using LoadBitmaps() function to load bitmaps. In my application I need to change the bitmaps several times. Thus there will be significant no of calls to LoadBitmaps()

    My question is whether LoadBitmaps() erases previously loaded bitmaps. If not then it will eat up the memory. Is there any way I can remove those bitmaps from the memory.

    Thanks.

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: CBitmapButton:LoadBitmaps() question....

    The source code for the function is:
    Code:
    // LoadBitmaps will load in one, two, three or all four bitmaps
    // returns TRUE if all specified images are loaded
    BOOL CBitmapButton::LoadBitmaps(LPCTSTR lpszBitmapResource,
    	LPCTSTR lpszBitmapResourceSel, LPCTSTR lpszBitmapResourceFocus,
    	LPCTSTR lpszBitmapResourceDisabled)
    {
    	// delete old bitmaps (if present)
    	m_bitmap.DeleteObject();
    	m_bitmapSel.DeleteObject();
    	m_bitmapFocus.DeleteObject();
    	m_bitmapDisabled.DeleteObject();
    
    	if (!m_bitmap.LoadBitmap(lpszBitmapResource))
    	{
    		TRACE(traceAppMsg, 0, "Failed to load bitmap for normal image.\n");
    		return FALSE;   // need this one image
    	}
    	BOOL bAllLoaded = TRUE;
    	if (lpszBitmapResourceSel != NULL)
    	{
    		if (!m_bitmapSel.LoadBitmap(lpszBitmapResourceSel))
    		{
    			TRACE(traceAppMsg, 0, "Failed to load bitmap for selected image.\n");
    			bAllLoaded = FALSE;
    		}
    	}
    	if (lpszBitmapResourceFocus != NULL)
    	{
    		if (!m_bitmapFocus.LoadBitmap(lpszBitmapResourceFocus))
    			bAllLoaded = FALSE;
    	}
    	if (lpszBitmapResourceDisabled != NULL)
    	{
    		if (!m_bitmapDisabled.LoadBitmap(lpszBitmapResourceDisabled))
    			bAllLoaded = FALSE;
    	}
    	return bAllLoaded;
    }
    So yes, the previous loaded bitmaps, if any, are destroyed.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    Join Date
    May 2006
    Location
    Mumbai, India
    Posts
    292

    Re: CBitmapButton:LoadBitmaps() question....

    Hi Cilu..

    Thanks a lot for that.


  4. #4
    Join Date
    Jan 2006
    Location
    Marseille, France
    Posts
    94

    Re: CBitmapButton:LoadBitmaps() question....

    Hello,

    For me, this function doesn't work (under Visual C++ 6).

    I had to rewrite it like this :

    Code:
    BOOL CMyBitmapButton::LoadBitmaps(LPCTSTR lpszBitmapResource, LPCTSTR lpszBitmapResourceSel, LPCTSTR lpszBitmapResourceFocus, LPCTSTR lpszBitmapResourceDisabled)
    {
    
    	HBITMAP hBitmap = NULL;
    	
    	// delete old bitmaps (if present)
    	m_bitmap.DeleteObject();
    	m_bitmapSel.DeleteObject();
    	m_bitmapFocus.DeleteObject();
    	m_bitmapDisabled.DeleteObject();
    
    	hBitmap = (HBITMAP)LoadImage(NULL,  lpszBitmapResource, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION | LR_DEFAULTSIZE);
    	if (!hBitmap)
    	{
    		TRACE(traceAppMsg, 0, "Failed to load bitmap for normal image.\n");
    		return FALSE;   // need this one image
    	}
    	m_bitmap.Attach(hBitmap);
    
    	BOOL bAllLoaded = TRUE;
    	if (lpszBitmapResourceSel != NULL)
    	{
    		hBitmap = (HBITMAP)LoadImage(NULL,  lpszBitmapResourceSel, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION | LR_DEFAULTSIZE);
    		if (!hBitmap)
    		{
    			TRACE(traceAppMsg, 0, "Failed to load bitmap for selected image.\n");
    			bAllLoaded = FALSE;
    		}
    		else{
    			m_bitmapSel.Attach(hBitmap);
    		}
    	}
    	if (lpszBitmapResourceFocus != NULL)
    	{
    		hBitmap = (HBITMAP)LoadImage(NULL,  lpszBitmapResourceFocus, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION | LR_DEFAULTSIZE);
    		if (!hBitmap){
    			bAllLoaded = FALSE;
    		}
    		else{
    			m_bitmapFocus.Attach(hBitmap);
    		}
    	}
    	if (lpszBitmapResourceDisabled != NULL)
    	{
    		hBitmap = (HBITMAP)LoadImage(NULL,  lpszBitmapResourceDisabled, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION | LR_DEFAULTSIZE);
    		if (!hBitmap){
    			bAllLoaded = FALSE;
    		}
    		else{
    			m_bitmapDisabled.Attach(hBitmap);
    		}
    	}
    	return bAllLoaded;
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured