Results 1 to 4 of 4

Thread: DC's, Objects and Memory (oh my!)

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 1999
    Location
    Bethel, North Carolina, USA
    Posts
    987

    Question

    I know the following code essentially does nothing, it is meant solely for example purposes. My question is when you use the DeleteDC function does it destroy whatever objects are currently selected into it as well. I have read somewhere that you should restore the previous object to the DC when you are done performing whatever operation you are attempting to it, but what if you want to, say for example, change the Pen and leave it as it is, is it OK to use the DeleteObject function to get rid of the original Pen that was created with the DC??

    Code:
    Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
    Private Declare Function CreatePen Lib "gdi32" (ByVal nPenStyle As Long, ByVal nWidth As Long, ByVal crColor As Long) As Long
    Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
    Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
    Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
    
    Private Sub Form_Load()
     Dim lDC As Long
     Dim lPenNew As Long, lPenOld As Long
     Dim lRet As Long
    
     ' get handle to new dc
     lDC = CreateCompatibleDC(Me.hdc)
     ' get handle to new pen
     lPenNew = CreatePen(ps_solid, 1, vbRed)
     ' get handle to previous pen and select new pen into dc
     lPenOld = SelectObject(lDC, lPenNew)
     ' destroy old pen
     lRet = DeleteObject(lPenOld)
     ' destroy dc
     lRet = DeleteDC(lDC)
     
    End Sub
    ??
    {Insert random techno-babble here}

    {Insert quote from some long gone mofo here}

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    Sep 1999
    Location
    Bethel, North Carolina, USA
    Posts
    987
    Just wanted to see if anybody that comes on later in the day might have an opinion on this, it was on the 3rd page in a few hours when I originally posted it.
    {Insert random techno-babble here}

    {Insert quote from some long gone mofo here}

  3. #3
    Guest
    You need to put the original pen back into the DC before you delete the DC suing SelectObject. If you don't the handle to the pen is never released, and eventually you run out of handles to use and your application goes a little strange.

    So the long and the short of it is (like your mother told you when you were growing up), if you use it, put it back wher you got it from. This holds true for and handle. Store all the old handles, then once you are down, put the old handles back into the DC, then delete the DC and delete the objects you created.

    - gaffa

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Sep 1999
    Location
    Bethel, North Carolina, USA
    Posts
    987
    So even (as in my code above) you use the DeleteObject on the handle of the old pen (or any object for that matter) it is still using up memory. Just want to make sure I've got this right, the only way to remove the objects that are created along with the DC is to use the DeleteDC function??
    {Insert random techno-babble here}

    {Insert quote from some long gone mofo here}

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width