-
CreateCompatibleDC
鎖定
CreateCompatibleDC函數功能
CreateCompatibleDC函數原型
HDC CreateCompatibleDC(HDC hdc);
vb定義:
Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
CreateCompatibleDC參數
返回值:如果成功,則返回內存設備上下文環境的句柄;如果失敗,則返回值為NULL。
Windows NT:若想獲得更多錯誤信息,請調用GetLastError函數。
註釋:內存設備上下文環境是僅在內存中存在的設備上下文環境,當內存設備上下文環境被創建時,它的顯示界面是標準的一個單色像素寬和一個單色像素高,在一個應用程序可以使用內存設備上下文環境進行繪圖操作之前,它必須選擇一個高和寬都正確的位圖到設備上下文環境中,這可以通過使用CreateCompatibleBitmap函數指定高、寬和色彩組合以滿足函數調用的需要。
CreateCompatibleDc函數只適用於支持光柵操作的設備,應用程序可以通過調用GetDeviceCaps函數來確定一個設備是否支持這些操作。
當不再需要內存設備上下文環境時,可調用DeleteDc函數刪除它。
[From MSDN]
Creates a memory device context that is compatible with the device specified bypDC.
BOOL CreateCompatibleDC(CDC* pDC);
Parameter:
- pDC
- A pointer to a device context. IfpDCisNULL, the function creates a memory device context that is compatible with the system display.
Return Value:
Nonzero if the function is successful; otherwise 0.
Remark:
A memory device context is a block of memory that represents a display surface. It can be used to prepare images in memory before copying them to the actual device surface of the compatible device.
When a memory device context is created, GDI automatically selects a 1-by-1 monochrome stock bitmap for it. GDI output functions can be used with a memory device context only if a bitmap has been created and selected into that context.
This function can only be used to create compatible device contexts for devices that support raster operations. See theCDC::BitBltmember function for information regarding bit-block transfers between device contexts. To determine whether a device context supports raster operations, see theRC_BITBLTraster capability in the member functionCDC::GetDeviceCaps.
Example:
// This handler loads a bitmap from system resources, // centers it in the view, and uses BitBlt() to paint the bitmap // bits. void CDCView::DrawBitmap(CDC* pDC){ // 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"); } }