最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

windows系統(tǒng)下,如何在C#程序中自動(dòng)安裝字體

 更新時(shí)間:2020年11月07日 09:19:02   作者:一只獨(dú)行的猿  
在Windows系統(tǒng)中,原有自帶的字體樣式有限,有時(shí)候我們的程序會(huì)使用到個(gè)別稀有或系統(tǒng)不自帶的字體。因此我們需要將字體打包到程序中,當(dāng)程序啟動(dòng)時(shí),檢測(cè)系統(tǒng)是否有該字體,如果沒有則安裝該字體,也可以動(dòng)態(tài)加載字體。

  1.1、使用代碼安裝字體

  注意:安裝字體時(shí),需要windows的管理員權(quán)限。

[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">不是管理員運(yùn)行程序</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);
     //判斷當(dāng)前登錄用戶是否為管理員
     if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator) == false)
     {
       throw new UnauthorizedAccessException("當(dāng)前用戶無管理員權(quán)限,無法安裝字體。");
     }
     //獲取Windows字體文件夾路徑
     string fontPath=Path.Combine(System.Environment.GetEnvironmentVariable("WINDIR") , "fonts",Path.GetFileName(fontFilePath));
     //檢測(cè)系統(tǒng)是否已安裝該字體
     if (!File.Exists(fontPath))
     {
       // File.Copy(System.Windows.Forms.Application.StartupPath + "\\font\\" + FontFileName, FontPath); //font是程序目錄下放字體的文件夾
       //將某路徑下的字體拷貝到系統(tǒng)字體文件夾下
       File.Copy(fontFilePath, fontPath); //font是程序目錄下放字體的文件夾
       AddFontResource(fontPath);

       //Res = SendMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0); 
       //WIN7下編譯會(huì)出錯(cuò),不清楚什么問題。注釋就行了。 
       //安裝字體
       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、從項(xiàng)目資源文件中加載字體

  該方法需要開發(fā)者將字體文件以資源的形式放入項(xiàng)目資源文件中。不用安裝到字體庫中,其他程序如果需要使用,就需要自己安裝或者加載。此時(shí)可以使用以下代碼創(chuàng)建程序所需字體:

/// <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、加載某個(gè)字體文件,加載字體

 設(shè)置好某個(gè)字體的路徑,然后加載字體文件,從而創(chuàng)建字體。不用安裝到字體庫中,其他程序如果需要使用,就需要自己安裝或者加載。 

/// <summary>
/// 通過文件獲取字體
/// </summary>
/// <param name="filePath">文件全路徑</param>
/// <returns>字體</returns>
public Font GetFontByFile(string filePath)
{
  //程序直接調(diào)用字體文件,不用安裝到系統(tǒng)字庫中。
  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就是通過文件創(chuàng)建的字體對(duì)象
  return font;
}

    1.4、#動(dòng)態(tài)加載和卸載字體(以文件的方式)
     因?yàn)槭窃贑E里,所以是用Coredll PC機(jī)用的不是這個(gè),可查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); //通知其它正在運(yùn)行的應(yīng)用程序,有新字體注冊(cè)了           
   InstalledFontCollection enumFonts = new InstalledFontCollection();
   FontFamily[] fonts = enumFonts.Families;
   foreach (FontFamily font in fonts)
   {
     MessageBox.Show(font.Name);
   }
 }

  2、檢測(cè)系統(tǒng)中是否包含某種字體

  對(duì)于檢測(cè)是否已經(jīng)安裝了某種字體的方法有很多,這里只介紹檢測(cè)是否有該文件的方式:

/// <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));
  //檢測(cè)系統(tǒng)是否已安裝該字體
  return File.Exists(FontPath);
}

   檢測(cè)某種字體樣式是否可用: 

