zoukankan      html  css  js  c++  java
  • C#使用自定义字体

            /// <summary>
            /// 设置字体
            /// </summary>
            /// <param name="path">字体文件路径,包含字体文件名和后缀名</param>
            /// <param name="fontStyle">字形(常规/粗体/斜体/粗斜体)</param>
            /// <param name="size">大小</param>
            public Font FontSet(string path, FontStyle fontStyle, int size)
            {
                //参数 System.Windows.Forms.Control control,
                //System.Reflection.PropertyInfo per = control.GetType().GetProperty("Font");//判断传进来的控件是否具有Text属性
                //if (per == null)
                //{
                //    return;
                //}
                //String path = System.Windows.Forms.Application.StartupPath + "\Fonts\";//font是程序目录下放字体的文件夹
                try
                {
                    System.Drawing.Text.PrivateFontCollection pfc = new System.Drawing.Text.PrivateFontCollection();
    
                    pfc.AddFontFile(path);//字体文件的路径
    
                    Font myFont = new Font(pfc.Families[0], size, fontStyle);
                    return myFont;
                }
                catch (System.Exception)
                {
                    return null;
                    // throw;
                }
            }
    

     加载系统已安装的字体到列表中:

                //加载系统已经安装的字体
                InstalledFontCollection myFonts = new InstalledFontCollection();
                FontFamily[] fontFamily = myFonts.Families;
                if (fontFamily != null && fontFamily.Length > 0)
                {
                    Array.Reverse(fontFamily);
                    foreach (FontFamily family in fontFamily)
                    {
                        this.lstFontType.Items.Add(family.Name);
                    }
    
                }
                else
                {
                    return;
                }
    

     加载指定路径的字体:

                //加载指定路径下的字体
                string strPat = Application.StartupPath + "\Fonts\";
                //遍历指定文件夹中的所有文件
                DirectoryInfo TheFolder = new DirectoryInfo(strPat);
    
                //遍历文件
                foreach (FileInfo NextFile in TheFolder.GetFiles())
                {
    
                    if (NextFile.Extension.ToLower() == ".ttf")
                    {
                        this.lstFontType.Items.Add(NextFile.Name.ToLower().TrimEnd(".ttf".ToCharArray()));
                    }
                }
    
  • 相关阅读:
    二叉树遍历问题、时间空间复杂度、淘汰策略算法、lru数据结构、动态规划贪心算法
    Django--csrf跨站请求伪造、Auth认证模块
    Django--中间件
    Django--Cookie和Session组件
    Django--form表单组件
    安装配置flask环境
    Django--模型层
    Django--路由层、视图层、模版层
    Eclipse SVN文件冲突及不能直接提交情况
    Eclipse开发Web常见异常
  • 原文地址:https://www.cnblogs.com/wxylog/p/6872324.html
Copyright © 2011-2022 走看看