看下面两段代码:
void CMyView::OnLButtonDown(...)
{
    CRect rect;
    CClientDC dc(this);
    dc.GetClipBox(rect);
}void CMyView::OnLButtonDown(...)
{
    CRect rect;
    CDC* pDC
    pDC->GetClipBox(rect);
    ReleaseDC(pDC);
}前一段代码中CDC对象在函数返回时自动就被释放了,那么,后一段代码中的pDC的具有怎样的生存期?它所指向的对象是何时创建的,由谁创建的?
另外,还有一个小问题。第一段代码中CPaintDC dc(this)中的this指向什么,是窗口类对象吗?如果是,怎么能用它来创建CPaintDC对象。
我是初学Windows编程,请各位高手多多指教,谢谢了。

解决方案 »

  1.   

    CDC* pDC;
    pDC->GetClipBox(rect);
    这样使用肯定报错
    CDC* pDC = GetDC();
    pDC->GetClipBox(rect);
    ...
    //释放
    ReleaseDC(pdc);   
    CClientDC dc(this)中的this是指CMyView对象的指针
      

  2.   

    你的第二个都没有实题话一下,怎么能够找到呢,你要先new一个,然后调用,生存期在它的作用域中
      

  3.   

    CDC* pDC = GetDC();//这个时候创建这个pDC的生存期在ReleaseDC(pdc);之后结束this是View对象的指针
      

  4.   

    this
    通 this 找到窗体句柄
      

  5.   

    CWinApp::OnIdle() calls AfxUnlockTempMaps with bDeleteTemp parameter set to true where the temporary object-handle maps are actually deleted. CWnd::GetDC creates a temporary object-handle map, if a permanent one is not found.
      

  6.   


    CDC* pDC;
    pDC->GetClipBox(rect);
    这样使用肯定报错
    CDC* pDC = GetDC();
    pDC->GetClipBox(rect);
    ...
    //释放
    ReleaseDC(pdc);   
    CClientDC dc(this)中的this是指CMyView对象的指针
    CDC* pDC = GetDC();//这个时候创建这个pDC的生存期在ReleaseDC(pdc);之后结束this是View对象的指针
    这两个地回答比较完全了。
      

  7.   

    void CMyView::OnLButtonDown(...)
    {
        CRect rect;
        CClientDC dc(this);
        dc.GetClipBox(rect);
    }void CMyView::OnLButtonDown(...)
    {
        CRect rect;
        CDC* pDC=GetDC();
        pDC->GetClipBox(rect);
        ReleaseDC(pDC);
    }
    =======================
    其实两者都实现了ReleaseDC(objPt);只是前者比较隐蔽。CClientDC是CDC的一个子类,它的析构函数调用了ReleaseDC(objPt)。世纪上它还是调用了ReleaseDC(objPt)
      

  8.   

    CClientDC 是个封装类。  和  
    CDC* pDC=GetDC();
    pDC->GetClipBox(rect);
    ReleaseDC(pDC);