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;
}

参考文章

[WinForm]安装字体两种方式

程序安装字体或直接调用非注册字体[c#]

C# 如何使用资源文件中的字体,无安装无释放

获取系统所有安装的字体名称

C# 判断字体是否存在以及安装的更多相关文章

  1. js判断本机是否已安装app

    需求:在浏览器或者app webview中打开的页面,js判断本机是否已安装搜狐新闻客户端. 一.微信 1.分享——好友/朋友圈,feed会有搜狐新闻标记,打开url后缀参数isappinstalle ...

  2. js判断ie和edge是否安装Adobe Reader PDF阅读器

    ie浏览器和edge浏览器,必须用Adobe Reader PDF阅读器才可以打开pdf文件,其他现代浏览器自带pdf阅读器,无需安装. 判断ie或者edge如果安装了,就浏览pdf文件:如果没安装就 ...

  3. android 判断应用程序是否已安装

    1.判断是否安装/** check the app is installed*/private boolean isAppInstalled(Context context,String packag ...

  4. delphi判断.net FrameWork是否已安装

    判断系统中.NET FrameWork已安装版本的方法很多,比如检查C:\Windows\Microsoft.NET\Framework\下的子目录,但是子目录往往是包含版本号,比如v2.0.5072 ...

  5. Solidworks提示字体Arial Unicode MS安装不正确,PDF文件中一个或多个文本字串可能遗失怎么办

    从以下网站下载Arial Unicode MS字体,WIN7的直接安装即可,XP的放到windows\fonts文件夹内.重启Solidworks即可 http://font.chinaz.com/1 ...

  6. 开始使用 Ubuntu(字体渲染去模糊+软件安装+优化配置+常见错误)(29)

    1. 中文字体渲染美化 + 去模糊 步骤: 1. 解压安装 lulinux_fontsConf_181226.tar.gz,按里面的安装说明操作: 2. 开启字体渲染: 打开 unity-tweak- ...

  7. 开始使用 Manjaro(添加源+字体渲染去模糊+软件安装+优化配置+常见错误)(30)

    1. 添加 archlinux 镜像源 1. 步骤一 向 /etc/pacman.d/mirrorlist 中添加国内镜像地址 1.1 方法1:自动添加 1. 输入如下命令查看国内镜像源,并按质量排序 ...

  8. 如何判断ios设备中是否安装了某款应用

    URL Schemes关键字研究一下即可 常见得URL Schemes见http://www.cnblogs.com/huangzs/p/4491286.html if ([[UIApplicatio ...

  9. 浏览器未安装flash插件,js判断直接去官网安装

    近期做了个活动页,里面根据需求插入了阿里云的视频,常见的浏览器都支持包括低版本的. 由于浏览器的更新换代很多版本放弃了flash的插件安装,火狐就是其中之一. 未安装flash的浏览器如果打开这个链接 ...

随机推荐

  1. Android Hook 框架 Cydia_substrate 详解

    目录(?)[-] 使用方法 短信监控实例 1.Cydia_Substrate 框架简介 Cydia Substrate是一个代码修改平台.它可以修改任何主进程的代码,不管是用Java还是C/C++(n ...

  2. bzoj 1858 序列操作

    bzoj 1858 序列操作 带有随机多个区间单值覆盖的区间操作题,可考虑用珂朵莉树解决. #include<bits/stdc++.h> using namespace std; #de ...

  3. tableview小结-初学者的问题

    初学者的问题主要集中在,下面几个问题: 一.几个函数总是不被调用:例如: - (UIView *)tableView:(UITableView *)tableView viewForHeaderInS ...

  4. C#读取Excel数据操作大全

    苦丁茶 发表于 2014-02-10 12:58:00 | 分类标签: ASP.NET 读取Excel 本文介绍下,用C#读取excel数据的例子,包括读取整个工作薄的数据.读取工作薄选定区域中的数据 ...

  5. elixir 集成ejabberd

    备注: 我开发测试的环境时centos 1. 预备环境 1. openssl yum install -y  openssl-devel 2. xml yum install -y expat-dev ...

  6. hapi lab测试框架简单使用

    1. 依赖安装 yarn init yarn add lab code 2. 基本模式 const Lab = require('lab'); const Code = require('code') ...

  7. 什么是spark(一) 分区以及和MR的区别

    什么是spark,是一个分布式计算平台,或者说是分布式计算引擎,他的职责就是将指定的数据读入到各个node的内存中,然后计算.所以spark是具有泛化性质的,只要数据源是可读入的,读到内存里面之后,处 ...

  8. linux误删数据恢复

    linux下数据恢复工具有: 1.通过分析文件系统的日志,解析文件的inode,可以恢复ex3 ex4的文件系统下的数据 extundelete:扫描inode和恢复数据同时进行,因此恢复速度很快.支 ...

  9. Synergy CORTEX M 启动流程

    1.启动文件“startup_S7G2.c” 中断向量表地址指针:“0xe000ed08” /* Vector table. */ BSP_DONT_REMOVE const exc_ptr_t __ ...

  10. GDB调试字符数组时指针和数组区别的体现

    测试ftell函数时发现报错,先贴源码 // File Name: ftell.c #include <stdio.h> #include <stdlib.h> int mai ...