2010-06-23 7 views
3

Je voudrais installer une police spécifique sur ma charge de programme et utiliser cette police dans le rendu du texte du programme. Comment puis-je installer par programme une police à partir de .NET CF sur WinCE 6.Comment installer une police par programme

Répondre

0

Copier la police * .ttf dans le dossier Windows \ Fonts, cela peut nécessiter de redémarrer votre appareil.

+0

Je ne peux pas redémarrer l'appareil, il rétablit à l'état d'origine lors du redémarrage. Donc, je dois installer la police à chaque lancement de mon programme. Et en plus, il n'y a pas de dossier Windows \ Font. Un autre moyen? – VOX

2

This blog entry montre comment énumérer et ajouter des polices dans Windows CE en utilisant du code natif. Pour le code managé, cela fonctionnera:

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

c'est un super article. merci ctacke. – VOX

+0

pouvez-vous s'il vous plaît veuillez traduire ce code C++ en C# un? – VOX

Questions connexes