c#中怎么实现记事本中的字体对话框(就是做一个是记事本相同的对话框)

2024-11-28 06:46:12
推荐回答(1个)
回答(1):

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;
namespace 记事本
{
public partial class Form1 : Form//可以更简单一些,因为有些操作是重复了的//
{
public Form1()
{
InitializeComponent();
}

private void 复制CToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Copy();
}

private void 剪切TToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Paste();
}

private void 保存SToolStripButton_Click(object sender, EventArgs e)
{
saveFileDialog1.Filter = "文本文档.TXT|*.txt|.rtf文档|*.rtf";
saveFileDialog1.ShowDialog();
MessageBox.Show(saveFileDialog1.FileName);
try
{
if(saveFileDialog1 .ShowDialog ()==DialogResult .OK )
richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.RichText);
}
catch(Exception ex)
{
MessageBox.Show(ex .Message );
}
}

private void 打开OToolStripButton_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "文本文档.TXT|*.txt|.rtf文档|*.rtf";// Filter 文件筛选器//
openFileDialog1.ShowDialog();
MessageBox.Show(openFileDialog1 .FileName );
try
{
if(openFileDialog1 .ShowDialog ()==DialogResult .OK )
richTextBox1.LoadFile(openFileDialog1.FileName, RichTextBoxStreamType.RichText);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

private void Form1_Load(object sender, EventArgs e)
{
for (int i = 8; i < 78; i++)
{
toolStripComboBox1.Items.Add(i);
}

}

private void 新建NToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Text = "";
}

private void 新建NToolStripButton_Click(object sender, EventArgs e)
{
richTextBox1.Text = "";
}

private void 剪切UToolStripButton_Click(object sender, EventArgs e)
{
richTextBox1.Cut();
}

private void 复制CToolStripButton_Click(object sender, EventArgs e)
{
richTextBox1.Copy();
}

private void 粘贴PToolStripButton_Click(object sender, EventArgs e)
{
richTextBox1.Paste();
}

private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "文本文档.TXT|*.txt|.rtf文档|*.rtf";// Filter 文件筛选器//
openFileDialog1.ShowDialog();
MessageBox.Show(openFileDialog1.FileName);
try
{
if(openFileDialog1 .ShowDialog ()==DialogResult .OK )
richTextBox1.LoadFile(openFileDialog1.FileName, RichTextBoxStreamType.RichText);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

private void 保存SToolStripMenuItem_Click(object sender, EventArgs e)
{
saveFileDialog1.Filter = "文本文档.TXT|*.txt|.rtf文档|*.rtf";
saveFileDialog1.ShowDialog();
MessageBox.Show(saveFileDialog1.FileName);
try
{
if(saveFileDialog1 .ShowDialog ()==DialogResult .OK )
richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.RichText);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

private void 粘贴PToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Paste();
}

private void toolStripComboBox1_TextChanged(object sender, EventArgs e)
{
float f = Convert.ToSingle(toolStripComboBox1.SelectedItem.ToString());
richTextBox1.SelectionFont = new Font("字号", f);
}

private void 红色ToolStripMenuItem_Click(object sender, EventArgs e)
{

richTextBox1 .SelectionColor = Color.Red;
}

private void 绿色ToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.SelectionColor = Color.Green;
}

private void 更多ToolStripMenuItem_Click(object sender, EventArgs e)
{
colorDialog1.ShowDialog();
if(colorDialog1 .ShowDialog ()== DialogResult .OK)
richTextBox1.SelectionColor = colorDialog1.Color;
}

private void 另存为AToolStripMenuItem_Click(object sender, EventArgs e)
{
saveFileDialog1.Filter = "文本文档.TXT|*.txt|.rtf文档|*.rtf";
saveFileDialog1.ShowDialog();
MessageBox.Show(saveFileDialog1.FileName);
try
{
if(saveFileDialog1 .ShowDialog() ==DialogResult .OK )
richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.RichText);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

private void 打印PToolStripMenuItem_Click(object sender, EventArgs e)
{
printDocument1.Print();
}

private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
e.Graphics.DrawString(richTextBox1 .Text ,new Font("宋体",15),Brushes .Green ,20,20);
}

private void 关于AToolStripMenuItem_Click(object sender, EventArgs e)
{
AboutBox1 ab = new AboutBox1();
ab.Show();
}

private void timer1_Tick(object sender, EventArgs e)
{
toolStripStatusLabel1.Text = "系统时间:" + DateTime.Now.ToLocalTime();
progressBar1.PerformStep();
}

private void 工具栏ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (工具栏ToolStripMenuItem.Checked == true)
{
工具栏ToolStripMenuItem.Checked = false;
toolStrip1.Visible = false;
}
else
{
工具栏ToolStripMenuItem.Checked = true ;
toolStrip1.Visible = true ;
}

}

private void 状态栏ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (状态栏ToolStripMenuItem.Checked == true)
{
状态栏ToolStripMenuItem.Checked = false;
statusStrip1.Visible = false;
}
else
{
状态栏ToolStripMenuItem.Checked = true ;
statusStrip1.Visible = true;
}
}

private void 字体ToolStripMenuItem_Click(object sender, EventArgs e)
{

fontDialog1.ShowDialog();
fontDialog1.ShowColor = true;
if (fontDialog1.ShowDialog() == DialogResult.OK)
{
richTextBox1.SelectionFont = fontDialog1.Font;
richTextBox1.SelectionColor = fontDialog1.Color;
}
}

private void 自动换行ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (自动换行ToolStripMenuItem.Checked == false)
{
自动换行ToolStripMenuItem.Checked = true;
richTextBox1.WordWrap = true;
}
else
{
自动换行ToolStripMenuItem.Checked =false ;
richTextBox1.WordWrap = false;
}
}

}
}
//要想用运行这些代码,必须要添加必要的控件哦//