51
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication09
{
public partial class Form1 : Form
{
public void SetFont(String fontname)
{
Font ft;
textBox1.Text = richTextBox1.SelectionStart.ToString();
int start = richTextBox1.SelectionStart;
int end = richTextBox1.SelectionStart + richTextBox1.SelectionLength - 1;
for (int i = start; i <= end; i++)
{
richTextBox1.Select(i, 1);
ft = richTextBox1.SelectionFont;
ft = new Font(fontname, ft.Size, ft.Style);
richTextBox1.SelectionFont = ft;
}
richTextBox1.Select(start, end - start + 1);
richTextBox1.Focus();
}
//11111111111111111
public void SetFont(int fontsize)
{
Font ft;
textBox1.Text = richTextBox1.SelectionStart.ToString();
int start = richTextBox1.SelectionStart;
int end = richTextBox1.SelectionStart + richTextBox1.SelectionLength - 1;
for (int i = start; i <= end; i++)
{
richTextBox1.Select(i, 1);
ft = richTextBox1.SelectionFont;
ft = new Font(ft.Name, fontsize, ft.Style);
richTextBox1.SelectionFont = ft;
}
richTextBox1.Select(start, end - start + 1);
richTextBox1.Focus();
}
public void SetFont(FontStyle style, char c)
{
Font ft;
int start = richTextBox1.SelectionStart;
int end = richTextBox1.SelectionStart + richTextBox1.SelectionLength - 1;
for (int i = start; i <= end; i++)
{
richTextBox1.Select(i, 1);
ft = richTextBox1.SelectionFont;
System.Drawing.FontStyle fs = ft.Style;
if (c == '+') fs = (System.Drawing.FontStyle)(fs | style);
else fs = (System.Drawing.FontStyle)(fs - style);
if (fs.ToString().IndexOf("Strikeout") >= 0)
fs = (System.Drawing.FontStyle)(fs - FontStyle.Strikeout);
ft = new Font(ft.Name, ft.Size, fs);
richTextBox1.SelectionFont = ft;
}
richTextBox1.Select(start, end - start + 1);
richTextBox1.Focus();
}
public Form1()
{
InitializeComponent();
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
SetFont("宋体");
}
private void radioButton6_CheckedChanged(object sender, EventArgs e)
{
richTextBox1.SelectionColor = Color.Blue;
}
private void button3_Click(object sender, EventArgs e)
{
System.Drawing.FontStyle fs = FontStyle.Regular;
if (button3.FlatStyle == FlatStyle.Flat)
{
button3.FlatStyle = FlatStyle.Standard;
button3.BackColor = Color.White;
fs = System.Drawing.FontStyle.Underline ;
SetFont(fs, '-');
}
else
{
button3.FlatStyle = FlatStyle.Flat;
button3.BackColor = Color.Silver;
fs = System.Drawing.FontStyle.Underline;
SetFont(fs, '+');
}
}
private void button1_Click(object sender, EventArgs e)
{
System.Drawing.FontStyle fs = FontStyle.Regular;
if (button1.FlatStyle == FlatStyle.Flat)
{
button1.FlatStyle = FlatStyle.Standard;
button1.BackColor = Color.White;
fs = System.Drawing.FontStyle.Bold;
SetFont(fs, '-');
}
else
{
button1.FlatStyle = FlatStyle.Standard;
button1.BackColor = Color.White;
fs = System.Drawing.FontStyle.Bold;
SetFont(fs, '+');
}
}
private void button2_Click(object sender, EventArgs e)
{
System.Drawing.FontStyle fs = FontStyle.Regular;
if (button2.FlatStyle == FlatStyle.Flat)
{
button2.FlatStyle = FlatStyle.Standard;
button2.BackColor = Color.White;
fs = System.Drawing.FontStyle.Italic;
SetFont(fs, '-');
}
else
{
button2.FlatStyle = FlatStyle.Standard;
button2.BackColor = Color.White;
fs = System.Drawing.FontStyle.Italic;
SetFont(fs, '+');
}
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
SetFont("楷体_GB2312");
}
private void radioButton3_CheckedChanged(object sender, EventArgs e)
{
SetFont("隶书");
}
private void radioButton4_CheckedChanged(object sender, EventArgs e)
{
richTextBox1.SelectionColor = Color.Black;
}
private void radioButton5_CheckedChanged(object sender, EventArgs e)
{
richTextBox1.SelectionColor = Color.Red;
}
private void radioButton7_CheckedChanged(object sender, EventArgs e)
{
SetFont(13);
}
private void radioButton8_CheckedChanged(object sender, EventArgs e)
{
SetFont(18);
}
private void button4_Click(object sender, EventArgs e)
{
try
{
richTextBox1.LoadFile(textBox1.Text, RichTextBoxStreamType.RichText);
}
catch (Exception ex)
{
MessageBox.Show("打开文件出现错误:" + ex.ToString());
}
}
private void button5_Click(object sender, EventArgs e)
{
try
{
richTextBox1.SaveFile(textBox2.Text, RichTextBoxStreamType.RichText);
}
catch (Exception ex)
{
MessageBox.Show("保存文件出现错误:" + ex.ToString());
}
}
}
}