如何以编程方式安装字体

时间:2010-06-23 19:41:36

标签: c# winapi compact-framework fonts windows-ce

我想在程序加载时安装特定字体,并在渲染程序文本时使用该字体。如何在WinCE 6上以编程方式从.NET CF安装字体。

3 个答案:

答案 0 :(得分:2)

This blog entry显示了如何使用本机代码枚举和添加Windows CE中的字体。对于托管代码,这将起作用:

internal class FontHelper
{
    private delegate int EnumFontFamProc(IntPtr lpelf, IntPtr lpntm, uint FontType, IntPtr lParam);
    private List<string> m_fonts = new List<string>();

    public FontHelper()
    {
        RefreshFontList();
    }

    public void RefreshFontList()
    {
        m_fonts.Clear();

        var dc = GetDC(IntPtr.Zero);
        var d = new EnumFontFamProc(EnumFontCallback);
        var ptr = Marshal.GetFunctionPointerForDelegate(d);
        EnumFontFamilies(dc, null, ptr, IntPtr.Zero);
    }

    public string[] SupportedFonts
    {
        get { return m_fonts.ToArray(); }
    }

    private const int SIZEOF_LOGFONT = 92;
    private const int LOGFONT = 28;
    private const int LF_FACESIZE = 32;
    private const int LF_FULLFACESIZE = 64;

    [DllImport("coredll", SetLastError = true)]
    private static extern IntPtr GetDC(IntPtr hwnd);

    [DllImport("coredll", SetLastError = true)]
    private static extern int EnumFontFamilies(IntPtr hdc, string lpszFamily, IntPtr lpEnumFontFamProc, IntPtr lParam);

    private int EnumFontCallback(IntPtr lpelf, IntPtr lpntm, uint FontType, IntPtr lParam)
    {
        var data = new byte[SIZEOF_LOGFONT + LF_FACESIZE + LF_FULLFACESIZE];

        Marshal.Copy(lpelf, data, 0, data.Length);
        var fontName = Encoding.Unicode.GetString(data, SIZEOF_LOGFONT, LF_FULLFACESIZE).TrimEnd('\0');
        Debug.WriteLine(fontName);
        m_fonts.Add(fontName);

        return 1;
    }
}

答案 1 :(得分:0)

将字体* .ttf文件复制到Windows \ Fonts文件夹,可能需要重新启动设备。

答案 2 :(得分:0)