![]() |
|
Text and FontsExample: font_one ![]() Loading FontsThe Win32 GDI has some remarkable capabilites for dealing with vastly different typefaces,
styles, languages and characters sets. One of the drawbacks of this is that dealing with
fonts can look rather intimidating to the newcomer.
Fortunately, it's not really has hard as it might appear, and a large portion of the work
involved is taken care of my sensible default values. All but 2 of the parameters to
The members of the VAR hdc : HDC; hf : HFONT; lfHeight : INTEGER; BEGIN (* ... *) hdc := GetDC(hwnd); lfHeight:= -MulDiv(12, GetDeviceCaps(hdc, LOGPIXELSY), 72); ReleaseDC(hwnd, hdc); hf := CreateFont(lfHeight,0,0,0,0,1,0,0,0,0,0,0,0,"Times New Roman"); IF hf#NIL THEN DeleteFont(g_hfFont); g_hfFont := hf; ELSE MessageBox(hwnd, "Font creation failed!", "Error", MB_OK BOR MB_ICONEXCLAMATION); END;
This is the code used to create the font in the example image. This is Times New Roman at 12 Point with
the Italics style set. The italics flag is the 6th parameter to
The one bit of trickery in this code is the value used for the size of the font, the The reason this situation exists is because the resolution of different devices is so vastly different... Printers can easily display 600 to 1200 pixels per inch, while a screen is lucky to get 200... if you used the same sized font on a printer as on a screen, you likely wouldn't even be able to see individual letters.
All we have to do is convert from the point size we want, into the appropriate logical size for the device. In this
case the device is the screen, so we get the Default FontsWhen you first call
This is
a system object and you can get it as many times as you want without leaking memory,
and you can call Drawing TextNow that we have a handy-dandy font, how do we get some text on the screen? This is assuming that we don't just want to use an Edit or Static control.
Your basic options are PROCEDURE DrawClientSize(hdc : HDC; prc : RECT; hf : HFONT); VAR Str,Title : ARRAY [0..80] OF CHAR; hfOld : HFONT; BEGIN Title := "These are the dimensions of your client area:"; hfOld := SelectFont(hdc, hf); FUNC SetBkColor(hdc, g_rgbBackground); FUNC SetTextColor(hdc, g_rgbText); IF g_bOpaque THEN FUNC SetBkMode(hdc, OPAQUE); ELSE FUNC SetBkMode(hdc, TRANSPARENT); END; FUNC DrawText(hdc, Title, -1, prc, DT_WORDBREAK); FUNC wsprintf(Str,"{%d, %d, %d, %d}",prc.left,prc.top,prc.right,prc.bottom); FUNC DrawText(hdc, Str, -1, prc, DT_SINGLELINE BOR DT_CENTER BOR DT_VCENTER); FUNC SelectFont(hdc, hfOld); END DrawClientSize;
First thing we do is use
Next we set the Text and Background colours. Setting the background colour doesn't actually make the whole
background this colour, it only affects certain operations (text being one of them) that use the background colour to
draw with. This is also dependant on the current Background Mode. If it is set to
Now we actually draw the text using
In the first call, we specify
For the second call, we're only printing a single line without wrapping, and we want it to be centered horizontally as well
as vertically (which Client RedrawJust a note about the example program... when the Selecting FontsIn general, any program that deals with fonts will want to let the user choose their own font, as well as the colour and style attribute to use when displaying it.
Like the common dialogs for getting open and save file names, there is a common dialog for choosing a font.
This is, oddly enough, called The following global variables: VAR g_hfFont : HFONT; g_bOpaque : BOOL; g_rgbText : COLORREF; have been initialised when our main window was created: | WM_CREATE : g_hfFont := GetStockFont(DEFAULT_GUI_FONT); g_bOpaque := TRUE; g_rgbText := RGB(0, 0, 0); This is our procedure to select a font: PROCEDURE DoSelectFont(hwnd : HWND); VAR cf : COMMDLG.CHOOSEFONT; lf : LOGFONT; hf : HFONT; BEGIN FUNC GetLOGFONT(g_hfFont, lf); cf.lStructSize := SIZE(cf); cf.Flags := COMMDLG.CF_EFFECTS BOR COMMDLG.CF_INITTOLOGFONTSTRUCT BOR COMMDLG.CF_SCREENFONTS; cf.hwndOwner := hwnd; cf.lpLogFont := ADR(lf); cf.rgbColors := g_rgbText; IF COMMDLG.ChooseFont(cf) THEN hf := CreateFontIndirect(lf); IF hf#NIL THEN g_hfFont := hf; ELSE FUNC MessageBox(hwnd, "Font creation failed!", "Error", MB_OK BOR MB_ICONEXCLAMATION); END; g_rgbText := cf.rgbColors; END; END DoSelectFont;
The
The easiest way to use this dialog is in conjunction with an existing
Oddly enough, the Bold and Italics styles don't count as effects, they are considered part of the font itself
and in fact some fonts only come in Bold or Italics. If you want to check or prevent the user from selecting
a bold or italic font you can check the
The colour of a font is not associated with an
Selecting ColoursIn order to allow the user to change just the colour of the font, or to let them pick a new colour
for anything at all, there is the The following global variables: VAR g_rgbBackground : COLORREF; g_rgbCustom : ARRAY [0..15] OF COLORREF; have been initialised when our main window was created: | WM_CREATE : g_rgbBackground := RGB(255, 255, 255); FOR i:=0 TO HIGH(g_rgbCustom) DO g_rgbCustom[i]:=RGB(0, 0, 0); END; This is the code used to allow the user to select the background colour in the example program. PROCEDURE DoSelectColour(hwnd : HWND); VAR cc : COMMDLG.CHOOSECOLOR; BEGIN cc.lStructSize := SIZE(cc); cc.Flags := COMMDLG.CC_RGBINIT BOR COMMDLG.CC_FULLOPEN BOR COMMDLG.CC_ANYCOLOR; cc.hwndOwner := hwnd; cc.rgbResult := g_rgbBackground; cc.lpCustColors := ADR(g_rgbCustom); IF COMMDLG.ChooseColor(cc) THEN g_rgbBackground := cc.rgbResult; END; END DoSelectColour;
This is fairly straightforward, again we're using the
The Control FontsSomething else you might want to do at some point is change the font on the controls on your
dialog or window. This is usually the case when using I've done this in previous examples, but it makes sense to mention it here because it's relevant and very short: SendDlgItemMessage(hwnd, IDC_OF_YOUR_CONTROL, WM_SETFONT, CAST(WPARAM,hfFont), TRUE);
Where
Copyright © 1998-2011, Brook Miles. All rights reserved. Adapted for Modula-2 by Frank Schoonjans, with permission. |