MiltiImage Drawing in MFC

I.Introduction

Trong bài này mình xin chia sẽ cách vẽ hình ra ứng dụng với các hình sắp theo kiểu bậc thang

II.Creating Project and Coding

Mở Visual Studio lên và tạo MFC Application

image_thumb[1]

tiếp theo các bạn vào Resourse –> Right Click vào Project –>Import

image_thumb[5]

sau đó Add hình Bitmap vào.

Các bạn vào hàm PreCreateWindow để xử lý việc xác địch kích thước ban đầu cho ngữ cảnh

   1: BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)

   2: {

   3:     // co the thay doi cua so tai day !

   4:     if( !CFrameWnd::PreCreateWindow(cs) )

   5:         return FALSE;

   6:     // TODO: Modify the Window class or styles here by modifying

   7:     //  the CREATESTRUCT cs

   8:     CBitmap bmp;

   9:     bmp.LoadBitmap(IDB_BITMAP);

  10:     

  11:     BITMAP bm ;

  12:     bmp.GetBitmap(&bm);

  13:     cs.cx = 7*bm.bmWidth;

  14:     cs.cy = 7*bm.bmHeight + 100;

  15:  

  16:     cs.dwExStyle &= ~WS_EX_CLIENTEDGE;

  17:     cs.lpszClass = AfxRegisterWndClass(0);

  18:     return TRUE;

  19: }

Tiếp theo chúng ta vào hàm Onpaint xử lý:

   1: void CChildView::OnPaint() 

   2: {

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

   4:     

   5:     // TODO: Add your message handler code here

   6:     // khai bao thiet bi ngu canh

   7:     CDC dcMem ;

   8:     dcMem.CreateCompatibleDC(&dc);

   9:     CBitmap bmp;

  10:     bmp.LoadBitmap(IDB_BITMAP);

  11:     BITMAP bm;

  12:     bmp.GetBitmap(&bm);

  13:     

  14:     dcMem.SelectObject(&bmp);

  15:     CPoint top1(bm.bmWidth,bm.bmHeight);

  16:     CPoint top2(bm.bmWidth*3,bm.bmHeight*3);

  17:     dc.BitBlt(0,0,bm.bmWidth,bm.bmHeight,&dcMem,0,0,SRCCOPY);

  18:     

  19:     dc.StretchBlt(top1.x,top1.y,bm.bmWidth*2,bm.bmHeight*2,&dcMem,0,0,bm.bmWidth,bm.bmHeight,SRCCOPY);

  20:     dc.StretchBlt(top2.x,top2.y,bm.bmWidth*4,bm.bmHeight*4,&dcMem,0,0,bm.bmWidth,bm.bmHeight,SRCCOPY);

  21:     // Do not call CWnd::OnPaint() for painting messages

Run Application

image

Leave a comment