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")
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;
}
参考文章
C# 判断字体是否存在以及安装的更多相关文章
- js判断本机是否已安装app
需求:在浏览器或者app webview中打开的页面,js判断本机是否已安装搜狐新闻客户端. 一.微信 1.分享——好友/朋友圈,feed会有搜狐新闻标记,打开url后缀参数isappinstalle ...
- js判断ie和edge是否安装Adobe Reader PDF阅读器
ie浏览器和edge浏览器,必须用Adobe Reader PDF阅读器才可以打开pdf文件,其他现代浏览器自带pdf阅读器,无需安装. 判断ie或者edge如果安装了,就浏览pdf文件:如果没安装就 ...
- android 判断应用程序是否已安装
1.判断是否安装/** check the app is installed*/private boolean isAppInstalled(Context context,String packag ...
- delphi判断.net FrameWork是否已安装
判断系统中.NET FrameWork已安装版本的方法很多,比如检查C:\Windows\Microsoft.NET\Framework\下的子目录,但是子目录往往是包含版本号,比如v2.0.5072 ...
- Solidworks提示字体Arial Unicode MS安装不正确,PDF文件中一个或多个文本字串可能遗失怎么办
从以下网站下载Arial Unicode MS字体,WIN7的直接安装即可,XP的放到windows\fonts文件夹内.重启Solidworks即可 http://font.chinaz.com/1 ...
- 开始使用 Ubuntu(字体渲染去模糊+软件安装+优化配置+常见错误)(29)
1. 中文字体渲染美化 + 去模糊 步骤: 1. 解压安装 lulinux_fontsConf_181226.tar.gz,按里面的安装说明操作: 2. 开启字体渲染: 打开 unity-tweak- ...
- 开始使用 Manjaro(添加源+字体渲染去模糊+软件安装+优化配置+常见错误)(30)
1. 添加 archlinux 镜像源 1. 步骤一 向 /etc/pacman.d/mirrorlist 中添加国内镜像地址 1.1 方法1:自动添加 1. 输入如下命令查看国内镜像源,并按质量排序 ...
- 如何判断ios设备中是否安装了某款应用
URL Schemes关键字研究一下即可 常见得URL Schemes见http://www.cnblogs.com/huangzs/p/4491286.html if ([[UIApplicatio ...
- 浏览器未安装flash插件,js判断直接去官网安装
近期做了个活动页,里面根据需求插入了阿里云的视频,常见的浏览器都支持包括低版本的. 由于浏览器的更新换代很多版本放弃了flash的插件安装,火狐就是其中之一. 未安装flash的浏览器如果打开这个链接 ...
随机推荐
- 【test】
{"type":[ {"name":"专题分类","num":1,"value":[ ...
- 使用dlopen和dlsym来使用C++中的类
http://my.oschina.net/u/1450061/blog/204608
- C函数说明
输入函数scanf_s() 比如:char d[20];写成scanf_s("%s",d,20); 输出函数printf() 比如:printf("hell ...
- 用eclipse打包可执行的jar(含第三方jar包)
在eclipse中的解决方式如下: 在工程目录下(与src同层)建立lib目录,将第三方Jar包放到这个目录里(copy,paste即可)[如果直接引用本地的jar,一旦换电脑就呵呵了...] 右击工 ...
- Quartz 2D编程指南- PDF文档的创建、显示及转换
PDF文档存储依赖于分辨率的向量图形.文本和位图,并用于程序的一系列指令中.一个PDF文档可以包含多页的图形和文本.PDF可用于创建跨平台.只读的文档,也可用于绘制依赖于分辨率的图形. ...
- 玩转Eclipse — 自动代码规范检查工具Checkstyle
大项目都需要小组中的多人共同完成,但是每个人都有自己的编码习惯,甚至很多都是不正确的.那么如何使小组所有开发人员都遵循某些编码规范,以保证项目代码风格的一致性呢?如果硬性地要求每个开发人员在提交代码之 ...
- C#连接数据库以及增、删、改、查操作
using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Linq; usin ...
- 织梦DedeCMS模板通用安装方法
在网络上有很多可以免费下载的DEDECMS模板,下载之后如何安装是很多新手的共同问题.下面将结合我个人使用DEDECMS模板的一些心得,讲一下DEDE模板安装的通用方法. 1.下载一个模板之后要判断一 ...
- 我的 SONY MP3 NWZ-B105F
我的 SONY MP3 NWZ-B105F 只有 2GB 的容量,好像2007年出来的,现在还可以开机和播放. 使用的 SigmaTel 的方案 BGA 的封装,1.1inch 屏幕竟然是彩屏,锂电池 ...
- RK3288 指令查看HDMI当前分辨率和支持的分辨率
$ adb shell root@xxx:/ # cd /sys/class/display/HDMI cd /sys/class/display/HDMI root@xxx:/sys/class/d ...