Effect Bitmap in MFC

I.Introduction

Trong bài này mình xin chia sẽ cách vẽ hình ra ứng dụng và tạo cho hình ấy một hiệu ứng trong MFC

II.Creating Project and Coding

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

image

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

image

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:     if( !CFrameWnd::PreCreateWindow(cs) )

   4:         return FALSE;

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

   6:     //  the CREATESTRUCT cs

   7:     CBitmap bmp;

   8:     BITMAP  bm;

   9:     

  10:     bmp.LoadBitmap(IDB_BITMAP);

  11:     bmp.GetBitmap(&bm);

  12:     cs.cx = bm.bmWidth*2 + 10;

  13:     cs.cy = bm.bmHeight * 2 + 100;

  14:  

  15:     cs.dwExStyle &= ~WS_EX_CLIENTEDGE;

  16:     cs.lpszClass = AfxRegisterWndClass(0);

  17:     return TRUE;

  18: }

tiếp theo chúng ta vào hàm Onpaint() và 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:     CDC dcMem ;

   7:     dcMem.CreateCompatibleDC(&dc);

   8:  

   9:     CBitmap bmp;

  10:     BITMAP bm;

  11:     bmp.LoadBitmap(IDB_BITMAP);

  12:     bmp.GetBitmap(&bm);

  13:     dcMem.SelectObject(&bmp);

  14:  

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

  16:     //Hinh Phai tren

  17:     dc.StretchBlt(bm.bmWidth*2,0,-bm.bmWidth,bm.bmHeight,&dcMem,0,0,bm.bmWidth,bm.bmHeight,SRCCOPY);

  18:     // Hinh trai duoi

  19:     dc.StretchBlt(0,bm.bmHeight*2,bm.bmWidth,-bm.bmHeight,&dcMem,0,0,bm.bmWidth,bm.bmHeight,SRCINVERT);

  20:     //phai duoi

  21:     dc.StretchBlt(bm.bmWidth*2,bm.bmHeight*2,-bm.bmWidth,-bm.bmHeight,&dcMem,0,0,bm.bmWidth,bm.bmHeight,SRCINVERT);

  22: }

Run Application và xem kết quả:

image

Leave a comment