MFC显示位图的几种方法
摘要:关于MFC显示位图的几种方法的深入研究。
步骤/方法
- 01
//在对话框内显示位图 CBitmap hbmp; HBITMAP hbitmap; //装载图片文件MM.bmp hbitmap=(HBITMAP)::LoadImage(::AfxGetInstanceHandle(),"MM.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LR_CREATEDIBSECTION); hbmp.Attach(hbitmap); //获取图片格式 BITMAP bm; hbmp.GetBitmap(&bm); CDC dcMem; dcMem.CreateCompatibleDC(GetDC()); CBitmap * poldBitmap=(CBitmap*)dcMem.SelectObject(hbmp); CRect lRect; GetClientRect(&lRect); lRect.NormalizeRect(); //显示位图
- 02
GetDC()->StretchBlt(lRect.left,lRect.top,lRect.Width(),lRect.Height(),&dcMem,0,0,bm.bmWidth,bm.bmHeight,SRCCOPY); dcMem.SelectObject(&poldBitmap); //在Static控件内显示位图 CBitmap hbmp; HBITMAP hbitmap; //将pStatic指向要显示的地方 CStatic *pStatic; pStatic=(CStatic*)GetDlgItem(IDC_STATIC); //IDC_STATIC是你的Staic控件名 //装载图片文件MM.bmp hbitmap=(HBITMAP)::LoadImage(::AfxGetInstanceHandle(),"MM.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LR_CREATEDIBSECTION); hbmp.Attach(hbitmap);
- 03
//获取图片格式 BITMAP bm; hbmp.GetBitmap(&bm); CDC dcMem; dcMem.CreateCompatibleDC(GetDC()); CBitmap * poldBitmap=(CBitmap*)dcMem.SelectObject(hbmp); CRect lRect; pStatic->GetClientRect(&lRect); lRect.NormalizeRect(); //显示位图 pStatic->GetDC()->StretchBlt(lRect.left,lRect.top,lRect.Width(),lRect.Height(),&dcMem,0,0,bm.bmWidth,bm.bmHeight,SRCCOPY); dcMem.SelectObject(&poldBitmap);