WPF如何使用外部字体

当我们所使用的字体,系统不存在怎么办?

一种方式就是给系统安装该字体,这种方式安装的字体能够全局生效。

控制面板中可以看到本机已经安装了哪些字体:

第二种方法就是免安装,直接加载字体资源。

一. 全局安装:

cs 复制代码
[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(fontFilePath, fontPath); //font是程序目录下放字体的文件夹
                    AddFontResource(fontPath);
                    //安装字体
                    WriteProfileString("fonts", Path.GetFileNameWithoutExtension(fontFilePath) + "(TrueType)", Path.GetFileName(fontFilePath));
                }
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format($"[{Path.GetFileNameWithoutExtension(fontFilePath)}] 字体安装失败!原因:{ex.Message}"));
            }
            return true;
        }

这种方案需要管理员权限才能安装,并且安装的字体路径在不同(win7,win10,win11)的操作系统里面不尽相同。整个安装过程相对会麻烦一些,卸载软件的时候,还得考虑需不需要卸载我们软件安装的字体。于是我们有了第二种方案,免安装使用字体!

字体引入方式:

使用xaml方式:

html 复制代码
<TextBlock Text="text1">
    <TextBlock.FontFamily>
        <FontFamily>/程序集名称;component/Font/字体文件名#字体名称</FontFamily>
    </TextBlock.FontFamily>
</TextBlock>

后台代码引用:

cs 复制代码
textBlock.FontFamily=new FontFamily(new Uri("/程序集名称;component/Font/字体文件名"),"字体名称")

这里需要注意下,引入字体的路径是字体文件的相对或绝对路径,通过 #号连接 字体名称来引入。

相关推荐
dalong101 天前
WPF:箭头绘制程序
wpf·自定义绘制
张工在路上2 天前
WPF 布局基础概述
大数据·hadoop·wpf
迪飞特科技3 天前
分布式事务下 Spring 声明式事务失效的 3 种修复方案
分布式·spring·wpf
2601_962174176 天前
Redis 简介
数据库·redis·wpf
SamChan907 天前
Python+PyMuPDF提取PDF表格并翻译:表格结构检测与双语重建实战
windows·python·ai·pdf·wpf
SamChan907 天前
Python+OpenCV检测PDF翻译后排版偏移:像素级格式一致性验证
python·opencv·ai·pdf·wpf
FuckPatience7 天前
WPF 拿到控件的最初样式,修改获得焦点样式
wpf
主宰者7 天前
【WPF】MainWindow前添加加载窗口,发现加载过程中有白色无内容窗口闪烁一下的解决方案
wpf
Vae_Mars8 天前
WPF中的ControlTemplate(控件模板)
wpf
SamChan908 天前
Python+ReportLab自动生成PDF翻译质量审计报告:从数据到可视化的完整方案
开发语言·python·ai·pdf·wpf