This is a short primer on how to use fonts in the Microsoft® .NET Micro Framework. In this article, I’ll show you how to add a font to a project that can be used in the Windows® Presentation Foundation (WPF), which is a desktop application development framework.
First you’ll need to create a new .NET Micro Framework project in Microsoft Visual Studio®, using the following steps:
- On the File menu, point to New and then click Project.
- Under Project types, expand the Visual C# node, and then click Micro Framework.
- Under Templates, click Window Application and then click OK.
Now let’s add a font to our new project. You’ll notice that under the Resources node in Solution Explorer there’s already a file named small.tinyfnt, as shown in the following illustration.
http://blogs.msdn.com/photos/aldenl/images/1882313/original.aspx
Next, browse to and open the program.cs file so that we can add some text to it that uses our new font. In program.cs, change line 39 from the following:
text.Font = Resources.GetFont(Resources.FontResources.small);
to this:
text.Font = Resources.GetFont(Resources.FontResources.NinaB);
Behind the scenes, the .NET Micro Framework uses an index number to search for resources in the system. This is unlike the .NET Framework on the desktop, where you can use strings to search for resources. For example, the desktop accesses fonts by using the following code:
Font ft = new Font("Arial", 30);
What’s important here is that the .NET Micro Framework reduced the code size by using an index number rather than by using a bulky string for each resource and by removing the ability to specify a font size. Notice that the Resources.Designer.cs file has an index number appended to the enumeration, as shown in the following code snippet:
internal enum FontResources : short
{
small = 13070,
NinaB = 18060,
}
As you can see, this enumeration is what was used in the earlier Resources.GetFont() call to access the resource.
The other basic difference between the two frameworks is that with the desktop, you can define the size of the fonts you use, whereas with the .NET Micro Framework, fonts have a predetermined size and are displayed only in that size.
These .tinyfnt files are a new font format used by the .NET Micro Framework. The .NET Micro Framework SDK contains additional prebuilt .tinyfnt files, so now we’ll go ahead and add a reference to one of the other .tinyfnt files, using the following steps:
1. Double-click Resources.resx in Solution Explorer.
2. On the Resources.resx tab, click the down arrow to the right of Add Resources, and then click Add Existing File on the drop-down menu, as shown in the following illustration.
http://blogs.msdn.com/photos/aldenl/images/1882317/original.aspx
3. In the Add existing file to resources dialog box, select All Files in the Files of type box.
4. While still in the dialog box, browse to \\Program Files\Microsoft .NET Micro Framework\vX.X.XXXX\Fonts, where X.X.XXXX is the version number of your .NET Micro Framework SDK.
5. In the Fonts folder, double-click the file NinaB.tinyfnt.
6. Finally, build the solution by clicking Rebuilt Solution on the Build menu.
And that’s all there is to it. You now have another font you can use in your .NET Micro Framework projects.
Note: There are only two .tinyfnt files currently available in the .NET Micro Framework, and at present there’s no way to create or add custom fonts. This is something that is being considered for the next release of the framework.