//*********************************************************************** // // BitmapDemo.cpp // //*********************************************************************** #include #include #include "Resource.h" #include "BitmapDemo.h" CMyApp myApp; ///////////////////////////////////////////////////////////////////////// // CMyApp member functions BOOL CMyApp::InitInstance () { m_pMainWnd = new CMainWindow; m_pMainWnd->ShowWindow (m_nCmdShow); m_pMainWnd->UpdateWindow (); return TRUE; } ///////////////////////////////////////////////////////////////////////// // CMainWindow message map and member functions BEGIN_MESSAGE_MAP (CMainWindow, CFrameWnd) ON_WM_CREATE () ON_WM_ERASEBKGND () ON_WM_PAINT () ON_WM_TIMER() ON_COMMAND (IDM_OPTIONS_EXIT, OnOptionsExit) END_MESSAGE_MAP () CMainWindow::CMainWindow () { Create (NULL, "Bitmap Demo", WS_OVERLAPPEDWINDOW, rectDefault, NULL, MAKEINTRESOURCE (IDR_MAINFRAME)); } int CMainWindow::OnCreate (LPCREATESTRUCT lpcs) { if (CFrameWnd::OnCreate (lpcs) == -1) return -1; CClientDC dc (this); CRect rect; GetClientRect(&rect); srand(100); // initialize random number generator CDC dcMem; dcMem.CreateCompatibleDC(&dc); dcMem.SetMapMode(dc.GetMapMode()); m_bitmap.CreateCompatibleBitmap(&dc, rect.Width()/10, rect.Height()/10); SetTimer(ID_TIMER,100,NULL); // sets timer for moving bitmap return 0; } BOOL CMainWindow::OnEraseBkgnd (CDC* pDC) { return true; // transparent window CRect rect; GetClientRect (&rect); DoGradientFill (pDC, &rect); return TRUE; } void CMainWindow::OnPaint () { CRect rect; GetClientRect (&rect); CPaintDC dc (this); BITMAP bm; m_bitmap.GetObject (sizeof (BITMAP), &bm); CDC dcMem; CPoint size(bm.bmWidth,bm.bmHeight); DoDrawText (&dc, &rect); dc.DPtoLP(&size); CPoint org(0,0); dc.DPtoLP(&org); int x1 = size.x *(rand() % 10); 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); //dc.DPtoLP(&rect1); //dc.DPtoLP(&rect2); 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); dc.BitBlt(rect1.x,rect1.y,size.x,size.y,&dc,rect2.x,rect2.y,SRCCOPY); dc.BitBlt(rect2.x,rect2.y,size.x,size.y,&dcMem,org.x,org.y,SRCCOPY); } void CMainWindow::DoGradientFill (CDC* pDC, CRect* pRect) { CBrush* pBrush[64]; for (int i=0; i<64; i++) pBrush[i] = new CBrush (PALETTERGB (0, 0, 255 - (i * 4))); int nWidth = pRect->Width (); int nHeight = pRect->Height (); CRect rect; for (i=0; iFillRect (&rect, pBrush[(i * 63) / nHeight]); } for (i=0; i<64; i++) delete pBrush[i]; } void CMainWindow::OnOptionsExit () { SendMessage (WM_CLOSE, 0, 0); } void CMainWindow::OnTimer(UINT nTimerId) { Invalidate(); } void CMainWindow::DoDrawText (CDC* pDC, CRect* pRect) { CFont font; int nHeight = -((pDC->GetDeviceCaps (LOGPIXELSY) * FONTHEIGHT) / 72); font.CreateFont (nHeight, 0, 0, 0, FW_BOLD, TRUE, 0, 0, DEFAULT_CHARSET, OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "Times New Roman"); //pDC->SetBkMode (TRANSPARENT); pDC->SetBkColor(RGB(255,0,255)); pDC->SetTextColor (RGB (255, 255, 255)); CFont* pOldFont = pDC->SelectObject (&font); pDC->DrawText ("Hello, MFC", -1, pRect, DT_SINGLELINE | DT_CENTER | DT_VCENTER); pDC->SelectObject (pOldFont); }