2

I am writing a small GUI library in OpenGL for fun and profit. When it comes to font handling, so far I require the client application to explicitly load all fonts and set them on the widgets. So far this is ok, I also have a means to set them once as a default for all widgets of a certain type.

Although this is feasible, I though, would it not be dandy to use the system's default font as a default instead. In the case of Windows this would be the fonts that are configured though the Color and Appearance dialog.

After warming up my rusted Win32 programming knowledge and souring the MSDN I can't find an awnser to this question. I can load a font by name and set it on any widget, but figuring out what font Win32 would use as a default eludes me.

So far the best I have found is the SystemParametersInfo function with SPI_GETNONCLIENTMETRICS. But these are just the settings for the "non client" area, such as window title bar and such.

rioki
  • 5,988
  • 5
  • 32
  • 55
  • I may have just stumbled over my solution; using `GetStockObject(DEFAULT_GUI_FONT)` and an educated guess via this aproach: http://www.codeproject.com/Articles/1235/Finding-a-Font-file-from-a-Font-name – rioki Feb 09 '16 at 09:49
  • `SPI_GETNONCLIENTMETRICS` is what you want to use, [forget about `DEFAULT_GUI_FONT`](https://blogs.msdn.microsoft.com/oldnewthing/20050707-00/?p=35013/) – Alex K. Feb 09 '16 at 10:41
  • 1
    Do this the same way that Microsoft GUI builders do it. Pick Segoe UI at 9 points as the default and let the font mapper find a substitute automatically on an old OS. Just make sure that child windows use their parent window's font as the default, that way the user just changes the main window font and everything looks consistent. – Hans Passant Feb 09 '16 at 12:21
  • 1
    The only problem with Hans's suggestion is that the font mapper is going to give you a 9-point font, which will be inconsistent with the font size used everywhere else on the system. And hard-coding a font size will get you into problems with users who have intentionally chosen a different sized font instead of changing their DPI settings. The Microsoft applications bundled with Windows have the luxury of only having to support a single version of Windows, and the Office team goes to a lot of trouble to basically reinvent the window manager. So I'm not sure what other apps do this. – Cody Gray - on strike Feb 09 '16 at 12:32
  • @CodyGray True that, but remember this a OpenGL GUI, designed to be used in full screen use and it's just an idea for the default fallback behavior. Also I am reinventing the wheel even more, since I am building an entire user interface library... – rioki Feb 09 '16 at 12:43
  • @HansPassant That won't do, since I am actually loading a true type font and rasterizing it to texture. I need an actual TTF file. I currently use DejaVu and package them in my asset folder; but that is in the client application, not the library. – rioki Feb 09 '16 at 12:49

1 Answers1

1

DEFAULT_GUI_FONT is not your solution. The name certainly sounds good, and indeed it was the default GUI font at some point in history, but that font hasn't been used in years.

You already stumbled upon the correct solution: calling SystemParametersInfo with the SPI_GETNONCLIENTMETRICS option. This will fill in a NONCLIENTMETRICS structure with information about the standard system fonts.

The "standard UI font" in that structure is called lfMessageFont. It is the one used for text in message boxes, dialog boxes, and elsewhere in the client area of windows. It is the same one configurable in the "Appearance" properties.

I wrote out a very detailed answer about fonts in Windows applications a few years ago. That one kind of focuses on MFC, so I've chosen not to mark this question as a duplicate of that other one and compose a separate answer, but really all of the information you need is there.

For fun, I'll throw in that you can get the system colors by calling the GetSysColor function. Pass one of the COLOR_* values to indicate which color you want; you'll get back a COLORREF value (typedefed as a 32-bit unsigned integer into which are packed the red, green, and blue component values of the color). Use the GetRValue, GetGValue, and GetBValue macros to extract the individual components; I doubt OpenGL wants COLORREF values.

Community
  • 1
  • 1
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • Thank you, sometimes you overlook the obvious. Then again "non client area" IS sort of misleading. – rioki Feb 09 '16 at 12:27