C# 判断字体是否存在以及安装 | 您所在的位置:网站首页 › 方方正正的字体 › C# 判断字体是否存在以及安装 |
1. 字体安装
在实际开发项目中,需要在客户端安装字体,一种是通过代码将字体文件复制到系统FONT目录即可,另一种通过安装文件实现,至于其他方式还未知晓。 1.1 软安装 public class FontOperate { [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, // message int wParam, // first message parameter int lParam // second message parameter ); [DllImport("gdi32")] public static extern int AddFontResource(string lpFileName); public static bool InstallFont(string sFontFileName, string sFontName) { string _sTargetFontPath = string.Format(@"{0}\fonts\{1}", System.Environment.GetEnvironmentVariable("WINDIR"), sFontFileName);//系统FONT目录 string _sResourceFontPath = string.Format(@"{0}\Font\{1}", System.Windows.Forms.Application.StartupPath, sFontFileName);//需要安装的FONT目录 int Res; const int WM_FONTCHANGE = 0x001D; const int HWND_BROADCAST = 0xffff; try { if (!File.Exists(_sTargetFontPath) && File.Exists(_sResourceFontPath)) { int _nRet; File.Copy(_sResourceFontPath, _sTargetFontPath); _nRet = AddFontResource(_sTargetFontPath); Res = SendMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0); _nRet = WriteProfileString("fonts", sFontName + "(TrueType)", sFontFileName); } } catch { return false; } return true; } }
函数的使用: fonts.installFont(字体文件, 字体名称)//fonts类名fonts.installFont("C39P36DmTt.TTF", "C39P36DmTt") ![]() ![]() 1.2 使用资源文件中的字体 /// /// 如何使用资源文件中的字体,无安装无释放 /// /// 资源文件中的字体文件,如Properties.Resources.华文行楷 /// public Font GetResoruceFont(byte[] bytes) { System.Drawing.Text.PrivateFontCollection pfc = new System.Drawing.Text.PrivateFontCollection(); IntPtr MeAdd = Marshal.AllocHGlobal(bytes.Length); Marshal.Copy(bytes, 0, MeAdd, bytes.Length); pfc.AddMemoryFont(MeAdd, bytes.Length); return new Font(pfc.Families[0], 15, FontStyle.Regular); } Demo2 //程序直接调用字体文件,不用安装到系统字库中。 //设置字体对象: String ls_appPath = System.Windows.Forms.Application.StartupPath + "\\font\\";//font是程序目录下放字体的文件夹 String fontFile1 = ls_appPath + "C39P36DmTt.TTF"; String fontFile2 = ls_appPath + "GWGLYPTT.TTF"; ...... pfc.AddFontFile(fontFile1);//字体文件的路径 pfc.AddFontFile(fontFile2);//字体文件的路径 ........ Font myFont1 = new Font(pfc.Families[0], 41, FontStyle.Regular, GraphicsUnit.Point, 0);//myFont1就是你创建的字体对象 Font myFont2 = new Font(pfc.Families1], 31, FontStyle.Bold | FontStyle.Regular); //使用字体: //label1.Font = myFont1;1.4 软件发布时的包含 当前可以有以下两种方法实现: (1)通过MSI安装文件实现; (2) InstallShield 部署软件中设置字体安装。 这两种方式,不再详述,具体请百度。 2 字体是否存在判断可以参考下面的代码进行实现 List arrStrNames = new List(); InstalledFontCollection MyFont = new InstalledFontCollection(); FontFamily[] fontFamilys = MyFont.Families; if (fontFamilys == null || fontFamilys.Length < 1) { return null; } foreach (FontFamily item in fontFamilys) { arrStrNames.Add(item.Name); } return arrStrNames;算了,我还是补充上来吧。今天晚上就给字体叫上劲了。 public static bool CheckSysFontExisting(string fontName = "文鼎細黑") { Font font; try { font = new Font(fontName, 10); if (font.Name != fontName) { return false; } } catch (Exception ex) { return false; } return true; }
参考文章 |
CopyRight 2018-2019 实验室设备网 版权所有 |