RichTextBox的使用
WPF里面虽然很多形式上跟Winform一样,但是控件的使用上面还是会有很多诧异。RichTextBox就是一个例子,是的,在WPF里面对这个控件可以做很多Winform很难做的效果出来。
比如在对RichTextBox插入图片,winform时代除了用复制粘贴这种借助剪贴板的差劲方法之外就是要重写和自定义RichTextBox控件了。这就需要高超的编程能力了。但在WPF里面,只需要加几个代码就能搞定了。
在XAML里面添加图片到RichTextBox可以如下所示:
<RichTextBox HorizontalAlignment="Left" Margin="90,12,0,0" Name="richTextBox1">
<RichTextBox.Document>
<FlowDocument Focusable="True" LineHeight="5">
<Paragraph x:Name="gara">
文字区域
<Image Source="D:\1342892_10.jpg" Focusable="True" Height="50" Stretch="Uniform" />
文字区域
<Run Text="文字区域文字区域"></Run>
<Run Text="文字区域"></Run>
</Paragraph>
<Paragraph x:Name="gara1">
<Run Text="文字区域"></Run>
<Run Text="文字区域"></Run>
</Paragraph>
</FlowDocument>
</RichTextBox.Document>
</RichTextBox>
这样就往控件里面添加了图片了。
备注:FlowDocument里面的LineHeight 属性是文字段落的间距。默认间距很大,所以这里调整一下!
当然,这样未必能够完全满足要求,因为有时候我们需要在程序运行的时候点击按钮选取图片进行添加。代码如下:
private void AddJPG_Click(object sender, RoutedEventArgs e)
{
string filepath = "";
string filename = "";
OpenFileDialog openfilejpg = new OpenFileDialog();
openfilejpg.Filter = "jpg图片(*.jpg)|*.jpg|gif图片(*.gif)|*.gif";
openfilejpg.FilterIndex = 0;
openfilejpg.RestoreDirectory = true;
openfilejpg.Multiselect = false;
if (openfilejpg.ShowDialog() == true)
{
filepath = openfilejpg.FileName;
Image img = new Image();
BitmapImage bImg = new BitmapImage();
img.IsEnabled = true;
bImg.BeginInit();
bImg.UriSource = new Uri(filepath, UriKind.Relative);
bImg.EndInit();
img.Source = bImg;
//MessageBox.Show(bImg.Width.ToString() + "," + bImg.Height.ToString());
/* 调整图片大小
if (bImg.Height > 100 || bImg.Width > 100)
{
img.Height = bImg.Height * 0.2;
img.Width = bImg.Width * 0.2;
}*/
img.Stretch = Stretch.Uniform; //图片缩放模式
new InlineUIContainer(img, richTextBox1.Selection.Start); //插入图片到选定位置
}
}
这样就插入了一张图片到RichTextBox里了,是不是很简单呢!
原文在此:http://blogs.msdn.com/jfoscoding/archive/2006/01/14/512825.aspx
这里仅整理出其中的知识点:
1. 取得已被选中的内容:
(1)使用 RichTextBox.Document.Selection属性
(2)访问RichTextBox.Document.Blocks属性的“blocks”中的Text
2. 在XAML中增加内容给RichTextBox:
<RichTextBox IsSpellCheckEnabled="True">
<FlowDocument>
<Paragraph>
<!-- 这里加上你的内容 -->
This is a richTextBox. I can <Bold>Bold</Bold>, <Italic>Italicize</Italic>, <Hyperlink>Hyperlink stuff</Hyperlink> right in my document.
</Paragraph>
</FlowDocument>
</RichTextBox>
3. 缩短段间距,类似<BR>,而不是<P>
方法是使用Style定义段间距:
<RichTextBox>
<RichTextBox.Resources>
<Style TargetType="{x:Type Paragraph}">
<Setter Property="Margin" Value="0"/>
</Style>
</RichTextBox.Resources>
<FlowDocument>
<Paragraph>
This is my first paragraph... see how there is...
</Paragraph>
<Paragraph>
a no space anymore between it and the second paragraph?
</Paragraph>
</FlowDocument>
</RichTextBox>
{
richTextBox.Document.Blocks.Clear();
using (StreamReader streamReader = File.OpenText(filename)) {
Paragraph paragraph = new Paragraph();
paragraph.Text = streamReader.ReadToEnd();
richTextBox.Document.Blocks.Add(paragraph);
}
}
private void LoadText(RichTextBox richTextBox, string txtContent)
{
richTextBox.Document.Blocks.Clear();
Paragraph paragraph = new Paragraph();
paragraph.Text = txtContent;
richTextBox.Document.Blocks.Add(paragraph);
}
private string GetText(RichTextBox richTextBox)
{
TextRange textRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
return textRange.Text;
}
{
if (string.IsNullOrEmpty(rtf)) {
throw new ArgumentNullException();
}
TextRange textRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
using (MemoryStream rtfMemoryStream = new MemoryStream()) {
using (StreamWriter rtfStreamWriter = new StreamWriter(rtfMemoryStream)) {
rtfStreamWriter.Write(rtf);
rtfStreamWriter.Flush();
rtfMemoryStream.Seek(0, SeekOrigin.Begin);
textRange.Load(rtfMemoryStream, DataFormats.Rtf);
}
}
}
private static void LoadFile(string filename, RichTextBox richTextBox)
{
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);
}
}
private static void SaveFile(string filename, RichTextBox richTextBox)
{
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);
}
}
<!-- Window1.xaml -->
<DockPanel>
<Menu DockPanel.Dock="Top">
<MenuItem Header="_File">
<MenuItem Header="_Open File" Click="OnOpenFile"/>
<MenuItem Header="_Save" Click="OnSaveFile"/>
<Separator/>
<MenuItem Header="E_xit" Click="OnExit"/>
</MenuItem>
</Menu>
<RichTextBox Name="richTextBox1"></RichTextBox>
</DockPanel>
private void OnExit(object sender, EventArgs e) {
this.Close();
}
private void OnOpenFile(object sender, EventArgs e) {
Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog();
ofd.Filter = "Text Files (*.txt; *.xaml; *.rtf)|*.txt;*.xaml;*.rtf";
ofd.Multiselect = false;
if (ofd.ShowDialog() == true) {
LoadFile(ofd.SafeFileName, richTextBox1);
}
private void OnSaveFile(object sender, EventArgs e) {
Microsoft.Win32.SaveFileDialog sfd = new Microsoft.Win32.SaveFileDialog();
sfd.Filter = "Text Files (*.txt; *.xaml; *.rtf)|*.txt;*.xaml;*.rtf";
if (sfd.ShowDialog() == true) {
SaveFile(sfd.SafeFileName, richTextBox1);
}
}
转载于:https://www.cnblogs.com/jhxk/articles/2281119.html
RichTextBox的使用相关推荐
- WPF初探--RichTextBox
1. 设置RichTextBox运行换行 将AcceptReturn属性设置为true 2. 保存RichTextBox内容到文件 //path为完整保存路径名 private ...
- C# richtextbox 自动下拉到最后 方法 RichTextBox读取txt中文后出现乱码
C# richtextbox 自动滚动到最后 光标到最后 自动显示最后一行 private void richTextBox1_TextChanged(object sender, EventArg ...
- 在silverlight中通过对话框把选择的图片插入到RichTextBox中
主要是通过OpenFileDialog打开一个对话框,然后把选择的图片插入到RichTextBox中,具体代码如下 OpenFileDialog open = new OpenFileDialog() ...
- wpf richtextbox 存储到数据库并显示
做一个项目,需要图文一起存入数据库,在网上浏览了很久.终于实现了在RICHTEXTBOX里面和数据库读写图文的方法. 存入数据库. public string toxaml() { ...
- RichTextBox读写数据库
将RichTextBox的内容直接写入数据库: private void button1_Click(object sender, EventArgs e) { System.IO.Memory ...
- RichTextBox实现关键字自定义颜色显示(C#)
现在的很多编辑工具都能实现代码中关键字的高亮显示,自己一时好玩也就随便写了一个: 首先建立一个XML文件:csharp.xml <?xml version="1.0" enc ...
- RichTextBox粘贴两次以及去掉粘贴内容的格式问题
RichTextBox是一个支持RTF的文本框,但很多情况下我们并不希望用户采用快捷键(如CTRL+V)来粘贴有格式的内容,或者我们需要清除用户粘贴的内容的格式,怎么办呢? 其实非常简单: 去掉格式: ...
- WPF中读取txt文件并让其在RichTextBox中显示
出于项目的需要,本来想直接将内容写在RichTextBox中,不过考虑到灵活性,我想,不管是谁,都会想把内容写在一个文件里,然后去读取它以实现这个效果.我也是这么想的,而且这个问题怎么想都不算是个难题 ...
- Winform中使用用户控件实现带行数和标尺的RichTextBox(附代码下载)
场景 RichTextBox控件允许用户输入和编辑文本的同时提供了比普通的TextBox控件更高级的格式特征. 效果 注: 博客主页: https://blog.csdn.net/badao_lium ...
最新文章
- 儿童版「微信」要来了?
- SDUT 2860-生日Party(BFS)
- 投稿Cover Letter如何写出彩
- 【HDOJ】1597 find the nth digit
- Introducing and integrating Hibernate(Chapter 2 of Hibernate In Action)
- [Flink]Flink实时框架介绍
- 2014 - 2015
- csdn设置资源下载所需积分
- javaMD5加密生成key方法
- armeabi与armeabi-v7a的区别,绝对干货!
- 各类图像数据大集合(下载链接)
- java if case when_【SQL学习笔记4】case when 和if的用法
- Linux部署单体架构,从单体式架构迁移到微服务架构:三个策略叙述
- 如何用电脑模拟手机屏幕滑动 Total Control帮您实现
- 网络层路由选择协议(RIPOSF)
- python 代理ip池_GitHub - xuan525/proxy_pool: Python爬虫代理IP池(proxy pool)
- 2022应届校招面试总结
- 根据日期计算星期几 -- 基姆拉尔森计算公式
- 基于Java毕业设计校园社团管理平台源码+系统+mysql+lw文档+部署软件
- 警察蜀黍权威数据告诉你,广东少男最易遭受网络诈骗
热门文章
- 7 种 JVM 垃圾收集器,看完我跪了。。
- 如何在面试中介绍自己的项目经验,很重要!
- 未来我们对微服务和 Serverless 架构有什么期望
- 阅读开源源码的正确姿势建议
- Nginx 反向代理及 Cookie 相关问题
- 框架:springboot组合spring、springmvc、mybatis的一个小demo
- 3.程序的局部性原理
- Git 之fatal: remote origin already exists 错误解决办法(通俗易懂)
- jsp页面怎么调用的servlet
- Android --- textColorHint与textColor的用法介绍(包懂)