/// <summary>
/// 檢測(cè)某種字體樣式是否可用
/// </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系統(tǒng)下,如何在C#程序中自動(dòng)安裝字體的詳細(xì)內(nèi)容,更多關(guān)于c# 安裝字體的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 深入理解C#窗體關(guān)閉事件

    深入理解C#窗體關(guān)閉事件

    很多初學(xué)者都想把默認(rèn)的C#關(guān)閉按鈕事件弄明白,本文就介紹一下C#窗體關(guān)閉事件,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • protobuf對(duì)象二進(jìn)制序列化存儲(chǔ)(詳解)

    protobuf對(duì)象二進(jìn)制序列化存儲(chǔ)(詳解)

    下面小編就為大家?guī)硪黄猵rotobuf對(duì)象二進(jìn)制序列化存儲(chǔ)(詳解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-02-02
  • 基于C#實(shí)現(xiàn)端口掃描器(單線程和多線程)

    基于C#實(shí)現(xiàn)端口掃描器(單線程和多線程)

    本文主要介紹了基于C#分別通過單線程和多線程實(shí)現(xiàn)端口掃描,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-12-12
  • C#使用StopWatch獲取程序毫秒級(jí)執(zhí)行時(shí)間的方法

    C#使用StopWatch獲取程序毫秒級(jí)執(zhí)行時(shí)間的方法

    這篇文章主要介紹了C#使用StopWatch獲取程序毫秒級(jí)執(zhí)行時(shí)間的方法,涉及C#操作時(shí)間的相關(guān)技巧,需要的朋友可以參考下
    2015-04-04
  • unity自帶尋路(導(dǎo)航)系統(tǒng) Nav Mesh導(dǎo)航網(wǎng)格

    unity自帶尋路(導(dǎo)航)系統(tǒng) Nav Mesh導(dǎo)航網(wǎng)格

    這篇文章主要為大家詳細(xì)介紹了unity自帶尋路(導(dǎo)航)系統(tǒng),Nav Mesh導(dǎo)航網(wǎng)格,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • C#讀寫操作app.config中的數(shù)據(jù)應(yīng)用介紹

    C#讀寫操作app.config中的數(shù)據(jù)應(yīng)用介紹

    C#讀寫操作app.config中的數(shù)據(jù)應(yīng)用介紹;需要的朋友可以參考下
    2012-11-11
  • C#讀取csv格式文件的方法

    C#讀取csv格式文件的方法

    這篇文章主要介紹了C#讀取csv格式文件的方法,包括針對(duì)csv文件操作的規(guī)則,實(shí)例代碼部分包含了數(shù)據(jù)有效性驗(yàn)證及行列驗(yàn)證等內(nèi)容,邏輯嚴(yán)謹(jǐn),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2014-10-10
  • 避免在C#循環(huán)中使用await的方法小結(jié)

    避免在C#循環(huán)中使用await的方法小結(jié)

    在C#中,異步編程因其能夠提升應(yīng)用程序性能和響應(yīng)能力而變得越來越流行,async和await關(guān)鍵字使得編寫異步代碼變得更加容易,但如果使用不當(dāng),它們也可能引入一些陷阱,所以本文我們將探討為什么應(yīng)該避免在C#循環(huán)中使用await,并討論一些更高效地處理異步操作的替代方法
    2024-09-09
  • C#設(shè)置右鍵菜單的方法

    C#設(shè)置右鍵菜單的方法

    這篇文章主要介紹了C#設(shè)置右鍵菜單的方法,實(shí)例分析了C#設(shè)置右鍵菜單的相關(guān)技巧,需要的朋友可以參考下
    2015-06-06
  • C#?Winform實(shí)現(xiàn)圓角無鋸齒按鈕

    C#?Winform實(shí)現(xiàn)圓角無鋸齒按鈕

    這篇文章主要介紹了C#?Winform實(shí)現(xiàn)圓角無鋸齒按鈕,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,感興趣的小伙伴可以參考一下
    2022-08-08

最新評(píng)論

合水县| 肥乡县| 新沂市| 敦煌市| 大同市| 淮滨县| 北辰区| 鱼台县| 乌拉特后旗| 弥渡县| 宾阳县| 区。| 乌兰察布市| 吕梁市| 固原市| 宜兰市| 环江| 河曲县| 平和县| 扬中市| 蒙自县| 富裕县| 博客| 巧家县| 湘潭市| 来安县| 镇宁| 车险| 和政县| 宜章县| 玛沁县| 新安县| 苗栗县| 磐石市| 余干县| 靖远县| 青冈县| 苍南县| 昌吉市| 江阴市| 明溪县|