The easiest way to ensure that the device context object is destroyed (and that the underlying Windows device context is released) is to construct the object on the stack in the following way:这里的stack和C++中的stack是一回事么?我想象不出下面的代码有“先进后出“的特征,请帮忙解释一下,谢谢!!void CMyView::OnLButtonDown(UINT nFlags, CPoint point)
{
    CRect rect;    CClientDC dc(this);  // constructs dc on the stack
    dc.GetClipBox(rect); // retrieves the clipping rectangle
} // dc automatically releasedNotice that the CClientDC constructor takes a window pointer as a parameter. The destructor for the CClientDC object is called when the function returns.”The destructor for the CClientDC object is called when the function returns.“其中的function是指CClientDC的构造函数么?构造函数一返回就调用析构函数,那对象dc不就不存在了么?那
dc.GetClipBox(rect);这代码怎么执行?请帮忙解释下,谢谢~~刚学VC,请大家不要见笑~~ 以上英文及代码出自VC6技术内幕第五章开头~~请大家帮忙,谢谢~

解决方案 »

  1.   

    我也不太懂,但我知道dc会自动析构,但却是在dc.getClipBox(rect);前,所以dc那时候还存在。但不太明白GetClipBox()函数有什么用。
      

  2.   

    TO zswzwy(酒是穿肠毒药 @ 色是刮骨钢刀):
    原文:
    Notice that the CClientDC constructor takes a window pointer as a parameter. The destructor for the CClientDC object is called when the function returns.”The destructor for the CClientDC object is called when the function returns.“其中的function是指CClientDC的构造函数么?
    那你认为这里的function是指什么呢?
      

  3.   

    是这样的,这里所说的栈的确是c++中的栈。
    他的意思就是CClientDC dc(this);//在stack中建立dc对象,调用constructor.void CMyView::OnLButtonDown(UINT nFlags, CPoint point)
    {
        CRect rect;    CClientDC dc(this);  // constructs dc on the stack
        dc.GetClipBox(rect); // retrieves the clipping rectangle
    } //这里函数在栈中将被弹出,所以dc对象将被撤消,调用distructor.为什么要调用distructor?因为CDC类的对象拥有了资源,为了不让资源泄漏,ms在他的析构函数中会撤消资源,比如忘了释放的加载到dc中的位图资源等。而这个过程在CDC的distructor中做是最好的。如果不在堆上建立CDC对象,有可能冒忘记撤消资源的危险。