C# 判断字体是否存在以及安装
2024-08-29 21:34:55
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")
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UtilityHelper; namespace LHCity_LMS_Client.Test
{
class Program
{
static void Main(string[] args)
{
FontOperate.InstallFont("simfang.ttf", "simfang");
Console.ReadLine();
}
}
}
Using demo
1.2 使用资源文件中的字体
/// <summary>
/// 如何使用资源文件中的字体,无安装无释放
/// </summary>
/// <param name="bytes">资源文件中的字体文件,如Properties.Resources.华文行楷</param>
/// <returns></returns>
public Font GetResoruceFont(byte[] bytes)
{
System.Drawing.Text.PrivateFontCollection pfc = new System.Drawing.Text.PrivateFontCollection();
IntPtr MeAdd = Marshal.AllocHGlobal(bytes.Length);
Marshal.Copy(bytes, , MeAdd, bytes.Length);
pfc.AddMemoryFont(MeAdd, bytes.Length);
return new Font(pfc.Families[], , 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[], , FontStyle.Regular, GraphicsUnit.Point, );//myFont1就是你创建的字体对象
Font myFont2 = new Font(pfc.Families1], , FontStyle.Bold | FontStyle.Regular); //使用字体:
//label1.Font = myFont1;
1.4 软件发布时的包含
当前可以有以下两种方法实现:
(1)通过MSI安装文件实现;
(2) InstallShield 部署软件中设置字体安装。
这两种方式,不再详述,具体请百度。
2 字体是否存在判断
可以参考下面的代码进行实现
List<string> arrStrNames = new List<string>();
InstalledFontCollection MyFont = new InstalledFontCollection();
FontFamily[] fontFamilys = MyFont.Families;
if (fontFamilys == null || fontFamilys.Length < )
{
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, );
if (font.Name != fontName)
{
return false;
} }
catch (Exception ex)
{
return false;
} return true;
}
参考文章
最新文章
- SQLAchemy Core学习之Reflection
- map reduce filter
- SQl 字段中出现某一个词语的次数
- WP开发笔记——字符串 转 MD5 加密
- 深入掌握JMS--转
- C#.NET利用ContextBoundObject和Attribute实现AOP技术--AOP事务实现例子
- Monkey源码分析之事件注入
- 2015最新Android学习线路图
- vueX、vue中transition的使用、axios
- ftp文件共享服务详解
- Crypto++入门学习笔记(DES、AES、RSA、SHA-256)
- OpenTracing:开放式分布式追踪规范
- Linux内核分析 笔记六 进程的描述和进程的创建 ——by王玥
- Office2016无法显示最近使用的文档怎么办
- 启动另一个app
- 7 Django系列之关于bootstrap-table插件的简单使用
- JZ2440 裸机驱动 第13章 LCD控制器(2)
- Hadoop基础-HDFS的API实现增删改查
- vue学习01
- 读书笔记6pandas简单使用
热门文章
- phpcms v9取消验证码
- IOI2002 POJ1054 The Troublesome Frog 讨厌的青蛙 (离散化+剪枝)
- 初次使用git遇到的问题总结
- Codeforces 828C String Reconstruction【并查集巧妙运用】
- Quartz 2D编程指南(2) - 图形上下文
- Sharepoint Web.config trust level=";Full";权限说明
- curl 错误 [globbing] illegal character in range specification at pos
- drill 表&;&;视图使用
- C#中的依赖注入那些事儿
- 使用cookie纪录访问次数