RichTextBox 图文混排

guohelv0519 2012-08-24 02:28:40
现需要在 RichTextBox 控件中设置任意字符串片段的格式(字体、色彩和大小),并实现图文混排。

请注意:

1. 文字与图片是由程序动态添加的,而不是对被选定的文字内容进行格式设置。对于简单字体格式设置,只要通过设置控件的
ForeColor 和 Font 属性即可。

2. 我尝试在控件中先添加图片,代码片段如下:


byte[] buffer = new System.Net.WebClient().DownloadData("http://172.7.43.111/serviceonline/icons/Face/0.gif");
MemoryStream ms = new MemoryStream(buffer);
System.Drawing.Image bmp = Bitmap.FromStream(ms);
Clipboard.SetDataObject(bmp);
DataFormats.Format myFormat = DataFormats.GetFormat(DataFormats.Bitmap);

if (richTextBox1.CanPaste(myFormat))
{
richTextBox1.Paste(myFormat);
}

此时可以显示图片。

但随后需要添加文字内容,代码片段如下:

richTextBox1.Rtf += " 这些是测试内容......."


此时还只是看到原来的图片,文字内容并没有出现
...全文
335 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
songjinxi123 2012-08-28
  • 打赏
  • 举报
回复
你如果要用其他的格式,就用你自己的这个方法啊
if (richTextBox1.CanPaste(myFormat))
{
richTextBox1.Paste(myFormat);
}
songjinxi123 2012-08-28
  • 打赏
  • 举报
回复
什么叫任意设置格式?
Marky 2012-08-25
  • 打赏
  • 举报
回复

//搜索指定内容文字
richTextBox1.Find("这是测试内容。。。", RichTextBoxFinds.MatchCase);

//设置选定内容的字体和颜色
richTextBox1.SelectionFont = new Font("Verdana", 12, FontStyle.Bold);
richTextBox1.SelectionColor = Color.Red;

//设置选定内容的段落设置为项目符号列表格式
richTextBox1.SelectionBullet = true;

//设置选定内容的段落缩进
richTextBox1.SelectionIndent = 8;
richTextBox1.SelectionHangingIndent = 3;
richTextBox1.SelectionRightIndent = 12;

还有很多方法,不一一列举了,自己试吧。试不出来请msdn
Marky 2012-08-25
  • 打赏
  • 举报
回复

//搜索指定内容文字
richTextBox1.Find("这是测试内容。。。", RichTextBoxFinds.MatchCase);

//设置选定内容的字体和颜色
richTextBox1.SelectionFont = new Font("Verdana", 12, FontStyle.Bold);
richTextBox1.SelectionColor = Color.Red;

//设置选定内容的段落设置为项目符号列表格式
richTextBox1.SelectionBullet = true;

//设置选定内容的段落缩进
richTextBox1.SelectionIndent = 8;
richTextBox1.SelectionHangingIndent = 3;
richTextBox1.SelectionRightIndent = 12;

还有很多方法,不一一列举了,自己试吧。试不出来请msdn
Marky 2012-08-25
  • 打赏
  • 举报
回复

//搜索指定内容文字
richTextBox1.Find("这是测试内容。。。", RichTextBoxFinds.MatchCase);

//设置选定内容的字体和颜色
richTextBox1.SelectionFont = new Font("Verdana", 12, FontStyle.Bold);
richTextBox1.SelectionColor = Color.Red;

//设置选定内容的段落设置为项目符号列表格式
richTextBox1.SelectionBullet = true;

//设置选定内容的段落缩进
richTextBox1.SelectionIndent = 8;
richTextBox1.SelectionHangingIndent = 3;
richTextBox1.SelectionRightIndent = 12;

还有很多方法,不一一列举了,自己试吧。试不出来请msdn
guohelv0519 2012-08-24
  • 打赏
  • 举报
回复
AppendText 方法仅仅是将文字附加到控件,怎么任意设置格式?
songjinxi123 2012-08-24
  • 打赏
  • 举报
回复
richTextBox1.AppendText("这是测试内容。。。。");
Marky 2012-08-24
  • 打赏
  • 举报
回复
richTextBox1.AppendText("这些是测试内容.......");

110,561

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