CPen* hOldPen;
CPen hPen(PS_SOLID, 2, RGB(0,0,255));
hOldPen=(CPen*)dc.SelectObject (&hPen);     use hPen hPen.DeleteObject();
dc.SelectObject(hOldPen);
一定要删除!!

解决方案 »

  1.   

    需不需要Delete,要看你用不用New
      

  2.   

    One of the most frequent misconceptions I've seen is that you have to allocate a drawing object in order to use it. So I see code of the formCPen * myPen = new CPen;
    CPen->Create(...);
    CPen * OldPen = dc->SelectObject(myPen);
    ...
    delete myPen;
    This is unnecessarily complex code. At least part of the confusion is that the SelectObject method wants a CPen * (or in general, a "Tool *"), which leads programmers to believe that they must supply a CPen * variable. Nothing in the specification of the call requires that the object  be allocated on the heap; only that a pointer to an object be provided. This can be done by doingCPen myPen(...);
    CPen * OldPen = dc->SelectObject(&myPen);
    ...
    dc->SelectObject(OldPen);
    This is much simpler code; it doesn't call the allocator. And the parameter &myPen satisfies the requirement of a CPen *. Since pens and other GDI tools are often created "on the fly" and discarded afterwards, there is no need to allocate them on the heap. When the destructor is called when you leave scope, the HPEN underlying the CPen is normally destroyed--but see below for when it is not!
      

  3.   

    One of the most frequent misconceptions I've seen is that you have to allocate a drawing object in order to use it. So I see code of the formCPen * myPen = new CPen;
    CPen->Create(...);
    CPen * OldPen = dc->SelectObject(myPen);
    ...
    delete myPen;
    This is unnecessarily complex code. At least part of the confusion is that the SelectObject method wants a CPen * (or in general, a "Tool *"), which leads programmers to believe that they must supply a CPen * variable. Nothing in the specification of the call requires that the object  be allocated on the heap; only that a pointer to an object be provided. This can be done by doingCPen myPen(...);
    CPen * OldPen = dc->SelectObject(&myPen);
    ...
    dc->SelectObject(OldPen);
    This is much simpler code; it doesn't call the allocator. And the parameter &myPen satisfies the requirement of a CPen *. Since pens and other GDI tools are often created "on the fly" and discarded afterwards, there is no need to allocate them on the heap. When the destructor is called when you leave scope, the HPEN underlying the CPen is normally destroyed--but see below for when it is not!
      

  4.   

    大虾们,还是不行呀,资源一会儿就没有了我把代码贴出来你们看看,是不是因为我把它放在MouseMove里面的关系呀?
    void CMONEView::OnMouseMove(UINT nFlags, CPoint point) 
    {
    CClientDC dc(this); CPen* hOldPen;
    CPen hPen(PS_SOLID, 2, RGB(0,0,255));

    hOldPen=(CPen*)dc.SelectObject (&hPen);
    hPen.DeleteObject(); .
    .
    .
    .
    dc.SelectObject(hOldPen);

    CView::OnMouseMove(nFlags, point);
    }
      

  5.   

    不要用new CPen

    CPen myPen(...);
    CPen * OldPen = dc->SelectObject(&myPen);
    ...
    dc->SelectObject(OldPen);
      

  6.   

    忘了加
    OldPen->DeleteObject()
      

  7.   

    void CMONEView::OnMouseMove(UINT nFlags, CPoint point) 
    {
    CClientDC dc(this); CPen* hOldPen;
    CPen hPen(PS_SOLID, 2, RGB(0,0,255));

    hOldPen=(CPen*)dc.SelectObject (&hPen); .
    .
    .
    .
    hPen.DeleteObject();
    dc.SelectObject(hOldPen);

    CView::OnMouseMove(nFlags, point);
    }
      

  8.   

    收到了。对于你的资源泄漏。在deleteobject之前,必须先selectobject还原以前的设备环境。再deleteobject才行
      

  9.   

    void CMONEView::OnMouseMove(UINT nFlags, CPoint point) 
    {
    CClientDC dc(this); CPen* hOldPen;
    CPen hPen(PS_SOLID, 2, RGB(0,0,255));

    hOldPen=(CPen*)dc.SelectObject (&hPen); .
    .
    .
    .
    dc.SelectObject(hOldPen);
    hPen.DeleteObject();

    CView::OnMouseMove(nFlags, point);
    }try it again
      

  10.   

    freelybird(阿愚)是对的,应该在画笔使用完以后再Delete,问题已解决,散分!(不好意思,分给得太少了,下次一定多给一点。)