Comment incorporer une police à mon application C #? (à l’aide de Visual Studio 2005)

Quel est le meilleur moyen d’intégrer une police TrueType dans l’application que je développe? En gros, je veux m’assurer qu’une police particulière est disponible pour mon application lorsqu’elle est installée sur une autre machine. J’ai le fichier de police * .ttf et j’ai juste besoin d’un moyen de l’intégrer ou de l’installer automatiquement lorsque l’application est exécutée.

Dois-je configurer le programme d’installation pour installer la police lors de l’installation ou puis-je charger dynamicment la police lors de l’exécution de l’application? En fait, ce serait bien de savoir les deux.

L’application est en cours de développement en C # à l’aide de .NET 2.0.

Ce blog devrait vous aider.

En gros, vous ajoutez la police en tant que ressource intégrée, puis vous la chargez dans un object PrivateFontCollection .

Voici la réponse de Will traduite en C # (non testée):

PrivateFontCollection pfc = new PrivateFontCollection(); using (Stream fontStream = GetType().Assembly.GetManifestResourceStream("Alphd___.ttf")) { if (null == fontStream) { return; } int fontStreamLength = (int) fontStream.Length; IntPtr data = Marshal.AllocCoTaskMem(fontStreamLength); byte[] fontData = new byte[fontStreamLength]; fontStream.Read(fontData, 0, fontStreamLength); Marshal.Copy(fontData, 0, data, fontStreamLength); pfc.AddMemoryFont(data, fontStreamLength); Marshal.FreeCoTaskMem(data); } 

avec leur méthode Paint ():

 protected void Form1_Paint(object sender, PaintEventArgs e) { bool bold = false; bool italic = false; e.Graphics.PageUnit = GraphicsUnit.Point; using (SolidBrush b = new SolidBrush(Color.Black)) { int y = 5; foreach (FontFamily fontFamily in pfc.Families) { if (fontFamily.IsStyleAvailable(FontStyle.Regular)) { using (Font font = new Font(fontFamily, 32, FontStyle.Regular)) { e.Graphics.DrawSsortingng(font.Name, font, b, 5, y, SsortingngFormat.GenericTypographic); } y += 40; } if (fontFamily.IsStyleAvailable(FontStyle.Bold)) { bold = true; using (Font font = new Font(fontFamily, 32, FontStyle.Bold)) { e.Graphics.DrawSsortingng(font.Name, font, b, 5, y, SsortingngFormat.GenericTypographic); } y += 40; } if (fontFamily.IsStyleAvailable(FontStyle.Italic)) { italic = true; using (Font font = new Font(fontFamily, 32, FontStyle.Italic)) { e.Graphics.DrawSsortingng(font.Name, font, b, 5, y, SsortingngFormat.GenericTypographic); } y += 40; } if(bold && italic) { using(Font font = new Font(fontFamily, 32, FontStyle.Bold | FontStyle.Italic)) { e.Graphics.DrawSsortingng(font.Name, font, b, 5, y, SsortingngFormat.GenericTypographic); } y += 40; } using (Font font = new Font(fontFamily, 32, FontStyle.Underline)) { e.Graphics.DrawSsortingng(font.Name, font, b, 5, y, SsortingngFormat.GenericTypographic); y += 40; } using (Font font = new Font(fontFamily, 32, FontStyle.Ssortingkeout)) { e.Graphics.DrawSsortingng(font.Name, font, b, 5, y, SsortingngFormat.GenericTypographic); } } } } 

C’est plus facile que cela semble; vous pouvez incorporer la police en tant que ressource dans votre application et y accéder en tant que propriété fortement typée dans l’espace de noms Propriétés de votre application. Mais le lien donné devrait être un bon sharepoint départ.


Pour les personnes à mobilité réduite:

Ajoutez la police en tant que ressource dans votre application. Appelez la ressource MyFontLol. Vous pouvez accéder à cette ressource (sous forme de tableau d’octets) à partir de Properties.Resources.MyFontLol.

Je n’ai pas testé les éléments suivants, mais cela semble être réalisable:

 public void LoadMyFontLolKThx() { // get our font and wrap it in a memory stream byte[] myFont = Properties.Resources.MyFontLol; using (var ms = new MemoryStream(myFont)) { // used to store our font and make it available in our app PrivateFontCollection pfc = new PrivateFontCollection(); // The next call requires a pointer to our memory font // I'm doing it this way; not sure what best practice is GCHandle handle = GCHandle.Alloc(ms, GCHandleType.Pinned); // If Length > int.MaxValue this will throw checked { pfc.AddMemoryFont( handle.AddrOfPinnedObject(), (int)ms.Length); } var font = new Font(pfc.Families[0],12); // use your font here } } 

Une dernière note. Le PFC stocke la police en tant que police GDI +. Ceux-ci ne sont pas compatibles avec certains contrôles de formulaires. De la docs:

Pour utiliser la police de mémoire, le texte d’un contrôle doit être rendu avec GDI +. Utilisez la méthode SetCompatibleTextRenderingDefault , en transmettant la valeur true, pour définir le rendu GDI + sur l’application ou des contrôles individuels en définissant la propriété UseCompatibleTextRendering du contrôle sur true. Certains contrôles ne peuvent pas être rendus avec GDI +.

ce n’est peut-être pas la meilleure solution, mais vous ne pouvez pas simplement inclure la police dans vos ressources, puis la copier dans le dossier de la police dans le répertoire windows?