Objectives
- Create a Bitmap
- Create a DC in MFC VC++
- Tell the DC to draw the bitmap
Technical Points
- Load the Bitmap into a Bitmap class
- The Bitmap class stores the Original DC Bitmap which will be restored when the class is destoried.
- The BitBlt function performs a 1:1 copy of pixels in the Source DC to the Target DC.
- StretchBlt function maps the Source DC to fit the dimensions of the Target DC.
OnCreate Event
pDC = new CClientDC(this);
HDC hdcWindow = pDC->GetSafeHdc();
// let our class handle the details of loading the bitmaps!
g_bmpBackground.Load(hdcWindow, "ch2p7_bg.bmp");
g_bmpSprite.Load(hdcWindow, "ch2p7_purball.bmp");
g_bmpSpriteMask.Load(hdcWindow, "ch2p7_purball_mask.bmp");
// release the DC we obtained
ReleaseDC(pDC);
delete pDC;
//Set the Milliseconds for each timer event
SetTimer(1,500,0);
OnDraw Event
StretchBlt(hdcWindow,
0, 0, 640, 480,
g_bmpBackground.GetBitmapDC(), 0, 0,
g_bmpBackground.GetWidth(),
g_bmpBackground.GetHeight(), SRCCOPY);
On Timer Event
CDC *pDC;
int randomx = RandomNumber(0,640);
int randomy = RandomNumber(0,480);
pDC = new CClientDC(this);
HDC hdcWindow = pDC->GetSafeHdc();
//redraw the background
StretchBlt(hdcWindow,0, 0, 640, 480,
g_bmpBackground.GetBitmapDC(), 0, 0, 640, 480, SRCCOPY);
// first blit the mask, using SRCAND.
BitBlt(hdcWindow, randomx, randomy, 640, 480,
g_bmpSpriteMask.GetBitmapDC(), 0, 0, SRCAND);
// then blit the image using the OR operator (SRCPAINT).
BitBlt(hdcWindow, randomx, randomy, 640, 480,
g_bmpSprite.GetBitmapDC(), 0, 0, SRCPAINT);
ReleaseDC(pDC);
delete pDC;
Comments