读取私有字体信息并进行相关判断

  1 private void Font_IsStyleAvailable()
  2         {
  3             Graphics graphics = CreateGraphics();
  4             graphics.Clear(Color.White);
  5 
  6             Pen pen = new Pen(Color.Gray);
  7             PointF pointf = new PointF(10.0f, 0.0f);
  8             SolidBrush solidbrush = new SolidBrush(Color.Black);
  9             Font mesfont = new Font("Arial", 12);
 10 
 11             //调用PrivateFontCollection类读取私有字体
 12             PrivateFontCollection privatefontcollection = new PrivateFontCollection();
 13             privatefontcollection.AddFontFile(@"C:/windows/fonts/simhei.ttf");
 14             privatefontcollection.AddFontFile(@"C:/windows/fonts/CourBI.ttf");
 15             privatefontcollection.AddFontFile(@"C:/windows/fonts/STLITI.ttf");
 16 
 17             string familyname = string.Empty;
 18             string tmpmes = string.Empty;
 19 
 20             int count = privatefontcollection.Families.Length;
 21             FontFamily[] fontfamilies = new FontFamily[count];
 22             fontfamilies = privatefontcollection.Families;
 23 
 24             for (int j = 0; j < count; j++)
 25             {
 26                 familyname = fontfamilies[j].Name;
 27                 //判断字体的常规风格是否可用
 28                 if (fontfamilies[j].IsStyleAvailable(FontStyle.Regular))
 29                 {
 30                     tmpmes = "字体的常规风格可用";
 31                     FontFamily fontfamily = new FontFamily(familyname, privatefontcollection);
 32                     Font tmpfont = new Font(fontfamily, 12, FontStyle.Regular, GraphicsUnit.Pixel);
 33                     graphics.DrawString(familyname + tmpmes, tmpfont, solidbrush, pointf);
 34                     pointf.Y += mesfont.Height;
 35                 }
 36                 else
 37                 {
 38                     graphics.DrawString(familyname + tmpmes + "字体的常规风格不可用", mesfont, solidbrush, pointf);
 39                     pointf.Y += mesfont.Height;
 40                 }
 41 
 42                 tmpmes = string.Empty;
 43                 //判断字体的粗体风格是否可用
 44                 if (fontfamilies[j].IsStyleAvailable(FontStyle.Bold))
 45                 {
 46                     tmpmes = "字体的粗体风格可用";
 47                     FontFamily fontfamily = new FontFamily(familyname, privatefontcollection);
 48                     Font tmpfont = new Font(fontfamily, 12, FontStyle.Bold, GraphicsUnit.Pixel);
 49                     graphics.DrawString(familyname + tmpmes, tmpfont, solidbrush, pointf);
 50                     pointf.Y += mesfont.Height;
 51                 }
 52                 else
 53                 {
 54                     graphics.DrawString(familyname + tmpmes + " 字体的粗体风格不可用", mesfont, solidbrush, pointf);
 55                     pointf.Y += mesfont.Height;
 56                 }
 57 
 58                 tmpmes = string.Empty;
 59                 //判断字体的斜体风格是否可用
 60                 if (fontfamilies[j].IsStyleAvailable(FontStyle.Italic))
 61                 {
 62                     tmpmes = "字体的斜体风格可用";
 63                     FontFamily fontfamily = new FontFamily(familyname, privatefontcollection);
 64                     Font tmpfont = new Font(fontfamily, 12, FontStyle.Italic, GraphicsUnit.Pixel);
 65                     graphics.DrawString(familyname + tmpmes, tmpfont, solidbrush, pointf);
 66                     pointf.Y += mesfont.Height;
 67                 }
 68                 else
 69                 {
 70                     graphics.DrawString(familyname + tmpmes + " 字体的斜体风格不可用", mesfont, solidbrush, pointf);
 71                     pointf.Y += mesfont.Height;
 72                 }
 73 
 74                 tmpmes = string.Empty;
 75                 //判断字体的下划线风格是否可用
 76                 if (fontfamilies[j].IsStyleAvailable(FontStyle.Underline))
 77                 {
 78                     tmpmes = "字体的下划线风格可用";
 79                     FontFamily fontfamily = new FontFamily(familyname, privatefontcollection);
 80                     Font tmpfont = new Font(fontfamily, 12, FontStyle.Underline, GraphicsUnit.Pixel);
 81                     graphics.DrawString(familyname + tmpmes, tmpfont, solidbrush, pointf);
 82                     pointf.Y += mesfont.Height;
 83                 }
 84                 else
 85                 {
 86                     graphics.DrawString(familyname + tmpmes + " 字体的下划线风格不可用", mesfont, solidbrush, pointf);
 87                     pointf.Y += mesfont.Height;
 88                 }
 89 
 90                 tmpmes = string.Empty;
 91                 //判断字体的强调线风格是否可用
 92                 if (fontfamilies[j].IsStyleAvailable(FontStyle.Strikeout))
 93                 {
 94                     tmpmes = "字体的强调线风格可用";
 95                     FontFamily fontfamily = new FontFamily(familyname, privatefontcollection);
 96                     Font tmpfont = new Font(fontfamily, 12, FontStyle.Strikeout, GraphicsUnit.Pixel);
 97                     graphics.DrawString(familyname + tmpmes, tmpfont, solidbrush, pointf);
 98                     pointf.Y += mesfont.Height;
 99                 }
100                 else
101                 {
102                     graphics.DrawString(familyname + tmpmes + " 字体的强调线风格不可用", mesfont, solidbrush, pointf);
103                     pointf.Y += mesfont.Height;
104                 }
105 
106                 graphics.DrawLine(pen, 0, (int)pointf.Y, 400, (int)pointf.Y);
107                 pointf.Y += 10.0f;
108             }
109 
110         } 
原文地址:https://www.cnblogs.com/micro-chen/p/13517374.html