• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Using Bitmaps with MFC in Visual.Net

Mavrick

Senior member
I'm trying to build a simple animation in Visual C++ .NET. I have 2 bitmaps to use. The first one is a background (that doesn't move) and I have a small bitmap that needs to move over it.

I'm not too familiar with Win32 programming, so I don't know how to tell my program (an MFC dialog box) to load and show those images.

I thought it could be in the OnPaint() function (I saw in many tutorials that they use the OnDraw(CDC *pDC) function), but since there is no CDC* element passed in argument, I have no idea how to use other functions (like BitBlt())

 
www.codeproject.com should have samples, also MSDN if you search on something like "MFC dialog bitmap."

Also if you add the MS Splash Screen component to a junk project you can look at its OnPaint handler, which does something close to what you want to do for the background.

FYI it starts by declaring a local CPaintDC object to get the DC, which automatically releases the DC when it's destroyed on exiting the OnPaint handler.
 
Originally posted by: kleinesarschloch
CWnd has a GetDC() member function.
Right, but for OnPaint you want a CPaintDC instead, to take care of BeginPaint & EndPaint and only draw in the invalid region. GetDC would make sense for doing animation outside of OnPaint.
 
Originally posted by: DaveSimmons
www.codeproject.com should have samples, also MSDN if you search on something like "MFC dialog bitmap."

Also if you add the MS Splash Screen component to a junk project you can look at its OnPaint handler, which does something close to what you want to do for the background.

FYI it starts by declaring a local CPaintDC object to get the DC, which automatically releases the DC when it's destroyed on exiting the OnPaint handler.

Indeed, MFC created a local CPaintDC object which it used (it says it uses it to draw the icon). But can I use this CPaintDC object and pass the bitmap in it (and later use BitBlt)?
 
The CPaintDC object must be local to OnPaint and can only be used in that function, but you can do as much drawing as you want with it then.

Here's code from splash.cpp's OnPaint :

CDC dcImage ;
if ( ! dcImage.CreateCompatibleDC ( &dc ))
return ;

BITMAP bm ;
m_bitmap.GetBitmap(&bm) ;

// Paint the image.
CBitmap* pOldBitmap = dcImage.SelectObject(&m_bitmap) ;
dc.BitBlt(0, 0, bm.bmWidth, bm.bmHeight, &dcImage, 0, 0, SRCCOPY) ;
dcImage.SelectObject(pOldBitmap) ;


the m_bitmap was set in OnCreate :

HBITMAP hBitmap = (HBITMAP) ::LoadImage(AfxGetInstanceHandle(), MAKEINTRESOURCE(idb), IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION ) ;
m_bitmap.Attach(hBitmap ) ;

---------------------------------

here's similar code from one page of a wizard-mode property sheet

CPaintDC dc(this) ; // device context for painting

CDC dcImage ;
if (!dcImage.CreateCompatibleDC(&dc))
return ;

BITMAP bm ;
m_bitmap.GetBitmap(&bm) ;

// Paint the image.
CBitmap* pOldBitmap = dcImage.SelectObject(&m_bitmap) ;
dc.BitBlt(0, 0, bm.bmWidth, bm.bmHeight, &dcImage, 0, 0, SRCCOPY) ;
dcImage.SelectObject(pOldBitmap) ;

--------------------------------------

For painting outside of OnPaint, you'd use a GetDC() like kleinesarschloch said, but note that whatever you paint outside of OnPaint will be "painted over" by the next OnPaint.

You can force a window to repaint itself with the Invalidate() function, so for animation you could always set up a timer and use it to force repaints.






 
Thanks Dave. I just realized that the dialog doesn't repaint by itself. I think that is why nothing appeared in the dialog. I'll try the invalidate function right now!!
 
Back
Top