Csharp/C#教程:windows系统下,如何在C#程序中自动安装字体分享

 1.1、使用代码安装字体

  注意:安装字体时,需要windows的管理员权限。

[DllImport("kernel32.dll",SetLastError=true)] publicstaticexternintWriteProfileString(stringlpszSection,stringlpszKeyName,stringlpszString); [DllImport("gdi32")] publicstaticexternintAddFontResource(stringlpFileName); ///<summary> ///安装字体 ///</summary> ///<paramname="fontFilePath">字体文件全路径</param> ///<returns>是否成功安装字体</returns> ///<exceptioncref="UnauthorizedAccessException">不是管理员运行程序</exception> ///<exceptioncref="Exception">字体安装失败</exception> publicstaticboolInstallFont(stringfontFilePath) { try { System.Security.Principal.WindowsIdentityidentity=System.Security.Principal.WindowsIdentity.GetCurrent(); System.Security.Principal.WindowsPrincipalprincipal=newSystem.Security.Principal.WindowsPrincipal(identity); //判断当前登录用户是否为管理员 if(principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator)==false) { thrownewUnauthorizedAccessException("当前用户无管理员权限,无法安装字体。"); } //获取Windows字体文件夹路径 stringfontPath=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(Exceptionex) { thrownewException(string.Format($"[{Path.GetFileNameWithoutExtension(fontFilePath)}]字体安装失败!原因:{ex.Message}")); } returntrue; }

  1.2、从项目资源文件中加载字体

  该方法需要开发者将字体文件以资源的形式放入项目资源文件中。不用安装到字体库中,其他程序如果需要使用,就需要自己安装或者加载。此时可以使用以下代码创建程序所需字体:

///<summary> ///如何使用资源文件中的字体,无安装无释放 ///</summary> ///<paramname="bytes">资源文件中的字体文件</param> ///<returns></returns> publicFontGetResoruceFont(byte[]bytes) { System.Drawing.Text.PrivateFontCollectionpfc=newSystem.Drawing.Text.PrivateFontCollection(); IntPtrMeAdd=Marshal.AllocHGlobal(bytes.Length); Marshal.Copy(bytes,0,MeAdd,bytes.Length); pfc.AddMemoryFont(MeAdd,bytes.Length); returnnewFont(pfc.Families[0],15,FontStyle.Regular); }

 1.3、加载某个字体文件,加载字体

 设置好某个字体的路径,然后加载字体文件,从而创建字体。不用安装到字体库中,其他程序如果需要使用,就需要自己安装或者加载。 

///<summary> ///通过文件获取字体 ///</summary> ///<paramname="filePath">文件全路径</param> ///<returns>字体</returns> publicFontGetFontByFile(stringfilePath) { //程序直接调用字体文件,不用安装到系统字库中。 System.Drawing.Text.PrivateFontCollectionpfc=newSystem.Drawing.Text.PrivateFontCollection(); pfc.AddFontFile(filePath);//字体文件的路径 Fontfont=newFont(pfc.Families[0],24,FontStyle.Regular,GraphicsUnit.Point,0);//font就是通过文件创建的字体对象 returnfont; }

    1.4、#动态加载和卸载字体(以文件的方式)
    因为是在CE里,所以是用CoredllPC机用的不是这个,可查MSDN

[System.Runtime.InteropServices.DllImport("coredll",EntryPoint="AddFontResource")] privatestaticexternintAddFontResource([In,MarshalAs(UnmanagedType.LPWStr)]stringfontSource); [DllImport("coredll",EntryPoint="SendMessage")] privatestaticexternintSendMessage(IntPtrhWnd,intMsg,IntPtrwParam,IntPtrlParam); privatevoidFun() { intinstallFont=AddFontResource(@"/SDMEM/MSYH.TTF");//这是字体的安装返回不为0即成功 SendMessage((IntPtr)0xffff,0x001d,IntPtr.Zero,IntPtr.Zero);//通知其它正在运行的应用程序,有新字体注册了 InstalledFontCollectionenumFonts=newInstalledFontCollection(); FontFamily[]fonts=enumFonts.Families; foreach(FontFamilyfontinfonts) { MessageBox.Show(font.Name); } }

  2、检测系统中是否包含某种字体

  对于检测是否已经安装了某种字体的方法有很多,这里只介绍检测是否有该文件的方式:

///<summary> ///检查字体是否存在 ///</summary> ///<paramname="familyName">字体名称</param> ///<returns></returns> publicstaticboolCheckFont(stringfamilyName) { stringFontPath=Path.Combine(System.Environment.GetEnvironmentVariable("WINDIR"),"fonts",Path.GetFileName(familyName)); //检测系统是否已安装该字体 returnFile.Exists(FontPath); }

   检测某种字体样式是否可用: 

///<summary> ///检测某种字体样式是否可用 ///</summary> ///<paramname="familyName">字体名称</param> ///<paramname="fontStyle">字体样式</param> ///<returns></returns> publicboolCheckFont(stringfamilyName,FontStylefontStyle=FontStyle.Regular) { InstalledFontCollectioninstalledFontCollection=newInstalledFontCollection(); FontFamily[]fontFamilies=installedFontCollection.Families; foreach(variteminfontFamilies) { if(item.Name.Equals(familyName)) { returnitem.IsStyleAvailable(fontStyle); } } returnfalse; }

上述就是C#学习教程:windows系统下,如何在C#程序中自动安装字体分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/903677.html

(0)
上一篇 2021年10月21日
下一篇 2021年10月21日

精彩推荐