开发者

How to programmatically install a font

开发者 https://www.devze.com 2023-01-05 00:28 出处:网络
I would like to install a specific font on my program load and use that font in rendering text of the program. How c开发者_如何学JAVAan I programmatically install a font from .NET CF on WinCE 6.This b

I would like to install a specific font on my program load and use that font in rendering text of the program. How c开发者_如何学JAVAan I programmatically install a font from .NET CF on WinCE 6.


This blog entry shows how to enumerate and add Fonts in Windows CE using native code. For managed code, this will work:

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;
    }
}


Copy font *.ttf file to Windows\Fonts folder, it may requires restarting your device.


Actually, this helps better.

http://social.msdn.microsoft.com/Forums/en/netfxcompact/thread/ee9a6947-0799-4a76-a7cd-c0bec4b7a2ab

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号