C# 判斷字體是否存在以及安裝


 
 

1. 字體安裝

在實際開發項目中,需要在客戶端安裝字體,一種是通過代碼將字體文件復制到系統FONT目錄即可,另一種通過安裝文件實現,至於其他方式還未知曉。

1.1 軟安裝

public class FontOperate
{
        [DllImport("kernel32.dll", SetLastError = true)]
        static extern int WriteProfileString(string lpszSection, string lpszKeyName, string lpszString);

        [DllImport("user32.dll")]
        public static extern int SendMessage(int hWnd,// handle to destination window 
        uint Msg,  // message 
        int wParam, // first message parameter 
        int lParam  // second message parameter 
        );
        [DllImport("gdi32")]
        public static extern int AddFontResource(string lpFileName);


        public static bool InstallFont(string sFontFileName, string sFontName)
        {
            string _sTargetFontPath = string.Format(@"{0}\fonts\{1}", System.Environment.GetEnvironmentVariable("WINDIR"), sFontFileName);//系統FONT目錄
            string _sResourceFontPath = string.Format(@"{0}\Font\{1}", System.Windows.Forms.Application.StartupPath, sFontFileName);//需要安裝的FONT目錄
       
       int Res;

       const int WM_FONTCHANGE = 0x001D;
            const int HWND_BROADCAST = 0xffff;
try { if (!File.Exists(_sTargetFontPath) && File.Exists(_sResourceFontPath)) { int _nRet; File.Copy(_sResourceFontPath, _sTargetFontPath); _nRet = AddFontResource(_sTargetFontPath);
            Res = SendMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0); _nRet
= WriteProfileString("fonts", sFontName + "(TrueType)", sFontFileName); } } catch { return false; } return true; } }

 

函數的使用: 
fonts.installFont(字體文件, 字體名稱)//fonts類名
fonts.installFont("C39P36DmTt.TTF", "C39P36DmTt")

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UtilityHelper;

namespace LHCity_LMS_Client.Test
{
    class Program
    {
        static void Main(string[] args)
        {
            FontOperate.InstallFont("simfang.ttf", "simfang");
            Console.ReadLine();
        }
    }
}
Using demo

 

1.2 使用資源文件中的字體

/// <summary>
/// 如何使用資源文件中的字體,無安裝無釋放
/// </summary>
/// <param name="bytes">資源文件中的字體文件,如Properties.Resources.華文行楷</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);
}    

Demo2

//程序直接調用字體文件,不用安裝到系統字庫中。
//設置字體對象:
String ls_appPath = System.Windows.Forms.Application.StartupPath + "\\font\\";//font是程序目錄下放字體的文件夾
String fontFile1 = ls_appPath + "C39P36DmTt.TTF";
String fontFile2 = ls_appPath + "GWGLYPTT.TTF";
......
pfc.AddFontFile(fontFile1);//字體文件的路徑
pfc.AddFontFile(fontFile2);//字體文件的路徑
........
Font myFont1 = new Font(pfc.Families[0], 41, FontStyle.Regular, GraphicsUnit.Point, 0);//myFont1就是你創建的字體對象
Font myFont2 = new Font(pfc.Families1], 31, FontStyle.Bold | FontStyle.Regular); 

//使用字體: //label1.Font = myFont1;

 

1.4 軟件發布時的包含

當前可以有以下兩種方法實現:

(1)通過MSI安裝文件實現;

(2) InstallShield 部署軟件中設置字體安裝。

這兩種方式,不再詳述,具體請百度。

 2 字體是否存在判斷

可以參考下面的代碼進行實現

List<string> arrStrNames = new List<string>();  
InstalledFontCollection MyFont = new InstalledFontCollection();  
FontFamily[] fontFamilys = MyFont.Families;  
if (fontFamilys == null || fontFamilys.Length < 1)  
{  
  return null;  
}  
foreach (FontFamily item in fontFamilys)  
{  
  arrStrNames.Add(item.Name);  
}  
return arrStrNames;  

 算了,我還是補充上來吧。今天晚上就給字體叫上勁了。

public static bool CheckSysFontExisting(string fontName = "文鼎細黑")
{
    Font font;

    try
    {
        font = new Font(fontName, 10);
        if (font.Name != fontName)
        {
            return false;
        }

    }
    catch (Exception ex)
    {
        return false;
    }

    return true;
}

 

 

 

 

 

 

參考文章

[WinForm]安裝字體兩種方式

程序安裝字體或直接調用非注冊字體[c#]     

C# 如何使用資源文件中的字體,無安裝無釋放 

獲取系統所有安裝的字體名稱

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM