只能选入到内存DC吗?hDc选入一个hBmp,然后DeleteDC hDc
或者
hDc选入一个hBmp,然后DeleteObjecthBmp
行不行

解决方案 »

  1.   

    CBitmap bmp;
    CDC dcMemory;
    CDC *pDC = GetDC(); bmp.CreateCompatibleBitmap(pDC,16,16);
    dcMemory.CreateCompatibleDC(pDC);
    CBitmap* pOldBitmap = (CBitmap* )dcMemory.SelectObject(&bmp);
    BITMAP bmpInfo;
    bmp.GetBitmap(&bmpInfo); // Find a center point 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 device context to the on-
    // screen device context to do the painting. Use the computed center
    // point for the target offset.
    // pDC->BitBlt(nX, nY, bmpInfo.bmWidth, bmpInfo.bmHeight, &dcMemory,
    // 0, 0, SRCCOPY); dcMemory.SelectObject(pOldBitmap); ReleaseDC(pDC);
      

  2.   

    同意flyelf(空谷清音),但是你的DeleteDC用的不对。
    In general, do not call this function; the destructor will do it for you. The DeleteDC member function deletes the Windows device contexts that are associated with m_hDC in the current CDC object. If this CDC object is the last active device context for a given device, the device is notified and all storage and system resources used by the device are released. An application should not call DeleteDC if objects have been selected into the device context. Objects must first be selected out of the device context before it it is deleted. An application must not delete a device context whose handle was obtained by calling CWnd::GetDC. Instead, it must call CWnd::ReleaseDC to free the device context. The CClientDC and CWindowDC classes are provided to wrap this functionality. The DeleteDC function is generally used to delete device contexts created with CreateDC, CreateIC, or CreateCompatibleDC.
      

  3.   

    hDc选入一个hBmp,然后DeleteDC hDc并不报错,不知道有什么不好,这种问题都没地方查啊
      

  4.   

    释放设备环境资源有两点:
    1、GDI对象的资源释放,如下:
        CBitmap *pOldBitmap = pDC->SelectObject(pBitmap);
        ......
        pDC->SelectObject(pOldBitmap);  //将原先的bitmap选入DC,有时也可以不用保存原来的GDI对象指针,直接用SelectObject()选入系统GDI对象就行。当然如果在GDI对象删除之前DC对象就已经被删除了,那就不需要将GDI对象调出设备环境了。
    2、DC对象的资源释放,
       pDC = GetDC();
       .....
       ReleaseDC(pDC); //用GetDC()得到的DC一定要用ReleaseDC()释放  而像:
       void DrawXY()
       {
           CClientDC dc;  //对于在堆栈上建立的DC对象,则在所在的函数运行完后自动删除。
           ....
        }
       
    个人观点,不知是否正确。
        
      
      

  5.   

    hDc选入一个hBmp,然后DeleteDC hDc并不报错,不知道有什么不好,这种问题都没地方查啊_____________________不报错是因为从语法上没问题。有什么不好么:
    The DeleteDC member function deletes the Windows device contexts that are associated with m_hDC in the current CDC object. If this CDC object is the last active device context for a given device, the device is notified and all storage and system resources used by the device are released. 
    这不是讲的很清楚了吗?
      

  6.   

    这个release是指把hBmp选出了这个hDc还是等价于DeleteObject hBmp?
      

  7.   

    还都不大一样,比把hBmp选出HDC更彻底一些,因为DC对象上所有资源被释放了,但又没有对hBmp的销毁,hBmp仍然是bitmap的句柄,如果DeleteObject的话,那么hBmp将不再是合法的句柄。还有,请你注意我前面提到的后面几句话:
    An application should not call DeleteDC if objects have been selected into the device context. Objects must first be selected out of the device context before it it is deleted. 
    请先选出object再DeleteDCAn application must not delete a device context whose handle was obtained by calling CWnd::GetDC. Instead, it must call CWnd::ReleaseDC to free the device context. The CClientDC and CWindowDC classes are provided to wrap this functionality. The DeleteDC function is generally used to delete device contexts created with CreateDC, CreateIC, or CreateCompatibleDC.
    也就是说DeleteDC请用在内存DC上
      

  8.   

    这样?
    hOldBmp=SelectObject(hDc,hNewBmp)
    ...
    SelectObject(hOldBmp)但是hOldBmp的值是0x0185000f,不知道是什么?上面第一段话好像有点不对,创建一个hDcMem,不就包含了默认了对象了吗?