1
I have some fonts that are not standard Windows, so I’m creating a PrivateFontCollection
but I’m having a hard time.
These sources are used by the entire program, so I thought I’d create a Classe Static
with a method get
, but how could add the sources to the PrivateFontCollection
? I know we use the method AddFontFile
so I thought I’d add the fonts right after the user login, but I’m not getting it.
Class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing.Text;
using System.Runtime.InteropServices;
using DevExpress.CodeParser;
namespace Onee.Classes.Font
{
public static class Fonts
{
private static PrivateFontCollection Font_collection;
Font_collection.AddFontFile("C:\\Users\\thomas\\Documents\\Workspace\\Onee\\Onee Beta 1.10.16.2\\Onee\\Resources\\C_Light.otf");
Font_collection.AddFontFile("C:\\Users\\thomas\\Documents\\Workspace\\Onee\\Onee Beta 1.10.16.2\\Onee\\Resources\\C_Bold.otf");
public static PrivateFontCollection Collection
{
get { return Font.Fonts.Font_collection; }
}
}
}
Form:
private void set_font()
{
label4.Font = new Font(Classes.Font.Fonts.Collection.Families[1], 12, FontStyle.Bold);
label_usuario.Font = new Font(Classes.Font.Fonts.Collection.Families[1], 12, FontStyle.Bold);
label3.Font = new Font(Classes.Font.Fonts.Collection.Families[1], 12, FontStyle.Bold);
label2.Font = new Font(Classes.Font.Fonts.Collection.Families[1], 12, FontStyle.Bold);
label5.Font = new Font(Classes.Font.Fonts.Collection.Families[1], 12, FontStyle.Bold);
label1.Font = new Font(Classes.Font.Fonts.Collection.Families[1], 12, FontStyle.Bold);
button_servicos.Font = new Font(Classes.Font.Fonts.Collection.Families[0], 15);
button_comercial.Font = new Font(Classes.Font.Fonts.Collection.Families[0], 15);
button_certificados.Font = new Font(Classes.Font.Fonts.Collection.Families[0], 15);
button_equipamentos.Font = new Font(Classes.Font.Fonts.Collection.Families[0], 15);
button_compras.Font = new Font(Classes.Font.Fonts.Collection.Families[0], 15);
simpleButton1.Font = new Font(Classes.Font.Fonts.Collection.Families[0], 15);
simpleButton2.Font = new Font(Classes.Font.Fonts.Collection.Families[0], 15);
button_opsistema.Font = new Font(Classes.Font.Fonts.Collection.Families[0], 15);
}
Error:
I was able to solve it this way:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing.Text;
using System.Runtime.InteropServices;
using DevExpress.CodeParser;
namespace Onee.Classes.Font
{
public static class Fonts
{
private static PrivateFontCollection Font_collection = new PrivateFontCollection();
public static void add_font()
{
Font_collection.AddFontFile("C:\\Users\\thomas\\Documents\\Workspace\\Onee\\Onee Beta 1.10.16.2\\Onee\\Resources\\C_Light.otf");
Font_collection.AddFontFile("C:\\Users\\thomas\\Documents\\Workspace\\Onee\\Onee Beta 1.10.16.2\\Onee\\Resources\\C_Bold.otf");
}
public static PrivateFontCollection Collection
{
get { return Font.Fonts.Font_collection; }
}
}
}
That’s the right approach?
Friend, could you direct me, or explain to me about these absolute paths? I made a revision in the code, directing the path to the application path, using
Directory.GetCurrentDirectory()
– Thomas Erich Pimentel
@Thomaserichpimentel the problem of using an absolute file path is that it makes your program less portable. Imagine that you want to put your new program on another computer. Using the code posted by you it would only work if you own a C:driver, and in this driver there exists a users folder, and in this folder exist the user Thomas, so on... It is much easier to use a directory related to your application.
– Genos