複製鏈接
請複製以下鏈接發送給好友

CreateCompatibleDC

鎖定
函數創建一個與指定設備兼容的內存設備上下文環境(DC)。
中文名
CreateCompatibleDC
函數功能
創建一個內存設備上下文環境
函數原型
HDC CreateCompatibleDC
hdc
現有設備上下文環境的句柄
Windows
95

CreateCompatibleDC函數功能

函數創建一個與指定設備兼容的內存設備上下文環境(DC)。通過GetDc獲取的HDC直接與相關設備溝通,而本函數創建的DC,則是與內存中的一個表面相關聯。

CreateCompatibleDC函數原型

HDC CreateCompatibleDC(HDC hdc);
vb定義:
Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long

CreateCompatibleDC參數

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 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");
  }
}