What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Messenger Plus! for Live Messenger » Scripting » [Resource] Font Class (Updated 24/09/07)

[Resource] Font Class (Updated 24/09/07)
Author: Message:
phalanxii
Full Member
***


Posts: 146
Reputation: 5
32 / Male / Flag
Joined: Aug 2006
Status: Away
O.P. [Resource] Font Class (Updated 24/09/07)
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. :D)


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:

[Image: fontscreenzg8.png]


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... :P)

.zip File Attachment: _Font11.zip (3.4 KB)
This file has been downloaded 257 time(s).

This post was edited on 09-24-2007 at 10:13 AM by phalanxii.
09-23-2007 08:26 AM
Profile PM Find Quote Report
NanaFreak
Scripting Contest Winner
*****


Posts: 1476
Reputation: 53
32 / Male / Flag
Joined: Jul 2006
RE: [Resource] Font Class
nice work dude

10/10 ;o

this might actually make my windows look better =p
09-23-2007 08:49 AM
Profile PM Find Quote Report
Matti
Elite Member
*****

Avatar
Script Developer and Helper

Posts: 1646
Reputation: 39
31 / Male / Flag
Joined: Apr 2004
RE: [Resource] Font Class
Awesome work, phalanxii! I don't see a good use for it in one of my scripts at the moment, but I'm sure this class will save my or someone else's day once! :)

Great work!!! (y)

Hmm, maybe I should start writing classes for public use too... :P

This post was edited on 09-23-2007 at 10:09 AM by Matti.
Plus! Script Developer | Plus! Beta Tester | Creator of Countdown Live | Co-developer of Screenshot Sender 5

Found my post useful? Rate me!
09-23-2007 10:08 AM
Profile E-Mail PM Web Find Quote Report
Volv
Skinning Contest Winner
*****

Avatar

Posts: 1233
Reputation: 31
34 / Male / Flag
Joined: Oct 2004
RE: [Resource] Font Class
This is definitely useful, I don't suppose you could extend it to changing the font of some of the control's text (eg. in a RichEdit) rather than the entire control though could you? :D
09-23-2007 11:28 AM
Profile PM Find Quote Report
phalanxii
Full Member
***


Posts: 146
Reputation: 5
32 / Male / Flag
Joined: Aug 2006
Status: Away
O.P. RE: [Resource] Font Class (Updated 24/09/07)
To my knowledge, this is only possible with RichEditControls, and Plus! already has an in-built function to do that. *-)

Also, I believe this class doesn't work for controls that already support fonts (ButtonControls, RadioControls, RichEditControls and RichStaticControls).

In other news, I updated the class because I left out one of the most useful functions: a choose font dialog box! There are also two new number functions to convert between logical units (used in the LOGFONT structure) and point units (commonly seen in word processors).
09-24-2007 10:10 AM
Profile PM Find Quote Report
SmokingCookie
Senior Member
****

Avatar

Posts: 815
Reputation: 15
30 / Male / Flag
Joined: Jul 2007
RE: [Resource] Font Class (Updated 24/09/07)
It may be a bit late, but how about using colours in the font?
09-11-2008 05:59 PM
Profile PM Find Quote Report
Matti
Elite Member
*****

Avatar
Script Developer and Helper

Posts: 1646
Reputation: 39
31 / Male / Flag
Joined: Apr 2004
RE: [Resource] Font Class (Updated 24/09/07)
quote:
Originally posted by SmokingCookie
It may be a bit late, but how about using colours in the font?
You need a few changes for that, but it's quite simple. ;)
Note that this ONLY applies when you're about to use the Choose Font dialog through the Choose() dialog. The LOGFONT itself doesn't have a COLORREF entry, but the CHOOSEFONT structure does.

In the constructor function of Font, add the ColorRef parameter to the end, so you get:
code:
function Font(Height, Width, Escapement, Orientation, Weight, Italic, Underline, StrikeOut, CharSet, OutPrecision, ClipPrecision, Quality, PitchAndFamily, FaceName, ColorRef) {
Then, between "this.FaceName =" and "this.LOGFONT =", add this line:
code:
this.ColorRef = typeof(ColorRef) == "undefined" ? 0x0 : ColorRef;
Now, in the Choose method, make the following modifications. The added lines are in red.
code:
...
this.CHOOSEFONT.WriteDWORD(20, Flags);
this.CHOOSEFONT.WriteDWORD(24, this.ColorRef);
...
if(Interop.Call("comdlg32.dll", "ChooseFontW", this.CHOOSEFONT.DataPtr)) {
   this.Height = this.CHOOSEFONT.ReadDWORD(16).toLogical() / 10;
   this.ColorRef = this.CHOOSEFONT.ReadDWORD(24);
   this.UpdateHFONT();
...
And that's it! You can now use Font.ColorRef to get or set the new color in 0x00bbggrr format, or add it as last parameter for the Font class constructor. When calling Choose(), you'll receive the selected color in Font.ColorRef and, when you're using the CF_INITTOLOGFONTSTRUCT flag, the color will be set in the Choose Font dialog according to the ColorRef property. :)

This doesn't include support for Load(Cache) and Save(), but I think you can figure that out yourself if you'd need it. ;)

(It's quite funny, as I were doing this exact thing myself before you asked. I noticed you reading this topic and thought: "Hey, a font class! Could come handy.")

Besides, awesome class! Really helpful! :D

This post was edited on 09-11-2008 at 06:33 PM by Matti.
Plus! Script Developer | Plus! Beta Tester | Creator of Countdown Live | Co-developer of Screenshot Sender 5

Found my post useful? Rate me!
09-11-2008 06:31 PM
Profile E-Mail PM Web Find Quote Report
SmokingCookie
Senior Member
****

Avatar

Posts: 815
Reputation: 15
30 / Male / Flag
Joined: Jul 2007
RE: RE: [Resource] Font Class (Updated 24/09/07)
quote:
Originally posted by Mattike[size=1
(It's quite funny, as I were doing this exact thing myself before you asked. I noticed you reading this topic and thought: "Hey, a font class! Could come handy.")[/size]

Hehe :P
Well, I've added the ColourRef thingy, called Choose(), but I get this warning:

[Image: weirdowarningiq1.th.jpg]

Translated: "No fonts are installed. Go to Fonts in the Control panel to install fonts".

Anyone?
09-12-2008 06:11 AM
Profile PM Find Quote Report
« Next Oldest Return to Top Next Newest »


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On