using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
namespace Lskj.Util
{
public class FontHelper
{
[DllImport("kernel32.dll", SetLastError = true)]
private static extern int WriteProfileString(string lpszSection, string lpszKeyName, string lpszString);
[DllImport("gdi32")]
private static extern int AddFontResource(string lpFileName);
///
/// 安装字体
///
/// 字体文件全路径
/// 是否成功安装字体
/// 不是管理员运行程序
/// 字体安装失败
public static bool InstallFont(string fontFilePath)
{
try
{
System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity);
//判断当前登录用户是否为管理员
if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator) == false)
{
return false;
}
//获取Windows字体文件夹路径
string fontPath = Path.Combine(System.Environment.GetEnvironmentVariable("WINDIR"), "fonts", Path.GetFileName(fontFilePath));
//检测系统是否已安装该字体
if (!File.Exists(fontPath))
{
// File.Copy(System.Windows.Forms.Application.StartupPath + "\\font\\" + FontFileName, FontPath); //font是程序目录下放字体的文件夹
//将某路径下的字体拷贝到系统字体文件夹下
File.Copy(fontFilePath, fontPath); //font是程序目录下放字体的文件夹
AddFontResource(fontPath);
//Res = SendMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0);
//WIN7下编译会出错,不清楚什么问题。注释就行了。
//安装字体
WriteProfileString("fonts", Path.GetFileNameWithoutExtension(fontFilePath) + "(TrueType)", Path.GetFileName(fontFilePath));
}
}
catch (Exception)
{
return false;
}
return true;
}
}
}