简易记事本

目录

一 设计原型

二 实现源码


一 设计原型

操作效果:

二 实现源码

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;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值