RichTextbox 操作

本文介绍了如何使用C#处理富文本数据,包括将其转换为数组以便存储,并从数组中还原内容。文中还详细说明了如何通过命令实现常见的编辑功能如复制、剪切等,并提供了设置字体颜色、大小及样式的具体方法。
</pre><p>1. 将内容转换为数组</p><pre name="code" class="csharp">FlowDocument fd = richTextBox1.Document;
System.IO.Stream s = new System.IO.MemoryStream();
System.Windows.Markup.XamlWriter.Save(fd, s);
byte[] data = new byte[s.Length];
s.Position = 0;
s.Read(data, 0, data.Length);s.Close();


2. 从数组中加载内容

System.IO.Stream news = new System.IO.MemoryStream(data);
FlowDocument newfd = System.Windows.Markup.XamlReader.Load(news) as FlowDocument;
richTextBox2.Document = newfd;

3. 命令

复制:   ToolBarCopy.Command = System.Windows.Input.ApplicationCommands.Copy;
 剪切:   toolBarCut.Command = System.Windows.Input.ApplicationCommands.Cut;
 粘贴:   ToolBarPaste.Command = System.Windows.Input.ApplicationCommands.Paste;
 撤销:   ToolBarUndo.Command = System.Windows.Input.ApplicationCommands.Undo;
 复原:   ToolBarRedo.Command = System.Windows.Input.ApplicationCommands.Redo;
 文字居中:  toolBarContentCenter.Command = System.Windows.Documents.EditingCommands.AlignCenter;
 文字居右:  toolBarContentRight.Command = System.Windows.Documents.EditingCommands.AlignRight;
 文字居左:  toolBarContentLeft.Command = System.Windows.Documents.EditingCommands.AlignLeft;
 有序排列:  ToolBarNumbering.Command = System.Windows.Documents.EditingCommands.ToggleNumbering;
 无序排列:  ToolBarBullets.Command = System.Windows.Documents.EditingCommands.ToggleBullets;

在实际使用时发现只这样操作的话,按钮无法点击,需将按钮放在toolbar里才可。


4. 设置颜色

System.Windows.Media.Color clr =
System.Windows.Media.Color.FromArgb(BkColor.A,BkColor.R,BkColor.G,BkColor.B);
richTextBox1.Selection.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(clr));

5. 设置字体

richTextBox1.Selection.ApplyPropertyValue(TextElement.FontFamilyProperty,SelFnt);

6. 设置大小

richTextBox1.Selection.ApplyPropertyValue(TextElement.FontSizeProperty, isize.ToString());
isize是整型
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值