函式功能
該函式創建一個與指定設備兼容的記憶體設備上下文環境(DC)。通過GetDc()獲取的HDC直接與相關設備溝通,而本函式創建的DC,則是與記憶體中的一個表面相關聯。
函式原型
HDC CreateCompatibleDC(HDC hdc);
vb定義:
Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
參數
hdc:現有設備上下文環境的句柄,如果該句柄為NULL,該函式創建一個與應用程式的當前顯示器兼容的記憶體設備上下文環境。
返回值:如果成功,則返回記憶體設備上下文環境的句柄;如果失敗,則返回值為NULL。
Windows NT:若想獲得更多錯誤信息,請調用GetLastError函式。
注釋:記憶體設備上下文環境是僅在記憶體中存在的設備上下文環境,當記憶體設備上下文環境被創建時,它的顯示界面是標準的一個單色像素寬和一個單色像素高,在一個應用程式可以使用記憶體設備上下文環境進行繪圖操作之前,它必須選擇一個高和寬都正確的點陣圖到設備上下文環境中,這可以通過使用CreateCompatibleBitmap函式指定高、寬和色彩組合以滿足函式調用的需要。
當一個記憶體設備上下文環境創建時,所有的特性都設為預設值,記憶體設備上下文環境作為一個普通的設備上下文環境使用,當然也可以設定這些特性為非預設值,得到它的特性的當前設定,為它選擇畫筆,刷子和區域。
CreateCompatibleDc函式只適用於支持光柵操作的設備,應用程式可以通過調用GetDeviceCaps函式來確定一個設備是否支持這些操作。
當不再需要記憶體設備上下文環境時,可調用DeleteDc函式刪除它。
ICM:如果通過該函式的hdc參數傳送給該函式設備上下文環境(Dc)對於獨立顏色管理(ICM)是能用的,則該函式創建的設備上下文環境(Dc)是ICM能用的,資源和目標顏色間隔是在Dc中定義。
速查:Windows NT:3.1及以上版本;Windows:95及以上版本;Windows CE:1.0及以上版本;頭檔案:wingdi.h;庫檔案:gdi32.lib。
[From MSDN]
Creates a memory device context that is compatible with the device specified by pDC.
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 the RC_BITBLTraster capability in the member function CDC::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(▭);
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");
}
}