[ Home | Syllabus |Course Notes]
CBitmap bitmap;
BITMAP bm;
CClientDC dc(this); // always and only attached to screen (framw buffer) bitmap
CRect rect;
CClientRect(&rect);
bitmap.LoadBitmap(IDR_LOGOIMAGE); // load from "bmp" file identified in resource file
bitmap.GetObject (sizeof (BITMAP), &bm); // get information on this bitmap
CPoint size (bm.bmWidth, bm.bmHeight); // among which is width and height
pDC->DPtoLP (&size); // to be sure convert to logical units
CPoint org (0, 0);
pDC->DPtoLP (&org);
CDC dcMem; // memory device context for drawing on memory bitmap
dcMem.CreateCompatibleDC (&dc); // make sure it is compatible with hardware device context
dcMem.SelectObject (bitmap); // select bitmap into the memory device context
dcMem.SetMapMode (dc.GetMapMode ()); // get compatible modes
pDC->BitBlt (x, y, size.x, size.y, &dcMem, org.x, org.y, SRCCOPY); // transfer bitmap to screen
}
int CMainWindow::OnCreate (LPCREATESTRUCT lpcs)
{
if (CFrameWnd::OnCreate (lpcs) == -1)
return -1;
m_bitmap.LoadBitmap (IDR_BITMAP);
//....
return 0;
}
void CMainWindow::OnPaint ()
{
CRect rect;
GetClientRect (&rect);
CPaintDC dc (this);
BITMAP bm;
m_bitmap.GetObject (sizeof (BITMAP), &bm);
int cx = (rect.Width () / (bm.bmWidth + 8)) + 1;
int cy = (rect.Height () / (bm.bmHeight + 8)) + 1;
int i, j, x, y;
for (i=0; i<cx; i++) {
for (j=0; j<cy; j++) {
x = 8 + (i * (bm.bmWidth + 8));
y = 8 + (j * (bm.bmHeight + 8));
if (m_bDrawOpaque)
m_bitmap.Draw (&dc, x, y);
else
m_bitmap.DrawTransparent (&dc, x, y, RGB (255, 0, 0));
}
}
}
void CMaskedBitmap::Draw (CDC* pDC, int x, int y)
{
BITMAP bm;
GetObject (sizeof (BITMAP), &bm);
CPoint size (bm.bmWidth, bm.bmHeight);
pDC->DPtoLP (&size);
CPoint org (0, 0);
pDC->DPtoLP (&org);
CDC dcMem;
dcMem.CreateCompatibleDC (pDC);
CBitmap* pOldBitmap = dcMem.SelectObject (this);
dcMem.SetMapMode (pDC->GetMapMode ());
pDC->BitBlt (x, y, size.x, size.y, &dcMem, org.x, org.y, SRCCOPY);
dcMem.SelectObject (pOldBitmap);
}
Problem: want to scramble the bitmap on the screen by exchanging small rectangles at regular intervals
afx_msg void OnTimer(UINT); // plus entry in MESSAGE_MAP
int CMainWindow::OnCreate (LPCREATESTRUCT lpcs)
{
if (CFrameWnd::OnCreate (lpcs) == -1)
return -1;
SetTimer(ID_TIMER,100,NULL); // sets timer for moving bitmap
srand( int(GetCurrentTime( ) )); // initialize random number generator also
//.....
void CMainWindow::OnTimer(UINT nTimerId)
{
Invalidate();
}
// in OnCreate we also have
int CMainWindow::OnCreate (LPCREATESTRUCT lpcs)
{
// ....
CClientDC dc (this);
CRect rect;
GetClientRect(&rect);
CDC dcMem;
dcMem.CreateCompatibleDC(&dc);
dcMem.SetMapMode(dc.GetMapMode());
m_bitmap.CreateCompatibleBitmap(&dc, rect.Width()/10, rect.Height()/10);
// One tenth the size of the client area. (allocates space for the bitmap and sets parameters).
return 0;
}
///......
void CMainWindow::OnPaint ()
{
CRect rect;
GetClientRect (&rect);
CPaintDC dc (this);
BITMAP bm;
m_bitmap.GetObject (sizeof (BITMAP), &bm);
CPoint size(bm.bmWidth,bm.bmHeight); // size is set to size of bitmap
DoDrawText (&dc, &rect);
dc.DPtoLP(&size);
CPoint org(0,0);
dc.DPtoLP(&org);
int x1 = size.x *(rand() % 10); // pick two rectangles out of the hundred possible at random
int y1 = size.y * (rand() % 10);
int x2 = size.x * (rand() % 10);
int y2 = size.y * (rand() % 10);
CPoint rect1(x1,y1);
CPoint rect2(x2,y2);
CDC dcMem;
dcMem.CreateCompatibleDC(&dc);
dcMem.SetMapMode(dc.GetMapMode());
dcMem.SelectObject(m_bitmap);
dcMem.BitBlt(org.x,org.y, size.x,size.y, &dc, rect1.x,rect1.y,SRCCOPY); // copy rect 1 to temporary bitmap
dc.BitBlt(rect1.x,rect1.y,size.x,size.y, &dc, rect2.x,rect2.y,SRCCOPY); // copy rect2 to rect1
dc.BitBlt(rect2.x,rect2.y,size.x,size.y, &dcMem, org.x,org.y,SRCCOPY); // copy temp to rect2
}