Font Class 1.1
This library gets around
Interop.Call's 12 parameter limit, allowing you to change the fonts of controls other than
ButtonControls,
RadioControls,
RichEditControls and
RichStaticControls! (Any resemblance to Eljay's
ToolTip Class is purely intentional.
)
Changes:
- Choose() - Create a choose font dialog box and load the selected font properties to the font object.
- toLogical() - Convert point units to logical units. See Notes section for more information.
- toPoint() - Convert logical units to point units. See Notes section for more information.
- UpdateProperties() - Minor improvement.
Parameters:
- Height - [Integer] Specifies the height of characters in the font.
- Width - [Integer] Specifies the average width of characters in the font.
- Escapement - [Integer] Specifies the angle between the escapement vector and the x-axis of the device.
- Orientation - [Integer] Specifies the angle between the base line of each character and the x-axis of the device.
- Weight - [Integer] Specifies the weight of the font.
- Italic - [Boolean] Specifies whether the font is an italic font.
- Underline - [Boolean] Specifies whether the font is an underlined font.
- StrikeOut - [Boolean] Specifies whether the font is a strikeout font.
- CharSet - [Constant] Specifies the character set.
- OutPrecision - [Constant] Specifies the output precision.
- ClipPrecision - [Constant] Specifies the clipping precision.
- Quality - [Constant] Specifies the output quality.
- PitchAndFamily - [Constant] Specifies the pitch and family of the font.
- FaceName - [String] Specifies the typeface name of the font.
For more information about each parameter and their constants, click
here.
Functions:
- Update() - Updates the LOGFONT and HFONT based on the font properties.
*** Remember to always use Update() after you edit a font's properties! ***
- Set(PlusWnd, ControlId) - Sets the font of ControlId of PlusWnd.
- Get(PlusWnd, ControlId) - Gets the font of ControlId of PlusWnd.
- Choose(Flags) - Creates a choose font dialog using the specified flags.
- Save() - Return the contents of the LOGFONT as a string (for storing).
- Load(Cache) - Loads a string containing the contents of a LOGFONT.
- toLogical() - Convert point units to logical units. See Notes section for more information.
- toPoint() - Convert logical units to point units. See Notes section for more information.
Examples:
In a nutshell, you can create your
Font object by doing this:
code:
// Create a font
var MyFont = new Font(0, 0, 0, 0, FW_BOLD, false, true, false, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "Comic Sans MS");
This is clearly quite tiresome and hard to follow, so you can also initialise a font and only change the required properties:
code:
// Initialise a font with default properties
var MyFont = new Font();
// Change the required properties
MyFont.Weight = FW_BOLD;
MyFont.Underline = true;
MyFont.CharSet = ANSI_CHARSET;
MyFont.FaceName = "Comic Sans MS";
// Update your font with your new properties
MyFont.Update();
Now to set your font to a window control, use
Set():
code:
// Create your window
var MyWindow = MsgPlus.CreateWnd("MyWindow.xml", "MyWindow");
// Create your font
var MyFont = new Font();
MyFont.Height = 16;
MyFont.FaceName = "Times New Roman";
MyFont.Update();
// Set your font to the control
MyFont.Set(MyWindow, "MyListView");
If you want to get the existing font of a control, use
Get():
code:
// Create your window
var MyWindow = MsgPlus.CreateWnd("MyWindow.xml", "MyWindow");
// Create your font
var MyFont = new Font();
// Get the font of the control
MyFont.Get(MyWindow, "MyListView");
Debug.Trace("Font: " + MyFont.FontFace);
An alternative to setting and getting the font of a control is to use the
WM_SETFONT and
WM_GETFONT messages with the font's handle (either
Font.Handle or
Font.HFONT):
code:
// Create your window
var MyWindow = MsgPlus.CreateWnd("MyWindow.xml", "MyWindow");
// Create your font
var MyFont = new Font();
MyFont.Height = 16;
MyFont.FaceName = "Times New Roman";
MyFont.Update();
// Set your font to the control
MyWindow.SendControlMessage("MyListView", WM_SETFONT, MyFont.Handle, 0);
In version 1.1, the
Choose() function has been added. This will prompt the user for a font with the choose font dialog box, and update the
Font object when the OK button has been pressed. For more information about each flag, click
here.
code:
// Create your font
var MyFont = new Font();
// Create the choose font dialog box and prompt the user
MyFont.Choose(CF_SCREENFONTS | CF_INITTOLOGFONTSTRUCT | CF_EFFECTS);
// Trace the new font face name
Debug.Trace(MyFont.FaceName);
The
Save() function will produce a string which you can save to a file or to the registry. This string can in turn be loaded with the
Load() function, allowing you to use fonts between threads:
code:
// Create the Windows Script Host
var WshShell = new ActiveXObject("WScript.Shell");
// Save the font data to the registry
WshShell.RegWrite(MsgPlus.ScriptRegPath + "Font", MyFont.Save());
// Load the font data from the registry
MyFont.Load(WshShell.RegRead(MsgPlus.ScriptRegPath + "Font"));
Screenshot:
Notes:
The size of the font is specified with the
Height property. If
Height is 0, then the default size is used (
Width is also 0 for default proportions). However, when
Height is not 0, the font is scaled. It is important to note that
Height takes logical units, which is different to point units commonly seen in word processors.
Therefore, two functions have been added to convert between them:
toLogical() and
toPoint(). Both functions are extensions of the
Number object, and should be used as follows:
code:
// Create your font
var MyFont = new Font();
// Convert a size 12 font to logical units
var Size = 12;
MyFont.Height = Size.toLogical();
Be careful though, the following code is not valid:
code:
12.toLogical();
// You may use this though
(12).toLogical();
The
Font object does not only have the above listed font properties and font handle (
Font.Handle or
Font.HFONT), but a
LOGFONT data structure. These can all be accessed by:
code:
// Create your font
var MyFont = new Font();
// Trace the pointer to the LOGFONT structure (it is a DataBloc object)
Debug.Trace(MyFont.LOGFONT.DataPtr);
// Trace the font's (LOGFONT's) handle (also known has HFONT)
Debug.Trace(MyFont.Handle);
Debug.Trace(MyFont.HFONT);
Font.Handle and
Font.HFONT are identical. The font data that the control uses is actually the data in the
LOGFONT structure. When you update font properties (
Font.Height,
Font.Width, etc.), the
LOGFONT is not updated. That is why
Update() must be called every time, to update not only the
LOGFONT, but the
LOGFONT's handle (
HFONT).
Also, although the choose font dialog box supports colour, setting fonts for controls does not.
Download Font Class now!
(I hope somebody finds this useful after all that typing...
)