試驗平台:.Net Micro Framework 模拟器
微軟示例程式中,僅支援兩種字型(small.tinyfnt和NinaB.tinyfnt),并不支援中文。
MF平台支援的字型是專有格式,擴充名為tinyfnt,需要用專門的轉化工具才能把windows平台上的字型轉換為tinyfnt字型。
轉換工具在MF SDK開發包中就有,安裝後的位置為:C:/Program Files/Microsoft .NET Micro Framework/v2.0.3036/Tools/TFConvert.exe
要轉換字型,需要三個條件:第一,轉換工具,這個我們已經有了;第二,中文字庫,直接在C:/WINDOWS/Fonts目錄中選一個你需要轉換的字型即可;第三,轉換用的配置檔案(擴充名為fntdef的檔案),這個比較麻煩,我着重說一下。
配置檔案中常用的指令如下:
1、AddFontToProcess
格式:AddFontToProcess path
說明:填寫中文字庫的路徑資訊(字庫源的路徑,需要輸入絕對路徑),如果路徑裡有空格,一定用引号括起來,如示例所示:
格式:SelectFont "selectionstring"
說明:參數比較多,常用的就下面幾個,餘下的請看msdn的相關文章。
WE(Weight)字型的厚重度,0~1000,标準的是400,粗體為700
FN(Face Name) 字型名稱,如果包含空格,要用引号括起來
HE(Height)字型大小(實際測試範圍1~36)
WI(Width)字型寬度(實際測試範圍1~12)
IT(Italic)設定字型的傾斜角度
UN(Underline)設定字型下面的下劃線
格式:ImportRange start end
說明:這個比較重要,是指從字庫中導出字型的起始和結束位置,可以多個指令連用。
詳細的介紹在msdn上有相關說明(很可惜是英文的):ms-help://MS.VSCC.v80/MS.VSIPCC.v80/MSVS.PSDK/PSDK/PSDK_TF_Fntdef.htm
配置檔案内容示例:
AddFontToProcess C:/SampleFonts/MSYH.ttf
SelectFont "WE:400,FN:宋體"
ImportRange 19968 40869
注意上面的示例檔案僅導出了常用的漢字,如果你用該字型顯示中英文混合的字元串,你會發現其它字型全部是空格,這顯然不是我們想要的結果,是以我們導出的字型不僅有中文、英文字元,還要有必要的标點符号,是以我們要用多個ImportRange 指令。
這是我寫的配置檔案,我導出字型為華文細黑字型。
AddFontToProcess D:/SELF/MF/china/STXIHEI.TTF
SelectFont "WE:400,FN:宋體"
#英文字母和常用符号
ImportRange 32 126
#羅馬數字
ImportRange 8544 8569
#各個方向的箭頭
ImportRange 8592 8601
#1~10 圓圈内嵌數字
ImportRange 9312 9321
#有用的符号
ImportRange 9600 9835
#漢字
ImportRange 19968 40869
導出的大小為:695K,原字型大小9540K,不過與英文字庫相比還是大的多。
如果在MF上的應用漢字很少,可以僅導出使用的字元。從這點出發,似乎根據使用的字元串,自動導出生成相應的字型庫的程式又有了用武之地(以前我們在西文DOS下,顯示中文字型就這麼做過)。
說做就做,下面就是我完成的程式(原理很簡單,根據區位碼導出字元)。
(圖MF071101001.jpg)
我們隻轉換“[葉帆工作室]歡迎您”這幾個字,最後我們轉換的字庫僅456個位元組,與695K相比那差好多數量級,是不是很棒?!如果我們把上面的程式改進一下,也許可以自動根據代碼建立相應的字庫檔案了,那就更棒了。
下面就把我們的字庫加入到程式,用模拟器試一下,看看是否能正确顯示漢字。
(圖MF071101002.jpg)
是不是很棒J
相關代碼如下(記得先在資源中添加我們轉換後的字庫檔案):
using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Input;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
namespace HZTest
{
public class Program : Microsoft.SPOT.Application
{
public static void Main()
{
Program myApplication = new Program();
Window mainWindow = myApplication.CreateWindow();
// Create the object that configures the GPIO pins to buttons.
GPIOButtonInputProvider inputProvider = new GPIOButtonInputProvider(null);
// Start the application
myApplication.Run(mainWindow);
}
private Window mainWindow;
public Window CreateWindow()
// Create a window object and set its size to the
// size of the display.
mainWindow = new Window();
mainWindow.Height = SystemMetrics.ScreenHeight;
mainWindow.Width = SystemMetrics.ScreenWidth;
// Create a single text control.
Text text = new Text();
//設定字型 yf.tinyfnt
text.Font = Resources.GetFont(Resources.FontResources.yf );
text.TextContent = "[葉帆工作室]歡迎您";
text.HorizontalAlignment = Microsoft.SPOT.Presentation.HorizontalAlignment.Center;
text.VerticalAlignment = Microsoft.SPOT.Presentation.VerticalAlignment.Center;
// Add the text control to the window.
mainWindow.Child = text;
// Connect the button handler to all of the buttons.
mainWindow.AddHandler(Buttons.ButtonUpEvent, new ButtonEventHandler(OnButtonUp), false);
// Set the window visibility to visible.
mainWindow.Visibility = Visibility.Visible;
// Attach the button focus to the window.
Buttons.Focus(mainWindow);
return mainWindow;
private void OnButtonUp(object sender, ButtonEventArgs e)
// Print the button code to the Visual Studio output window.
Debug.Print(e.Button.ToString());
}
}
文字部分就先介紹到這裡,下面的幾篇文章就該介紹一下圖形方面的内容了,等圖形掌握了,相信最後我們一定能在MF平台上實作一個很棒的應用。