最近一段时间一直在做关于文本编辑的东西,发现有篇好文章,原文:http://blog.csdn.net/whitechololate/archive/2010/07/27/5769111.aspx
/// <summary>
/// 加载右键菜单内容
/// </summary>
/// <param name="colorList"></param>
private void GetMenuMsg(ArrayList colorList)
{
ContextMenu cMenu = new ContextMenu();
richTextBox1.ContextMenu = cMenu;
MenuItem mItem = new MenuItem();
mItem.Command = ApplicationCommands.Cut;
cMenu.Items.Add(mItem);
MenuItem mItem1 = new MenuItem();
mItem1.Command = ApplicationCommands.Copy;
cMenu.Items.Add(mItem1);
MenuItem mItem2 = new MenuItem();
mItem2.Command = ApplicationCommands.Paste; //粘贴
cMenu.Items.Add(mItem2);
MenuItem mItem3 = new MenuItem();
mItem3.Command = ApplicationCommands.SelectAll;
cMenu.Items.Add(mItem3);
if (colorList.Count >0)
{
for (int i = 0; i < colorList.Count; i++)
{
string clo = string.Empty;
clo = colorList[i].ToString();
switch (clo)
{
case "1":
{
MenuItem redItem = new MenuItem();
redItem.Header = "红色";
cMenu.Items.Add(redItem);
redItem.PreviewMouseDown += new MouseButtonEventHandler(redItem_PreviewMouseDown);
}
break;
case "2":
{
MenuItem buleItem = new MenuItem();
buleItem.Header = "蓝色";
cMenu.Items.Add(buleItem);
buleItem.PreviewMouseDown += new MouseButtonEventHandler(buleItem_PreviewMouseDown);
}
break;
case "3":
{
MenuItem yellowItem = new MenuItem();
yellowItem.Header = "黄色";
cMenu.Items.Add(yellowItem);
yellowItem.PreviewMouseDown += new MouseButtonEventHandler(yellowItem_PreviewMouseDown);
}
break;
case "4":
{
MenuItem blackItem = new MenuItem();
blackItem.Header = "黑色";
cMenu.Items.Add(blackItem);
blackItem.PreviewMouseDown += new MouseButtonEventHandler(blackItem_PreviewMouseDown);
}
break;
}
}
}
}
void blackItem_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
richTextBox1.Selection.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Black);
}
private void redItem_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
richTextBox1.Selection.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Red);
}
private void buleItem_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
richTextBox1.Selection.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Blue);
}
private void yellowItem_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
richTextBox1.Selection.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Yellow);
}
/// <summary>
/// 获取每个元素信息 /// </summary>
/// <param name="tpoint">起始点</param>
/// <param name="nePoint">结束点</param>
private void GetMeg( TextPointer tpoint,TextPointer nePoint)
{
TextRange trange = new TextRange(tpoint, nePoint);
//本元素前字体颜色值
SolidColorBrush color = (SolidColorBrush)trange.GetPropertyValue(TextElement.ForegroundProperty);
doText = trange.Text;
if (color == Brushes.Blue)
{
myColor = "Blue";
}
else if(color == Brushes.Yellow)
{
myColor = "Yellow";
}
else if(color == Brushes.Red)
{
myColor = "Red";
}
else if (color == Brushes.Black)
{
myColor = "Black";
}
else
{
myColor = "Black";
}
if (doText != string.Empty)
{//遍历text内容(位置,字体颜色,显示内容),把相关信息存入到数组中
msgCount++;
tempV = msgCount + "~~~~~" + myColor + "~~~~~" + doText;
textAr.Add(tempV);
}
TextPointer nextPoint = tpoint.GetNextInsertionPosition(LogicalDirection.Forward);//得到下一个元素点
if (nextPoint != richTextBox1.Document.ContentEnd && nextPoint != null)
{
GetMeg(nePoint,nextPoint);
}
}
private static void SaveFile(string filename, RichTextBox richTextBox)
{ //SaveFile("d:\\test.xaml", richTextBox1);
if (string.IsNullOrEmpty(filename))
{
throw new ArgumentNullException();
}
using (FileStream stream = File.OpenWrite(filename))
{
TextRange documentTextRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
string dataFormat = DataFormats.Text;
string ext = System.IO.Path.GetExtension(filename);
if (String.Compare(ext, ".xaml", true) == 0)
{
dataFormat = DataFormats.Xaml;
}
else if (String.Compare(ext, ".rtf", true) == 0)
{
dataFormat = DataFormats.Rtf;
}
documentTextRange.Save(stream, dataFormat);
}
}
private static void LoadFile(string filename, RichTextBox richTextBox)
{//LoadFile("d:\\test.xaml", richTextBox1);
if (string.IsNullOrEmpty(filename)) {
throw new ArgumentNullException();
}
if (!File.Exists(filename)) {
throw new FileNotFoundException();
}
using (FileStream stream = File.OpenRead(filename)) {
TextRange documentTextRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
string dataFormat = DataFormats.Text;
string ext = System.IO.Path.GetExtension(filename);
if (String.Compare(ext, ".xaml",true) == 0) {
dataFormat = DataFormats.Xaml;
}
else if (String.Compare(ext, ".rtf", true) == 0) {
dataFormat = DataFormats.Rtf;
}
documentTextRange.Load(stream, dataFormat);
}
}