0

How can I change the font of my project in a way that anyone who downloads the app sees it with the font that I choose?

I'm using C#, Microsoft Visual Studio Code, Winforms.

I keep searching but all that I can find is a tutorial on how to install the font on my own computer.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 1
    Does the font is standard or very specific to your OS? – Maciej Los May 28 '23 at 16:49
  • 1
    https://learn.microsoft.com/en-us/dotnet/api/system.drawing.text.privatefontcollection?view=dotnet-plat-ext-7.0 – Hans Passant May 28 '23 at 17:22
  • 1
    [How to properly render an embedded Font?](https://stackoverflow.com/a/64512339/7444103) -- [PrivateFontCollection & / Or Marshal.AllocCoTaskMem()](https://stackoverflow.com/a/57231407/7444103) – Jimi May 28 '23 at 19:15

2 Answers2

0

If you want to change the font of all controls in a form (.NET Framework). See this link.

But, if you are using .NET Core (.NET 6 or later) you can:

In project file inside <PropertyGroup> add:

<ApplicationDefaultFont>Curlz MT, 18pt</ApplicationDefaultFont>

You can also call Application.SetDefaultFont() directly in your Main() method as shown here:

static void Main()
{
    ApplicationConfiguration.Initialize();

    Application.SetDefaultFont(new Font(new FontFamily("Curlz MT"), 18f));

    Application.Run(new AirQualityForm());
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Ibrahim Hamaty
  • 540
  • 2
  • 18
  • But that still requires that font to be installed for all other users (computers) – Hans Kesting May 28 '23 at 18:23
  • 1
    If he wants to Add a Font programmatically, there is posts with this question. [See this](https://stackoverflow.com/questions/21986744/how-to-install-a-font-programmatically-c) – Ibrahim Hamaty May 28 '23 at 18:47
0

Here is an example of a custom font working.

(Important: In this example I dragged "FiraCode-SemiBold.ttf" into the root of my project, then I right clicked it in visual studio and set the property: "Copy to Output Directory" to "Copy if newer" (copy always will work too))

[STAThread]
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);

    var pfc = new PrivateFontCollection();
    pfc.AddFontFile("FiraCode-SemiBold.ttf");
    FontFamily fontFamily = pfc.Families[0];
    float fontSize = 8.25f;
    FontStyle fontStyle = FontStyle.Regular;
    GraphicsUnit graphicsUnit = GraphicsUnit.Point;
    byte gdiCharSet = 1;

    Application.VisualStyleState = System.Windows.Forms.VisualStyles.VisualStyleState.NoneEnabled;
    Application.SetDefaultFont(new Font(fontFamily, fontSize, fontStyle, graphicsUnit, gdiCharSet));

    Application.Run(new Form1());
}
Dylan
  • 1,071
  • 1
  • 12
  • 24