Announcement

Collapse
No announcement yet.

The value of hObj doesn't return to zero after calling DeleteObject hObj

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • The value of hObj doesn't return to zero after calling DeleteObject hObj

    All this screen painting is new to me.

    The value of
    hObj doesn't return to zero after calling DeleteObject hObj - is that correct?
    Do I need to "release" the hdc ?
    Anything incorrect below?


    Code:
    FUNCTION SET_FONT_PARAMETERS(hWnd AS DWORD)AS LONG
    
    LOCAL hdc AS LONG
    LOCAL TM AS TEXTMETRIC
    LOCAL hObj AS DWORD
    
    FONT_PARAMETERS.FONT_HANDLE = MakeFontEx("Courier New", 12, %FW_NORMAL, 0, 0)
    
    hdc = GetDC (hWnd)
    hObj = SelectObject (hDC, FONT_PARAMETERS.FONT_HANDLE)
    GetTextMetrics (hdc, TM)
    FONT_PARAMETERS.FONT_WIDTH = TM.tmAveCharWidth
    DeleteObject hObj
    
    END FUNCTION



    I have also seen this:
    DeleteObject SelectObject (hDC, GL_hFONT_COURIER_NEW_12_NORMAL)

    Is that giving me the original obj handle?

  • #2
    SelectObject (hDC, GL_hFONT_COURIER_NEW_12_NORMAL) gives you the previous selection that was replaced by your current selection.

    Comment


    • #3
      > The value of hObj doesn't return to zero after calling DeleteObject hObj

      Why would you expect it to?

      hObj is just a variable you have used to store the value of the handle which is/was assigned to an Object.
      It will remain unchanged until you assign a different value to it.


      (A handle is not a "thing", its just a numeric value used to identify an object)

      Comment


      • #4
        Code:
        LOCAL hdc AS LONG
        LOCAL TM AS TEXTMETRIC
        LOCAL hFont AS DWORD
        local hOldFont AS DWORD
        local hDummy AS DWORD
        hFont = MakeFontEx("Courier New", 12, %FW_NORMAL, 0, 0)
        hdc = GetDC (hWnd)
        ' -------------------------------------------
        ' return value is the previous object (in this case font handle) in DC
        ' always store it !
        ' -------------------------------------------
        hOldFont = SelectObject (hDC, hFont)
        ' -------------------------------------------
        GetTextMetrics (hdc, TM)
        FONT_PARAMETERS.FONT_WIDTH = TM.tmAveCharWidth
        ' -------------------------------------------
        ' always restore Dc back to its original state
        ' in this case select back old font into DC
        ' -------------------------------------------
        hDummy = SelectObject (hDC, hOldFont)
        ' -------------------------------------------
        ' now you can delete the temporary font handle
        ' since it is no longer selected into the DC
        ' never delete an object while it is still selected
        ' into a DC
        ' the return value hDummy should be the same value as hFont
        ' since that was the font you selected into the Dc before.
        ' -------------------------------------------
        DeleteObject hFont
        ' -------------------------------------------
        ' Just because you deleted the object handle from the system
        ' does not mean Windows changed the value of the variable hFont
        ' it still holds the handle value, even though the handle is now
        ' invalid. You can set the variable back to zero if need just by
        ' a simple: hFont = 0
        ' after this
        ' -------------------------------------------
        Chris Boss
        Computer Workshop
        Developer of "EZGUI"
        http://cwsof.com
        http://twitter.com/EZGUIProGuy

        Comment


        • #5
          Thanks guys!

          Comment

          Working...
          X