我用C#写了一个记事本,在菜单上有字体,背景,字体颜色设置,当关闭记事本再次打开时记事本文本框中默认使用上次的设置
我首先保存到文件后在加载
//保存设置
Font font = this.txtNotePad.Font;
string fontColor = this.txtNotePad.ForeColor.ToString();
//this.BackColor = this.txtNotePad.BackColor;
string backColor = this.txtNotePad.BackColor.ToString();
string set = font + "*" + fontColor + "*" + backColor;
string family = font.Name.ToString();
string size = font.Size.ToString();
float unit = (float)font.Unit; 
float diCharSet = font.GdiCharSet;
bool gdiVerticalFont = font.GdiVerticalFont;
string windowText = this.txtNotePad.ForeColor.ToString();


FileStream file = new FileStream(set,FileMode.Create,FileAccess.Write,FileShare.None);
StreamWriter writer = new StreamWriter(file,Encoding.Default);

File.WriteAllText(Application.StartupPath + "\\set.txt", font.ToString(), Encoding.Default);//加载保存的设置
private void NotePadFrm_Load(object sender, EventArgs e) {
string file = File.ReadAllText(Application.StartupPath+"\\set.txt",Encoding.Default);
string [] set = file.Split('*');
string font = set[0];

string fontColor = set[1];
string backColor = set[2];

}
    
private void tsmiFont_Click(object sender, EventArgs e) {
if(DialogResult.OK == fontDialog.ShowDialog())
{
this.txtNotePad.Font = fontDialog.Font;
}

System.Windows.Forms.FontDialog fd = new FontDialog();
if (fd.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
this.txtNotePad.Font = fd.Font;
}
 
}但是我发现保存的设置中Font中的参数很多,加载时读取后转化成Font类型变的很难麻烦哪位高手帮忙解决一下.....
感激不尽

解决方案 »

  1.   

    试试:
    byte[] bytes = new byte[92];
    this.textBox1.Font.ToLogFont(bytes);
    保存这92个bytes读取时:
    this.textBox1.Font = System.Drawing.Font.FromLogFont(bytes);
      

  2.   

    byte[] bytes = new byte[92];
    this.textBox1.Font.ToLogFont(bytes);
    保存这92个bytes读取时:
    this.textBox1.Font = System.Drawing.Font.FromLogFont(bytes);
    读取的好像也只有45个字节的byte呀你这个保存的好像是乱码呀(?      送    ?爀椀愀氀  ?漀渀漀琀礀瀀攀??爀椀愀氀?刀攀最甀氀愀爀?嘀攀)
    //保存设置
    byte[] bytes = new byte[92];
    this.txtNotePad.Font.ToLogFont(bytes);
    FileStream file = new FileStrea
    (Application.StartupPath+"\\set.txt", FileMode.Create, FileAccess.Write, FileShare.None);
    file.Write(bytes,0,bytes.Length);
    file.Flush();//加载保存的设置
    private void NotePadFrm_Load(object sender, EventArgs e) {
    byte[] bytes = Encoding.ASCII.GetBytes(File.ReadAllText(Application.StartupPath + "\\set.txt", Encoding.Default));
    this.txtNotePad.Font = System.Drawing.Font.FromLogFont(bytes);
    } 我在调试时报了一个异常:未处理ArgumentException异常
    错误提示:只支持 TrueType 字体。这不是 TrueType 字体。麻烦能讲的更仔细些吗?