MFC對話方塊裡獲取DC畫圖的幾種方法 阿新 • • 發佈:2019-01-09 對於create的dc應該予以deletedc,而對於getdc的應予以releasedc 一、CDC *pDC = GetDC(); CPen pen(PS_SOLID, 2, RGB(0,0,255)); pDC->SelectObject(&pen); pDC->MoveTo(10, 10); pDC->LineTo(500, 450); ReleaseDC(pDC); 二、 CClientDC dc(this); CDC dcCompatible; dcCompatible.CreateCompatibleDC(&dc); dcCompatible.SelectObject(&m_bitmap); pMemDC->BitBlt(m_rcWindowRect.left, m_rcWindowRect.top, m_rcWindowRect.Width(), m_rcWindowRect.Heigh(), &dcCompatible, 0, 0, SRCCOPY); pMemDC->StretchBlt(m_rcWindowRect.left, m_rcWindowRect.top, m_rcWindowRect.Width(), m_rcWindowRect.Height(), &dcCompatible, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY); DeleteDC(dcCompatible); 三、 CPaintDC dc(this); //CPaintDC只在OnPaint函式裡有效。 HDC hDC; CDC *pDC = m_pParentDlg->GetDC(); hDC = CreateCompatibleDC(pDC->GetSafeHdc()); HGDIOBJ hOldBmp = SelectObject(hDC, m_hbitmap); StretchBlt(pDC->m_hDC, 535, 70, 200, 150, hDC, 0, 0, m_tuwidth, m_tuheight, SRCCOPY); SelectObject(hDC, hOldBmp); DeleteDC(hDC); m_pParentDlg->ReleaseDC(pDC); wince從檔案裡面載入bmp格式圖片:(要包含標頭檔案ShellAPI.h) #include <ShellAPI.h>HBITMAP hmap; hmap = SHLoadDIBitmap(_T("\\SDMMC\\Test\\kenan.bmp")); CDC dcCompatible;dcCompatible.CreateCompatibleDC(pMemDC); SelectObject(dcCompatible, hmap); BitBlt(pMemDC->m_hDC, m_rcWindowRect.left, m_rcWindowRect.top, m_rcWindowRect.Width(), m_rcWindowRect.Height(), dcCompatible, 0, 0, SRCCOPY); DeleteDC(dcCompatible)