Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
252 views
in Technique[技术] by (71.8m points)

c# - How to programmatically install a font

I would like to install a specific font on my program load and use that font in rendering text of the program. How can I programmatically install a font from .NET CF on WinCE 6.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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('');
        Debug.WriteLine(fontName);
        m_fonts.Add(fontName);

        return 1;
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...