GDI位元影像只是另一個GDI對象,例如筆或者字型。你必須先建立一個位元影像,然後將它選入裝置環境。當你完成改對象的操作之後,必須解除對它的選中,並刪除它。
儘管如此,仍有一些問題,原因是顯示器的“位元影像”實際上就是顯示器表面映像,印表機裝置的“位元影像”是列印頁面本身。因此不能將位元影像選入顯示裝置環境或者印表機裝置環境。必須使用CDC::CreateCompatibleDC 函數為位元影像建立一個特殊的記憶體裝置環境。然後使用CDC::StretchBlt或者CDC::BitBlt來從記憶體裝置環境中向"真正"的裝置環境中複製這些位。
範例:
void CBlat2View::OnDraw(CDC* pDC){ CBlat2Doc* pDoc = GetDocument(); ASSERT_VALID(pDoc); // load IDB_BITMAP1 from our resources CBitmap bmp; if (bmp.LoadBitmap(IDB_BITMAP1)) { // Get the size of the bitmap BITMAP bmpInfo; bmp.GetBitmap(&bmpInfo); // Create an in-memory DC compatible with the // display DC we're using to paint CDC dcMemory; dcMemory.CreateCompatibleDC(pDC); // Select the bitmap into the in-memory DC CBitmap* pOldBitmap = dcMemory.SelectObject(&bmp); // Find a centerpoint for the bitmap in the client area CRect rect; GetClientRect(&rect); int nX = rect.left + (rect.Width() - bmpInfo.bmWidth) / 2; int nY = rect.top + (rect.Height() - bmpInfo.bmHeight) / 2; // Copy the bits from the in-memory DC into the on- // screen DC to actually do the painting. Use the centerpoint // we computed for the target offset. pDC->BitBlt(nX, nY, bmpInfo.bmWidth, bmpInfo.bmHeight, &dcMemory, 0, 0, SRCCOPY); dcMemory.SelectObject(pOldBitmap); } else TRACE0("ERROR: Where's IDB_BITMAP1?\n");}
顯示映射模式:MM_TEXT 每個位元影像像素將映射為一個顯示像素。
MM_LOENGLISH :位元影像大小將是 0.54*0.96 英寸。
注 GDI對象包括:Cbitmap, CBrush,CFont, CPalette,CPen,CRgn。