Results 1 to 6 of 6

Thread: Retrieving LogFonts

  1. #1
    Ariad
    Guest

    Retrieving LogFonts

    Hello,

    Using a combination of a LogFont strucutre and calls to CreateFontIndirect and SelectObject I can easily change and restore the font on a memory object.

    However, I haven't a clue how I'm can retreive that font, preferably back into a LogFont structure.

    Does anybody know how to obtain the current font of a memory object?

  2. #2
    Frenzied Member
    Join Date
    Aug 2001
    Posts
    1,075
    I'm not sure if this is what you're looking for but her it goes.


    Private Const OBJ_FONT = 6
    Private Declare Function GetWindowDC Lib "user32" (ByVal hWnd As Long) As Long
    Private Declare Function GetCurrentObject Lib "GDI32" (ByVal hDC As Long, ByVal uObjectType As Long) As Long
    Private Declare Function GetObject Lib "GDI32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long

    Dim lFontData As LOGFONT
    Dim hWinDC As Long
    Dim lFont As Long


    hWinDC = GetWindowDC(Form1.hWnd)
    lFont = GetCurrentObject(hWinDC, OBJ_FONT)
    GetObject lFont, Len(lFontData), lFontData


    Greg

  3. #3
    Ariad
    Guest
    Greg,

    Thanks for the code sample - this is exactly what I was looking for.

  4. #4
    Addicted Member
    Join Date
    Oct 2000
    Location
    Orlando, FL
    Posts
    253
    Thats really kewl. Is there a way to retrieve the font, change a parameter in the Font Structure and return it back to the control?
    Always looking for a better and faster way!

  5. #5
    Frenzied Member
    Join Date
    Aug 2001
    Posts
    1,075
    Yes, after you have the font with the above code you can change it like this...

    VB Code:
    1. 'Change size and weight
    2.     lFontData.lfWeight = FW_NORMAL
    3.     lFontData.lfHeight = -MulDiv(8, GetDeviceCaps(hWinDC, LOGPIXELSX), 72
    4.  
    5.     ' Change to MS Sans Serif
    6.     ' FontName has to be stored in a byte array
    7.     sFontName = "MS Sans Serif"
    8.     For lRet = 1 To Len(sFontName)
    9.         lFontData.lfFaceName(lRet) = Asc(Mid$(sFontName, lRet, 1))
    10.     Next
    11.     lFontData.lfFaceName(Len(sFontName) + 1) = 0
    12.      
    13.     ' Create the new Font and then set it to hWinDC
    14.     lNewFont = CreateFontIndirect(lFontData)
    15.     lFont = SelectObject(hWinDC, lNewFont)
    16.    
    17.     ' After you finish with it
    18.     ' Restore the old font
    19.     SelectObject hWinDC, lFont
    20.     ' Delete the font we made
    21.     DeleteObject lNewFont

    Greg
    Free VB Add-In - The Reference Librarian
    Click Here for screen shot and download link.

  6. #6
    Banned Motxopro's Avatar
    Join Date
    Dec 2001
    Posts
    57
    booyah he's right

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