Created
January 13, 2011 02:42
-
-
Save thinkhy/777308 to your computer and use it in GitHub Desktop.
Draw Bitmap in a windows base on MFC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void CMainFrame::DrawView(HBITMAP hbmp) | |
{ | |
ASSERT(hbmp != NULL); | |
CView *pView = GetActiveView(); | |
ASSERT(pView != NULL); | |
// pView->Invalidate(TRUE); | |
CDC *pDC = pView->GetDC(); | |
pDC->SelectObject((HGDIOBJ)hbmp); | |
//建立一个缓存设备上下文 | |
CDC m_MemDc; | |
m_MemDc.CreateCompatibleDC(pDC); | |
//选择位图 | |
m_MemDc.SelectObject((HGDIOBJ)hbmp); | |
BITMAP bm; | |
::GetObject(hbmp, sizeof(BITMAP), &bm); | |
//bm.bmWidth | |
//bm.bmHeight | |
CRect winrect; | |
pView->GetClientRect(&winrect); | |
int nWidth = bm.bmWidth < winrect.Width() - 20 ? bm.bmWidth : winrect.Width() - 20; | |
int nHeight = bm.bmHeight < winrect.Height() - 20 ? bm.bmWidth : winrect.Height() - 20; | |
int left = (winrect.Width() -nWidth) / 2; | |
int top = (winrect.Height() - nHeight) / 2; | |
CRect rect; | |
// 清屏 | |
CBrush backBrush(RGB(255, 255, 255)); | |
CBrush* pOldBrush = pDC->SelectObject(&backBrush); | |
pDC->GetClipBox(&rect); | |
pDC->PatBlt(rect.left, rect.top, rect.Width(), rect.Height(), PATCOPY); | |
pDC->SelectObject(pOldBrush); | |
// 画在屏幕上 | |
pDC->StretchBlt(left, top, //左上角坐标 | |
nWidth, nHeight, //显示的宽和高 | |
&m_MemDc, //从缓存中获取 | |
0, 0, //从源位图的(0,0)坐标开始显示 | |
bm.bmWidth, bm.bmHeight, | |
SRCCOPY); //将源位图拷贝至屏幕); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment