如何C#程序安装windows系统字体
创始人
2024-11-18 18:01:44
0次

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

 /// 
 /// 安装字体
 /// 
 /// 字体文件全路径
 /// 是否成功安装字体
 /// 不是管理员运行程序
 /// 字体安装失败
 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, 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、从项目资源文件中加载字体

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

/// 
/// 如何使用资源文件中的字体,无安装无释放
/// 
/// 资源文件中的字体文件
/// 
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、加载某个字体文件,加载字体

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

/// 
/// 通过文件获取字体
/// 
/// 文件全路径
/// 字体
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;
}

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

[System.Runtime.InteropServices.DllImport("coredll", EntryPoint = "AddFontResource")]
 private static extern int AddFontResource([In,MarshalAs(UnmanagedType.LPWStr)]string fontSource);

 [DllImport("coredll", EntryPoint = "SendMessage")]
 private static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
 private void Fun()
 {
   int installFont = AddFontResource(@"/SDMEM/MSYH.TTF"); //这是字体的安装 返回不为0即成功
   SendMessage((IntPtr)0xffff, 0x001d, IntPtr.Zero, IntPtr.Zero); //通知其它正在运行的应用程序,有新字体注册了           
   InstalledFontCollection enumFonts = new InstalledFontCollection();
   FontFamily[] fonts = enumFonts.Families;
   foreach (FontFamily font in fonts)
   {
     MessageBox.Show(font.Name);
   }
 }

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

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

/// 
/// 检查字体是否存在
/// 
/// 字体名称
/// 
public static bool CheckFont(string familyName)
{
  string FontPath = Path.Combine(System.Environment.GetEnvironmentVariable("WINDIR"), "fonts", Path.GetFileName(familyName));
  //检测系统是否已安装该字体
  return File.Exists(FontPath);
}

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

/// 
/// 检测某种字体样式是否可用
/// 
/// 字体名称
/// 字体样式
/// 
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;
}

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...