Obtaining the system font in custom size

This is a discussion on Obtaining the system font in custom size within the Windows Programming forums, part of the Platform Specific Boards category; I am having a great deal of trouble for what would seem to be an easy task: I need to ...

  1. #1
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591

    Obtaining the system font in custom size

    I am having a great deal of trouble for what would seem to be an easy task: I need to obtain the system font and specify its size.
    I found this incredibly easy and stress-free to do in a dialog box, all I had to do was include the "DS_SHELLFONT" flag and then the statement "FONT 8, "MS Shell Dlg"".
    However, doing this in a non-resource, non-dialog situation seems to be unnecessarily difficult:
    - GetStockObject(SYSTEM_FONT) does not allow you to specify the size (so I end up with the right font... but wrong (huge) size)
    - CreateFont did not seem to return the correct font when specifying the font as "MS Shell Dlg". (plus its many parameters make it cumbersome and hard to negotiate)

    So how would I get the system font and then change its size to a more appropriate value for use with my GUI?
    Thanks.

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Code:
    LOGFONT lf;
    
    ::GetObject(::GetStockObject(SYSTEM_FONT), sizeof lf, &lf);
    
    // Modify lf
    
    HFONT hf = ::CreateFontIndirect(&lf);
    Unknown crash. LOGFONT, static, MSDN.
    http://msdn.microsoft.com/library/de...ntext_1wmq.asp

  3. #3
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Use of SYSTEM_FONT and DEFAULT_GUI_FONT is outdated. Using "MS Shell Dlg" should work. An alternative is to use the font used for message box text:
    Code:
    NONCLIENTMETRICS ncm = { sizeof(NONCLIENTMETRICS) };
    SystemParametersInfo(SPI_GETNONCLIENTMETRICS, 0, &ncm, 0);
    hFont = CreateFontIndirect(&ncm.lfMessageFont);

  4. #4
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    awesome, thanks man!
    p.s Its funny because I was actually just reading your other thread and the LOGFONT entry in MSDN just before posting, but somehow it never makes sense to me until someone posts some sample code (like yours) that consolidates it all.

    -edit: It is probably the case that I am using the CreateFont method incorrectly and that "MS Shell Dlg" does work. I can get it to return the right font (tahoma for XP) but only on *certain* height/width values. I.E. if I specify 8,0 it will create the right font (tahoma) but too small, if I specify a larger size such as 12,0 it diverts back to using MS San Serif. I don't fully understand how emHeight and avgWidth impact on the font or its final size, but I gather they have something to do with CreateFont approximating fonts to choose the one that is most similar to the attributes supplied and not necessarily the one provided in the typename parameter. Since it would appear that there are certain valid sizes that must be used in conjunction with certain fonts and since I don't know the exact size values to use in correspondance with each font (and wouldn't know how to get such values), I'll probably opt out and try the alternative message-box method.
    Last edited by @nthony; 07-31-2006 at 09:19 PM.

  5. #5
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    CreateFont is one of those functions, you figure out once and then copy and paste whenever you need it again. Most of the arguments should be default values. Something like this should work:
    Code:
    	const INT ITEM_POINT_SIZE = 9;
    	HDC hdc = GetDC(hwnd);
    	INT nFontHeight = MulDiv(ITEM_POINT_SIZE, GetDeviceCaps(hdc, LOGPIXELSY), 72);
    	HFONT hFont = CreateFont(nFontHeight, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS,
    	                         CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, TEXT("MS Shell Dlg"));
    
    	ReleaseDC(hwnd, hdc);

  6. #6
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    wow, I would have never come up with that on my own, thanks.
    When I use the NONCLIENTMETRICS way, in XP, I get Tahoma, but when I use the above way I get MS Sans Serif... so I'm still a little bit mystified as to which is the official default system font.
    In XP, on most menus, messageboxes and button labels, Tahoma is used, however sometimes in editboxes and text boxes, MS Sans Serif is used. I'm still pretty new to the realm of windows programming, so can I ask you guys, when should/do you use Tahoma vs MS Sans Serif font?

  7. #7
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    >> so I'm still a little bit mystified as to which is the official default system font. <<

    Tahoma is the font used by Windows 2000 and XP. However, to get this font you need to use the typeface "MS Shell Dlg 2". If you use the "MS Shell Dlg", the old font is used for backwards compatibility. "MS Shell Dlg 2" is only available on 2000 and XP, so if you want to target older platforms you need to provide appropriate code:
    Code:
    	const INT     ITEM_POINT_SIZE = 9;
    	LPTSTR        szFont = TEXT("MS Shell Dlg");
    	HFONT         hFont  = NULL;
    	OSVERSIONINFO osv    = { sizeof(OSVERSIONINFO) };
    	HDC           hdc    = GetDC(hwnd);
    	INT           nFontHeight = MulDiv(ITEM_POINT_SIZE, GetDeviceCaps(hdc, LOGPIXELSY), 72);
    
    	GetVersionEx(&osv);
    
    	if (osv.dwPlatformId == VER_PLATFORM_WIN32_NT &&
    	    osv.dwMajorVersion >= 5)
    	{
    		/* On Windows 2000 or greater. Use the new-look font. */
    		szFont = TEXT("MS Shell Dlg 2");
    	}
    
    	hFont = CreateFont(nFontHeight, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS,
    	                   CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, szFont);
    
    	ReleaseDC(hwnd, hdc);
    In XP, on most menus, messageboxes and button labels, Tahoma is used, however sometimes in editboxes and text boxes, MS Sans Serif is used.
    Well, this may be deliberate (a decision has been made that Sans Serif is more appropriate), it may be a mistake (Microsoft programmers are not perfect) or it may just be that the code has not been updated from older platforms.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Console Font Size
    By bradszy in forum Windows Programming
    Replies: 34
    Last Post: 04-26-2008, 07:09 AM
  2. Replies: 16
    Last Post: 11-23-2007, 12:48 PM
  3. Opinions on custom system build
    By lightatdawn in forum Tech Board
    Replies: 2
    Last Post: 10-18-2005, 04:15 AM
  4. An exercise in optimization
    By Prelude in forum Contests Board
    Replies: 10
    Last Post: 04-29-2005, 03:06 PM
  5. Confusion inputing custom size variables?
    By thenrkst in forum C++ Programming
    Replies: 5
    Last Post: 04-24-2003, 08:02 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21