Хорошо, так что после тонны SO и Google, основываясь на полученных ранее комментариях, я смог встроить шрифт в свой код. Теперь при запуске мое приложение проверяет, установлен ли шрифт на компьютере. Если шрифт не установлен, мое приложение устанавливает его.
Этот способ избавляет от необходимости вручную проверять, установлен ли шрифт на каждом компьютере, на котором будет запускаться приложение.
Примечание: для работы требуются права администратора.
Первый: встроить файл шрифта в качестве ресурса
- Дважды щелкните Resources.resx, и на панели инструментов дизайнера нажмите Добавить ресурс / Добавить существующий файл и выберите файл .ttf
- В обозревателе решений щелкните правой кнопкой мыши свой файл .ttf и перейдите в Свойства. Установите для параметра «Действие построения» значение «Содержимое», а для свойства «Копировать в выходной каталог» - «Всегда копировать»
Второе: Добавить этот код
using Microsoft.Win32;
using System;
using System.Drawing.Text;
using System.IO;
using System.Runtime.InteropServices;
namespace TestAutomation
{
public partial class SplashScreen : Form
{
[DllImport("gdi32.dll", EntryPoint = "AddFontResource")]
public static extern int AddFontResource(string lpFileName);
[DllImport("gdi32.dll")]
private static extern int CreateScalableFontResource(uint fdwHidden, string
lpszFontRes, string lpszFontFile, string lpszCurrentPath);
// <summary>
// Installs font on the user's system and adds it to the registry so it's available on the next session
// Your font must be embedded as a resource in your project with its 'Build Action' property set to 'Content'
// and its 'Copy To Output Directory' property set to 'Copy Always'
// </summary>
private void RegisterFont(string contentFontName)
{
DirectoryInfo dirWindowsFolder = Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.System));
// Concatenate Fonts folder onto Windows folder.
string strFontsFolder = Path.Combine(dirWindowsFolder.FullName, "Fonts");
// Creates the full path where your font will be installed
var fontDestination = Path.Combine(strFontsFolder, contentFontName);
// Check if file exists in destination folder. If not, then copy the file from project directory to destination
if (!File.Exists(fontDestination))
{
try
{
// Copies font to destination
File.Copy(Path.Combine(Directory.GetCurrentDirectory(), contentFontName), fontDestination);
// Retrieves font name
PrivateFontCollection fontCol = new PrivateFontCollection();
fontCol.AddFontFile(fontDestination);
var actualFontName = fontCol.Families[0].Name;
// Add font
AddFontResource(fontDestination);
// Add registry entry
Registry.SetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts",
actualFontName, contentFontName, RegistryValueKind.String);
}
catch (Exception e)
{
MessageBox.Show(e.Message + "\n\nThe required font(s) have not been installed."
+ "\n\nPlease contact your systems administrator for help.");
Start();
}
}
// If file exists in destination folder, then start program.
else
{
Start();
}
}
public SplashScreen()
{
RegisterFont("GOTHIC.TTF");
}
private void Start()
{
InitializeComponent();
}
}
}
Я изменил код в соответствии со своими потребностями. Вот ссылки, где я нашел мою информацию:
Как быстро и легко встроить шрифты в приложение winforms в C #
https://csharp.hotexamples.com/examples/System.Drawing.Text/PrivateFontCollection/AddFontFile/php-privatefontcollection-addfontfile-method-examples.html