目录
一 设计原型
操作效果:
二 实现源码
using Newtonsoft.Json;
namespace 简易记事本
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private string NotePath = Directory.GetCurrentDirectory() + "\\Note.txt";
private string NoteStyle = Directory.GetCurrentDirectory() + "\\NoteStyle.json";
private NoteStyleClass noteStyleClass = new NoteStyleClass();
private void font_Click(object sender, EventArgs e)
{
DialogResult result = fontDialog1.ShowDialog();
if (result == DialogResult.OK)
{
Note.Font = fontDialog1.Font;
noteStyleClass.Font = fontDialog1.Font;
}
}
private void fontColor_Click(object sender, EventArgs e)
{
DialogResult result = colorDialog1.ShowDialog();
if (result == DialogResult.OK)
{
Note.ForeColor = colorDialog1.Color;
noteStyleClass.Color = colorDialog1.Color;
}
}
private void save_Click(object sender, EventArgs e)
{
File.WriteAllText(NotePath, Note.Text);
File.WriteAllText(NoteStyle, JsonConvert.SerializeObject(noteStyleClass));
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
Note.Text = File.ReadAllText(NotePath);
noteStyleClass = JsonConvert.DeserializeObject<NoteStyleClass>(File.ReadAllText(NoteStyle));
if (noteStyleClass != null)
{
Note.ForeColor = noteStyleClass.Color;
Note.Font = noteStyleClass.Font;
}
}
catch (Exception)
{
}
}
}
public class NoteStyleClass
{
public Font Font { get; set; }
public Color Color { get; set; }
}
}
设计器自动生成代码:
namespace 简易记事本
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
Note = new RichTextBox();
font = new Button();
fontColor = new Button();
save = new Button();
fontDialog1 = new FontDialog();
colorDialog1 = new ColorDialog();
SuspendLayout();
//
// Note
//
Note.Location = new Point(3, 9);
Note.Name = "Note";
Note.Size = new Size(630, 581);
Note.TabIndex = 0;
Note.Text = "";
//
// font
//
font.Location = new Point(3, 596);
font.Name = "font";
font.Size = new Size(119, 29);
font.TabIndex = 1;
font.Text = "设置字体样式";
font.UseVisualStyleBackColor = true;
font.Click += font_Click;
//
// fontColor
//
fontColor.Location = new Point(153, 596);
fontColor.Name = "fontColor";
fontColor.Size = new Size(120, 29);
fontColor.TabIndex = 2;
fontColor.Text = "设置字体颜色";
fontColor.UseVisualStyleBackColor = true;
fontColor.Click += fontColor_Click;
//
// save
//
save.Location = new Point(539, 596);
save.Name = "save";
save.Size = new Size(94, 29);
save.TabIndex = 3;
save.Text = "保存";
save.UseVisualStyleBackColor = true;
save.Click += save_Click;
//
// Form1
//
AutoScaleDimensions = new SizeF(9F, 20F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(637, 630);
Controls.Add(save);
Controls.Add(fontColor);
Controls.Add(font);
Controls.Add(Note);
MaximizeBox = false;
MinimizeBox = false;
Name = "Form1";
Text = "简易记事本";
Load += Form1_Load;
ResumeLayout(false);
}
#endregion
private RichTextBox Note;
private Button font;
private Button fontColor;
private Button save;
private FontDialog fontDialog1;
private ColorDialog colorDialog1;
}
}