我几乎从没有显式调用过这个方法,也从没遇到过问题。
想问一下CWnd::ReleaseDC()这个方法究竟什么时候需要显式调用?MSDN上的关于GetDC()的说明:
Unless the device context belongs to a window class, the ReleaseDC
member function must be called to release the context after painting.A device context belonging to the CWnd class is returned by the GetDC
member function if CS_CLASSDC, CS_OWNDC, or CS_PARENTDC was specified
as a style in the WNDCLASS structure when the class was registered.个人理解是否缺省情况下,GetDC()得到的CDC*就是“归属于”CWnd类的?

解决方案 »

  1.   

    使用GetClassLong方法验证过了,确实如此。
    缺省情况下,GetDC()得到的CDC*就是“归属于”CWnd类的。也就是说,ReleaseDC()只有对于自注册的窗体类,才可能是需要的。
      

  2.   

    举个例子,CImage也是有GetDC(),那算你的缺省情况么
      

  3.   

    CImage::GetDC()明明有歧义,那个实际上是Create出来的一个兼容DCinline HDC CImage::CDCCache::GetDC() throw()
    {
    HDC hDC; for( int iDC = 0; iDC < CIMAGE_DC_CACHE_SIZE; iDC++ )
    {
    hDC = static_cast< HDC >( InterlockedExchangePointer( reinterpret_cast< void** >(&m_ahDCs[iDC]), NULL ) );
    if( hDC != NULL )
    {
    return( hDC );
    }
    } hDC = ::CreateCompatibleDC( NULL ); return( hDC );
    }inline void CImage::CDCCache::ReleaseDC( HDC hDC ) throw()
    {
    for( int iDC = 0; iDC < CIMAGE_DC_CACHE_SIZE; iDC++ )
    {
    HDC hOldDC; hOldDC = static_cast< HDC >( InterlockedExchangePointer( reinterpret_cast< void** >(&m_ahDCs[iDC]), hDC ) );
    if( hOldDC == NULL )
    {
    return;
    }
    else
    {
    hDC = hOldDC;
    }
    }
    if( hDC != NULL )
    {
    ::DeleteDC( hDC );
    }
    }inline HDC CImage::GetDC() const throw()
    {
    ATLASSUME( m_hBitmap != NULL ); m_nDCRefCount++;
    if( m_hDC == NULL )
    {
    m_hDC = s_cache.GetDC();
    m_hOldBitmap = HBITMAP( ::SelectObject( m_hDC, m_hBitmap ) );
    } return( m_hDC );
    }inline void CImage::ReleaseDC() const throw()
    {
    HBITMAP hBitmap; ATLASSUME( m_hDC != NULL ); m_nDCRefCount--;
    if( m_nDCRefCount == 0 )
    {
    hBitmap = HBITMAP( ::SelectObject( m_hDC, m_hOldBitmap ) );
    ATLASSERT( hBitmap == m_hBitmap );
    s_cache.ReleaseDC( m_hDC );
    m_hDC = NULL;
    }
    }
      

  4.   

    你在继承CWnd类的派生类的函数中,通常用在比如CView类的OnDraw,CDilaog类的OnPaint函数中,你通过CWnd::GetDC()得到CDC类的指针,然后绘图,最后绘制完成以后调用CWnd::ReleaseDC(CDC*);来释放掉你通过CWnd::GetDC()返回的CDC*指针
      

  5.   


    不需要的,而且OnPaint()中应该用CPaintDC的