Font样式只有在创建的时候才能设定,将样式存储到数据库中后,想要还原Label的Font,则需要考虑拆装等

情况.

 

model 为实体类的一个实例,里面包含了

fontFamily    字符串
fontSize       decimal
italic            是否斜体, boolean值
bold             是否粗体, boolean值
strikeout     是否有删除线,boolean值
Underline    是否有下划线,boolean值

 

 

 

首先构造一个所有样式都有的Font

 

Font font = new Font(new FontFamily(model.fontFamily),
                                              float.Parse(model.fontSize.ToString()),
                                               FontStyle.Bold | FontStyle.Italic | FontStyle.Strikeout | FontStyle.Underline);
                        if (!model.italic)//没有斜体
                        {//删除字体中的斜体样式
                            font = new Font(font, font.Style & ~FontStyle.Italic);
                        }
                        if (!model.bold)//没有粗体
                        {//删除字体中的粗体
                            font = new Font(font, font.Style & ~FontStyle.Bold);
                        }
                        if (!model.strikeout)//没有删除线
                        {//删除字体中的删除线
                            font = new Font(font, font.Style & ~FontStyle.Strikeout);
                        }
                        if (!model.underline)//没有下划线
                        {//删除字体中的下划线
                            font = new Font(font, font.Style & ~FontStyle.Underline);
                        }
                        //这样就得到原来保存的字体样式了,有点麻烦……
                        lbl.Font = font;

说明:
  FontStyle就是一个二进制数。

   假设第一位是斜体,即000001为斜体,第二位是粗体,即000010为粗体,000011又粗又斜。
首先对表示粗体的000010取反,就成了111101,然后再与某个Style进行按位与,按位与的特点就是如果前后两个二进制数的某一位都为1,结果才为1,很显然可以看出来,任何数与111101进行按位与的话,结果就是把第二位置0,这样就达到了取消黑体的效果。

 

 

-------------

纯文本来存对象,这种思路不大好。那只能字符串拆分,逐个判断构造Font需要的参数。
二进制方式存对象比这个方便多了。

//序列化

FileStream fs=new FileStream(@"F:\sys.cfg");

BinaryFormatter bf = new BinaryFormatter();

bf.Serialize(fs, this.Font);

fs.Close();

//反序列化

FileStream fs = new FileStream(@"F:\sys.cfg");

BinaryFormatter bf = new BinaryFormatter();

Font font= bf.Deserialize(fs);

fs.Close();

 

 

-------

还有一种比较方便的,保存字体为文本时不是ToString()方式,用转换器来保存。

 

FontConverter fc = new FontConverter();

//转换为文本形式

string strf= fc.ConvertToInvariantString(button1.Font);

Console.WriteLine("字符形式的Font:" + strf);

//从文本到字体

Font f = (Font)fc.ConvertFromString(strf);

button2.Font = f;

 

 

----------------

保存font的字符串都是如下:

   [Font:   Name=宋体,   Size=9,   Units=3,   GdiCharSet=134,   GdiVerticalFont=False]

public Font(

   string familyName,

   float emSize,

   FontStyle style,

   GraphicsUnit unit,

   byte gdiCharSet,

   bool gdiVerticalFont

)

参数中的style是FontStyle枚举:
Regular    普通文本。
Bold        加粗文本。
Italic        倾斜文本。
Underline 带下划线的文本。
Strikeout 中间有直线通过的文本。 

Unit是GraphicsUnit类型的枚举:
World 将世界坐标系单位指定为度量单位。
Display 指定显示设备的度量单位。通常,视频显示使用的单位是像素;打印机使用的单位是 1/100 英寸。
Pixel 将设备像素指定为度量单位。
Point 将打印机点(1/72 英寸)指定为度量单位。
Inch 将英寸指定为度量单位。
Document 将文档单位(1/300 英寸)指定为度量单位。
Millimeter 将毫米指定为度量单位。 

 

 

 

ini文件font问题
[IndexProperty]
font=[Font: Name=宋体, Size=9, Units=3, GdiCharSet=1, GdiVerticalFont=False] //ini文件 字符串存储

通过fontdialog产生一个font.tostring(),产生font字符串.

static public Font GetFont(string strFont)
        {
            //string s = "[Font: Name=宋体, Size=9, Units=3, GdiCharSet=1, GdiVerticalFont=False]";


            string strMatch =
  @"^\[Font: Name=(?<name>[^,]+), Size=(?<size>\d+), Units=(?<units>\d+), " +
  @"GdiCharSet=(?<charSet>\d+), GdiVerticalFont=(?<verticalFont>\w+)]";

            Match match = Regex.Match(strFont, strMatch);
            

            Font FONT = new Font(match.Result("${name}"),
                float.Parse(match.Result("${size}")), FontStyle.Regular,
                  (GraphicsUnit)int.Parse(match.Result("${units}")),
                  byte.Parse(match.Result("${charSet}")),
                  bool.Parse(match.Result("${verticalFont}")));
            return FONT;
        }
    }
字体颜色或者颜色的存储
//颜色转字符串
string c1 = System.Drawing.ColorTranslator.ToHtml(System.Drawing.Color.Red); 
//字符串转
System.Drawing.Color c2 = System.Drawing.ColorTranslator.FromHtml("#ff0000");