標籤:
運行效果:
代碼:
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms;10 11 namespace FreeNotes12 {13 public partial class NotePad : Form14 {15 public NotePad()16 {17 InitializeComponent();18 }19 20 /// <summary>21 /// 開啟檔案事件22 /// </summary>23 private void btn_Open_Click(object sender, EventArgs e)24 {25 OpenFileDialog of = new OpenFileDialog();26 27 of.DefaultExt = "*.rtf";28 29 of.Filter = "rtf檔案(*.rtf)|*.rtf|所有檔案(*.*)|*.*";30 31 if (of.ShowDialog() == DialogResult.OK && of.FileName.Length > 0)32 {33 richTextBox1.LoadFile(of.FileName, RichTextBoxStreamType.RichText);34 }35 }36 37 /// <summary>38 /// 儲存檔案事件39 /// </summary>40 private void btn_Save_Click(object sender, EventArgs e)41 {42 SaveFileDialog sa = new SaveFileDialog();43 44 sa.Title = "儲存";45 46 sa.FileName = "*.rtf";47 48 sa.Filter = "rtf檔案(*.rtf)|*.rtf|所有檔案(*.*)|*.*";49 50 sa.DefaultExt = "*.rtf";51 52 if (sa.ShowDialog() == DialogResult.OK && sa.FileName.Length > 0)53 {54 richTextBox1.SaveFile(sa.FileName, RichTextBoxStreamType.RichText);55 }56 }57 58 /// <summary>59 /// 字型事件60 /// </summary>61 private void btn_Font_Click(object sender, EventArgs e)62 {63 FontDialog fda = new FontDialog();64 65 fda.ShowColor = true;66 67 if (fda.ShowDialog() != DialogResult.Cancel)68 {69 richTextBox1.SelectionFont = fda.Font;70 richTextBox1.SelectionColor = fda.Color;71 }72 }73 74 /// <summary>75 /// 清空文本事件76 /// </summary>77 private void btn_Clear_Click(object sender, EventArgs e)78 {79 richTextBox1.Clear();80 }81 }82 }
WinForm 小程式 NotePad