溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何C#程序安裝windows系統(tǒng)字體

發(fā)布時(shí)間:2020-11-07 16:08:36 來(lái)源:億速云 閱讀:296 作者:Leah 欄目:開(kāi)發(fā)技術(shù)

如何C#程序安裝windows系統(tǒng)字體?針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。

  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)前用戶無(wú)管理員權(quán)限,無(wú)法安裝字體。");
     }
     //獲取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ò),不清楚什么問(wèn)題。注釋就行了。 
       //安裝字體
       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)目資源文件中加載字體

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

/// <summary>
/// 如何使用資源文件中的字體,無(wú)安裝無(wú)釋放
/// </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)建字體。不用安裝到字體庫(kù)中,其他程序如果需要使用,就需要自己安裝或者加載。 

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

關(guān)于如何C#程序安裝windows系統(tǒng)字體問(wèn)題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒(méi)有解開(kāi),可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI