How to create a PrivateFontCollection

1

I have some fonts that are not standard Windows, so I'm creating a PrivateFontCollection but I'm having trouble.

These sources are used by the whole program, so I thought about creating a Classe Static with a get method, but how could I add the sources to PrivateFontCollection ? I know that we use the AddFontFile method so I thought about adding 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:

Iwasabletoresolveitasfollows:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Drawing.Text;usingSystem.Runtime.InteropServices;usingDevExpress.CodeParser;namespaceOnee.Classes.Font{publicstaticclassFonts{privatestaticPrivateFontCollectionFont_collection=newPrivateFontCollection();publicstaticvoidadd_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; }
        }
    }
}

Is this approach true?

    
asked by anonymous 25.10.2016 / 18:42

1 answer

1

Some simple improvements to implement:

  • Use @ before a string to interpret it literally. This helps in cases when there are too many characters to escape. Ex:

    Font_collection.AddFontFile("C:\Meu\Diretorio\Super\Obscuro");
    

    Turns:

    Font_collection.AddFontFile(@"C:\Meu\Diretorio\Super\Obscuro");
    
  • If you need to add the fonts (method add_font() ) only once, consider using a static constructor :

    static Fonts(){                    
        Font_collection = new PrivateFontCollection();
        Font_collection.AddFontFile(@"C:\Fonte\Um");
        Font_collection.AddFontFile(@"C:\Fonte\Dois");
    }
    
  • In your forms it is not necessary to create a new instance of Font for each control! Create one for a desired style and use it in all the controls you want to have this font.

  • Still, some other items of your implementation are questionable (absolute paths to reference a file / resource?), but are very comprehensive and fall outside the scope of this question.

        
    25.10.2016 / 20:33