Results 1 to 6 of 6

Thread: [RESOLVED] Get title bar font

  1. #1

    Thread Starter
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Resolved [RESOLVED] Get title bar font

    I need to get both the font name and size of the title bar font. The following code from here gets me most of the way:
    vb Code:
    1. Option Explicit
    2.  
    3. Private Const LF_FACESIZE = 32
    4. Private Const SPI_GETNONCLIENTMETRICS = 41
    5.  
    6. Private Type LOGFONT
    7.     lfHeight As Long
    8.     lfWidth As Long
    9.     lfEscapement As Long
    10.     lfOrientation As Long
    11.     lfWeight As Long
    12.     lfItalic As Byte
    13.     lfUnderline As Byte
    14.     lfStrikeOut As Byte
    15.     lfCharSet As Byte
    16.     lfOutPrecision As Byte
    17.     lfClipPrecision As Byte
    18.     lfQuality As Byte
    19.     lfPitchAndFamily As Byte
    20.     lfFaceName(1 To LF_FACESIZE) As Byte
    21. End Type
    22.  
    23. Private Type NONCLIENTMETRICS
    24.     cbSize As Long
    25.     iBorderWidth As Long
    26.     iScrollWidth As Long
    27.     iScrollHeight As Long
    28.     iCaptionWidth As Long
    29.     iCaptionHeight As Long
    30.     lfCaptionFont As LOGFONT
    31.     iSMCaptionWidth As Long
    32.     iSMCaptionHeight As Long
    33.     lfSMCaptionFont As LOGFONT
    34.     iMenuWidth As Long
    35.     iMenuHeight As Long
    36.     lfMenuFont As LOGFONT
    37.     lfStatusFont As LOGFONT
    38.     lfMessageFont As LOGFONT
    39. End Type
    40.  
    41. Private Declare Function SystemParametersInfo Lib "user32" Alias "SystemParametersInfoA" (ByVal uAction As Long, ByVal uParam As Long, ByRef lpvParam As Any, ByVal fuWinIni As Long) As Long
    42.  
    43. Public Function ActiveTitleBarFontName()
    44.     Dim s As String
    45.     Dim i As Byte
    46.     Dim ncm As NONCLIENTMETRICS
    47.     Dim sdfont As StdFont
    48.    
    49.     ncm.cbSize = Len(ncm)
    50.     If SystemParametersInfo(41, ncm.cbSize, ncm, 0) Then
    51.         s = StrConv(ncm.lfCaptionFont.lfFaceName, vbUnicode)
    52.         i = InStr(s, vbNullChar)
    53.         If i > 0 Then s = Left(s, i - 1)
    54.     End If
    55.     ActiveTitleBarFontName = s
    56. End Function
    I must be a little slow, though, because I can't figure out how to determine the font size from that structure. On my system the ActiveTitleBar font is Tahoma 8, but the lfCaptionFont structure has the following values:

    lfHeight = -11
    lfWeight = 700

    Little help?
    Last edited by Ellis Dee; Jan 27th, 2010 at 08:52 PM.

  2. #2
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Get title bar font

    Not sure how its done, but a quick search on PSC found this example,

    Function GetCaptionFont...
    Dim WinFont As LogFont
    "Size = " & -(WinFont.FontHeight * (72 / GetDeviceCaps(Me.hDC, LOGPIXELSY)))

  3. #3

    Thread Starter
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Get title bar font

    Yeah, I just found this article on vbnet which has everything I'm looking for and then some. Notably it also returns Bold information, which can effect font sizes for display purposes. I'll post my finished function once I pare down that ocean of code to the bare minimum.

    Thanks for the quick response of the formula. I found a second solution that posted the same thing but I was going crazy trying to figure out where the heck hdc came from. My solution kept bombing out because I had moved the code to a bas module. I probably wouldn't have figured it out for a while with the vbnet code either, so your short and sweet response was actually quite helpful. Thanks much.

  4. #4
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Get title bar font

    That vbnet example seems to be good, my font size is set to 10 and it returns 10, whereas that PSC code returns 9.75.

  5. #5

    Thread Starter
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Get title bar font

    I would lend more credence to the PSC code simply because actual font sizes are very rarely perfect integers. As I found while writing my form resizing code, when you set a font to a size it usually snaps to a decimal value near what you sent. For example, setting a font to 10 will almost always result in an actual font of size 9.75.

    I think the distinction is mostly academic, though, because you'll get the same result if you set the font to either 10 or 9.75. On a conceptual level it's probably better to get the 10 value simply because you'll then be letting the OS handle the actual sizing the same way it does for the Appearance settings.

    I'm writing an mp3 player that handles 4+ hour long files gracefully, (eg: radio show mp3 files,) and would like it to snap to the titlebar similar to how WinAmp looks. I figured the ideal way to size the font would be to just use the OS's titlebar font size since that's where it will be.

    I'd rate you but once again I have to spread some around first. Which is really too bad; you and LaVolpe would both have gotten quite a bit more rating from me for all the threads you two have helped me in.

  6. #6

    Thread Starter
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Get title bar font

    Here's my finished solution, which will set a given form's font to the system titlebar font. The following goes in a bas module:
    vb Code:
    1. Option Explicit
    2.  
    3. Private Const LF_FACESIZE = 32
    4.  
    5. Private Type LOGFONT
    6.     lfHeight As Long
    7.     lfWidth As Long
    8.     lfEscapement As Long
    9.     lfOrientation As Long
    10.     lfWeight As Long
    11.     lfItalic As Byte
    12.     lfUnderline As Byte
    13.     lfStrikeOut As Byte
    14.     lfCharSet As Byte
    15.     lfOutPrecision As Byte
    16.     lfClipPrecision As Byte
    17.     lfQuality As Byte
    18.     lfPitchAndFamily As Byte
    19.     lfFaceName(0 To LF_FACESIZE - 1) As Byte
    20. End Type
    21.  
    22. Private Type NONCLIENTMETRICS
    23.     cbSize As Long
    24.     iBorderWidth As Long
    25.     iScrollWidth As Long
    26.     iScrollHeight As Long
    27.     iCaptionWidth As Long
    28.     iCaptionHeight As Long
    29.     lfCaptionFont As LOGFONT
    30.     iSMCaptionWidth As Long
    31.     iSMCaptionHeight As Long
    32.     lfSMCaptionFont As LOGFONT
    33.     iMenuWidth As Long
    34.     iMenuHeight As Long
    35.     lfMenuFont As LOGFONT
    36.     lfStatusFont As LOGFONT
    37.     lfMessageFont As LOGFONT
    38. End Type
    39.  
    40. Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Long, ByVal nIndex As Long) As Long
    41. Private Declare Function SystemParametersInfo Lib "user32" Alias "SystemParametersInfoA" (ByVal uAction As Long, ByVal uParam As Long, lpvParam As Any, ByVal fuWinIni As Long) As Long
    42.  
    43. ' Sets a form's font to Windows Titlebar font
    44. ' Thanks to Randy Birch of VBNet
    45. Public Sub SetFormFontToTitlebarFont(pfrm As Form)
    46.     Const SPI_GETNONCLIENTMETRICS = 41
    47.     Const LOGPIXELSY As Long = 90
    48.     Dim typMetric As NONCLIENTMETRICS
    49.    
    50.     typMetric.cbSize = Len(typMetric)
    51.     If SystemParametersInfo(SPI_GETNONCLIENTMETRICS, typMetric.cbSize, typMetric, 0) = 1 Then
    52.         pfrm.FontName = StrConv(typMetric.lfCaptionFont.lfFaceName, vbUnicode)
    53.         pfrm.FontSize = -typMetric.lfCaptionFont.lfHeight / (GetDeviceCaps(pfrm.hdc, LOGPIXELSY) / 72)
    54.         pfrm.FontBold = (typMetric.lfCaptionFont.lfWeight > 400)
    55.     End If
    56. End Sub
    Sample usage:
    Code:
    Private Sub Form_Load()
        SetFormFontToTitlebarFont Me
    End Sub

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