windows系统下,在C#程序中自动安装字体
发布时间:2020-12-14 03:54:56 所属栏目:Windows 来源:网络整理
导读:在Windows系统中,原有自带的字体样式有限,有时候我们的程序会使用到个别稀有或系统不自带的字体。因此我们需要将字体打包到程序中,当程序启动时,检测系统是否有该字体,如果没有则安装该字体,也可以动态加载字体。 1.1、使用代码安装字体 注意:安装字
在Windows系统中,原有自带的字体样式有限,有时候我们的程序会使用到个别稀有或系统不自带的字体。因此我们需要将字体打包到程序中,当程序启动时,检测系统是否有该字体,如果没有则安装该字体,也可以动态加载字体。 1.1、使用代码安装字体注意:安装字体时,需要windows的管理员权限。 [DllImport("kernel32.dll",SetLastError = true)] public static extern int WriteProfileString(string lpszSection,string lpszKeyName,string lpszString); [DllImport("gdi32")] public static extern int AddFontResource(string lpFileName); /// <summary> /// 安装字体 /// </summary> /// <param name="fontFilePath">字体文件全路径</param> /// <returns>是否成功安装字体</returns> /// <exception cref="UnauthorizedAccessException">不是管理员运行程序</exception> /// <exception cref="Exception">字体安装失败</exception> 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) { throw new UnauthorizedAccessException("当前用户无管理员权限,无法安装字体。"); } //获取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); //WIN7下编译会出错,不清楚什么问题。注释就行了。 //安装字体 WriteProfileString("fonts",Path.GetFileNameWithoutExtension(fontFilePath) + "(TrueType)",Path.GetFileName(fontFilePath)); } } catch (Exception ex) { throw new Exception(string.Format($"[{Path.GetFileNameWithoutExtension(fontFilePath)}] 字体安装失败!原因:{ex.Message}" )); } return true; } ? 1.2、从项目资源文件中加载字体该方法需要开发者将字体文件以资源的形式放入项目资源文件中。不用安装到字体库中,其他程序如果需要使用,就需要自己安装或者加载。此时可以使用以下代码创建程序所需字体: /// <summary> /// 如何使用资源文件中的字体,无安装无释放 /// </summary> /// <param name="bytes">资源文件中的字体文件</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,0,MeAdd,bytes.Length); pfc.AddMemoryFont(MeAdd,bytes.Length); return new Font(pfc.Families[0],15,FontStyle.Regular); } 1.3、加载某个字体文件,加载字体设置好某个字体的路径,然后加载字体文件,从而创建字体。不用安装到字体库中,其他程序如果需要使用,就需要自己安装或者加载。? /// <summary> /// 通过文件获取字体 /// </summary> /// <param name="filePath">文件全路径</param> /// <returns>字体</returns> public Font GetFontByFile(string filePath) { //程序直接调用字体文件,不用安装到系统字库中。 System.Drawing.Text.PrivateFontCollection pfc = new System.Drawing.Text.PrivateFontCollection(); pfc.AddFontFile(filePath);//字体文件的路径 Font font = new Font(pfc.Families[0],24,FontStyle.Regular,GraphicsUnit.Point,0);//font就是通过文件创建的字体对象 return font; } ? ?? 2、检测系统中是否包含某种字体对于检测是否已经安装了某种字体的方法有很多,这里只介绍检测是否有该文件的方式: /// <summary> /// 检查字体是否存在 /// </summary> /// <param name="familyName">字体名称</param> /// <returns></returns> public static bool CheckFont(string familyName) { string FontPath = Path.Combine(System.Environment.GetEnvironmentVariable("WINDIR"),Path.GetFileName(familyName)); //检测系统是否已安装该字体 return File.Exists(FontPath); } ? 检测某种字体样式是否可用: ? /// <summary> /// 检测某种字体样式是否可用 /// </summary> /// <param name="familyName">字体名称</param> /// <param name="fontStyle">字体样式</param> /// <returns></returns> public bool CheckFont(string familyName,FontStyle fontStyle= FontStyle.Regular ) { InstalledFontCollection installedFontCollection = new InstalledFontCollection(); FontFamily[] fontFamilies = installedFontCollection.Families; foreach(var item in fontFamilies) { if (item.Name.Equals(familyName)) { return item.IsStyleAvailable(fontStyle); } } return false; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 将Microsoft动态2013或2015与PHP集成
- Windows – 微软仍然支持WIN吗?
- Windows – 打开.bat文件脚本后如何保持cmd运行
- osx – 用于在Windows / Linux上创建Mac软件包的PackageMak
- 如何在Windows和Java下与USB设备进行通信?
- 批处理文件 – Windows批处理文件过早终止
- 如何在microsoft azure上配置实体框架(Model First)?
- windows – 如何在GetLastError()返回ERROR_MOD_NOT_FOUND后
- windows-server-2008 – 是否可以在本地从Hyper-V服务器访问
- .net – 为什么暴露的类型必须为WinMD / WinRT组件密封?
推荐文章
站长推荐
- microsoft-graph – 使用所有者在Microsoft Grap
- New-Object上的Windows RT Powershell(Permissio
- 在Windows中更改文件所有者
- 用于支持Windows Vista / 7中的回声消除的API
- 在Windows上打开Python中的设备的句柄
- windows-mobile – UWP:File.ReadAllBytes来自A
- windows – 域安全性与森林安全性
- Windows Admin希望从Linux(ubuntu)文件服务器开始
- 如何在Windows 10中升级Python安装?
- 将Clickonce应用程序分发到Windows 8
热点阅读