windows系统下,如何在C#程序中自动安装字体

2022-07-28,,,,

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

    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、检测系统中是否包含某种字体

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

/// <summary>
/// 检查字体是否存在
/// </summary>
/// <param name="familyname">字体名称</param>
/// <returns></returns>
public static bool checkfont(string familyname)
{
  string fontpath = path.combine(system.environment.getenvironmentvariable("windir"), "fonts", 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;
}

以上就是windows系统下,如何在c#程序中自动安装字体的详细内容,更多关于c# 安装字体的资料请关注其它相关文章!

《windows系统下,如何在C#程序中自动安装字体.doc》

下载本文的Word格式文档,以方便收藏与打印。