1. Install the font
//ProgramDirectly install the font file in the system.
Function declaration:
[Dllimport ("kernel32.dll", setlasterror = true)]
Static extern int writeprofilestring (string lpszsection, string lpszkeyname, string lpszstring );
[Dllimport ("user32.dll")]
Public static extern int sendmessage (INT hwnd, // handle to destination window
Uint MSG,
Int wparam,
Int lparam
);
[Dllimport ("GDI32")]
Public static extern int addfontresource (string lpfilename );
Public static bool installfont (string fontfilename, string fontname)
{
String winfontdir = system. environment. getenvironmentvariable ("WINDIR") + "\ fonts ";
// String fontfilename = "ds-digital bold italic. TTF ";
// String fontname = "ds-digital bold italic ";
Int ret;
Int res;
String fontpath;
// Const int wm_fontchange = 0x001d;
// Const int hwnd_broadcast = 0 xFFFF;
Fontpath = winfontdir + "\" + fontfilename;
Try
{
If (! File. exists (fontpath ))
{
File. Copy (system. Windows. Forms. application. startuppath +" \ Font \\ "+ Fontfilename, fontpath );// Font is the folder with the font of the program directory.
Ret = addfontresource (fontpath );
// Res = sendmessage (hwnd_broadcast, wm_fontchange, 0, 0 );
// An error occurs during compilation in Windows 7. Just comment.
Ret = writeprofilestring ("fonts", fontname + "(TrueType)", fontfilename );
}
} Catch (exception ex)
{
MessageBox. Show ("[" + fontname + "] font installation failed! Cause: "+ ex. Message );
Return false;
}
Return true;
}
Function usage:
Fonts . Installfont (font file, font name) // Fonts Class Name
Fonts . Installfont ("C39p36dmtt. TTF","C39p36dmtt")
2. Call the font file
// The Program directly calls the font file and does not need to be installed in the system font.
Set the font object:
String ls_apppath = system. Windows. Forms. application. startuppath + "\\Font \ "; // Font is the folder in which fonts are placed in the program directory.
String fontfile1 = ls_apppath +" C39p36dmtt. TTF ";
String fontfile2 = ls_apppath +"Gwglyptt. TTF ";
......
PFC. addfontfile (fontfile1 );// Path of the font file
PFC. addfontfile (fontfile2 );/ /Path of the font file
........
font myfont1 = new font (PFC. families [0], 41, fontstyle. regular, graphicsunit. point, 0); // myfont1 is the font object you created
font myfont2 = new font (PFC. families1], 31, fontstyle. bold | fontstyle. regular);
Font:
Label1.Font = myfont1;