Install the font program or directly call a non-registered font [C #]

Source: Internet
Author: User

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;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.