Results 1 to 3 of 3

Thread: SelectObject API function

  1. #1

    Thread Starter
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238

    SelectObject API function

    Juz a stupid Q, and take a look the code shown below:

    Code:
        Dim hdc As Long
        Dim hPen As Long
        Dim hOldPen As Long
        Dim pt As POINTAPI
        
        hdc = GetDC(Form1.hwnd)
        hPen = CreatePen(PS_SOLID, RGB(0, 255, 255))
        hOldPen = SelectObject(hdc, hPen)
        Polyline hdc, pt(0), 5
        SelectObject hdc, hOldPen
        DeleteObject hPen
        ReleaseDC Form1.hwnd, hdc
    Why there is a need to restore the old pen object into the device Context (DC)? What will happen if I does not restore it?

  2. #2
    gaffa
    Guest
    A simple test for you:

    Take out the restore pen code, then call the sub 1000000 times. Eventually you will run out of GDI handles, and the painting of all windows will go haywire

    Basically, you need to restore the handles to pens and other objects just so your app doens't chew up all the avaialble system handles. It's annoying, but the consequences if you don't are even more annoying.

    - gaffa

  3. #3

    Thread Starter
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    Thanks gaffa, I felt that after I change my code to restore the original pen object and now my app doesn't eat up all my memory

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