The GetBitmapFont function in GRAFMENU takes a parameter of 0, 1, or 2 and returns a handle to a bitmap. This bitmap contains the string ”Courier,“ ”Helvetica,“ or ”Times Roman“ in the appropriate font and about twice the size of the normal system font. Let's see how GetBitmapFont does it. (The code that follows is not the same as that in the GRAFMENU.C file. For purposes of clarity, I've replaced references to the lfSet structure with the values appropriate for Times Roman.)
The first step is to get a handle to the system font and use GetObject to copy characteristics of that font into the structure lf that has type LOGFONT (”logical font“):
hFont = GetStockObject (SYSTEM_FONT) ;
GetObject (hFont, sizeof (LOGFONT), (LPSTR) &lf) ;
Certain fields of this logical font structure must be modified to make it describe a larger Times Roman font:
lf.lfHeight *= 2 ;
lf.lfWidth *= 2 ;
lf.lfPitchAndFamily = VARIABLE_PITCH | FF_ROMAN ;
strcpy (lf.lfFaceName, "Tms Rmn") ;
The next step is to get a device context for the screen and create a memory device context compatible with the screen:
hdc = CreateIC ("DISPLAY", NULL, NULL, NULL) ;
hdcMem = CreateCompatibleDC (hdc) ;
The handle to the memory device context is hdcMem. Next, we create a font based on the modified lf structure and select that font into the memory device context:
SelectObject (hdcMem, CreateFontIndirect (&lf)) ;
Now when we write some text to the memory device context, Windows will use the Times Roman font selected into the device context.
But this memory device context still has a one-pixel monochrome device surface. We have to create a bitmap large enough for the text we want to display on it. You can obtain the dimensions of the text through GetTextExtent and create a bitmap based on these dimensions with CreateBitmap:
dwSize = GetTextExtent (hdcMem, "Times Roman", 11) ;
hBitmap = CreateBitmap (LOWORD (dwSize), HIWORD (dwSize), 1, 1, NULL) ;
SelectObject (hdcMem, hBitmap) ;
This device context now has a monochrome display surface exactly the size of the text. Now all we have to do is write the text to it. You've seen this function before:
TextOut (hdcMem, 0, 0, "Times Roman", 11) ;
We're finished except for cleaning up. To do so, we select the system font (with handle hFont) back into the device context using SelectObject, and we delete the previous font handle that SelectObject returns, which is the handle to the Times Roman font:
DeleteObject (SelectObject (hdcMem, hFont)) ;
Now we can also delete the two device contexts:
DeleteDC (hdcMem) ;
DeleteDC (hdc) ;
We're left with a bitmap that has the text ”Times Roman“ in a Times Roman font.