`
wangleifire
  • 浏览: 498616 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

WPF实现RichTextBox插入图片及调整行距

    博客分类:
  • c#
阅读更多

WPF里面虽然很多形式上跟Winform一样,但是控件的使用上面还是会有很多诧异。RichTextBox就是一个例子,是的,在WPF里面对这个控件可以做很多Winform很难做的效果出来。

比如在对RichTextBox插入图片,winform时代除了用复制粘贴这种借助剪贴板的差劲方法之外就是要重写和自定义RichTextBox控件了。这就需要高超的编程能力了。但在WPF里面,只需要加几个代码就能搞定了。

在XAML里面添加图片到RichTextBox可以如下所示:

<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> ​ ​ ​ ​ ​ ​ ​ < R​i​c​h​T​e​x​t​B​o​x​ H​o​r​i​z​o​n​t​a​l​A​l​i​g​n​m​e​n​t =​"​L​e​f​t​" ​M​a​r​g​i​n =​"​9​0​,​1​2​,​0​,​0​" ​N​a​m​e =​"​r​i​c​h​T​e​x​t​B​o​x​1​" > ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ < R​i​c​h​T​e​x​t​B​o​x​.​D​o​c​u​m​e​n​t > ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ < F​l​o​w​D​o​c​u​m​e​n​t​ F​o​c​u​s​a​b​l​e =​"​T​r​u​e​" ​L​i​n​e​H​e​i​g​h​t =​"​5​" > ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ < P​a​r​a​g​r​a​p​h​ x​:​N​a​m​e =​"​g​a​r​a​" > ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​文​字​区​域​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ < I​m​a​g​e​ S​o​u​r​c​e =​"​D​:​\​1​3​4​2​8​9​2​_​1​0​.​j​p​g​" ​F​o​c​u​s​a​b​l​e =​"​T​r​u​e​" ​H​e​i​g​h​t =​"​5​0​" ​S​t​r​e​t​c​h =​"​U​n​i​f​o​r​m​" /​> ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​文​字​区​域​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ < R​u​n​ T​e​x​t =​"​文​字​区​域​文​字​区​域​" >​<​/ R​u​n > ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ < R​u​n​ T​e​x​t =​"​文​字​区​域​" >​<​/ R​u​n > ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ <​/ P​a​r​a​g​r​a​p​h > ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ < P​a​r​a​g​r​a​p​h​ x​:​N​a​m​e =​"​g​a​r​a​1​" > ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ < R​u​n​ T​e​x​t =​"​文​字​区​域​" >​<​/ R​u​n > ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ < R​u​n​ T​e​x​t =​"​文​字​区​域​" >​<​/ R​u​n > ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ <​/ P​a​r​a​g​r​a​p​h > ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ <​/ F​l​o​w​D​o​c​u​m​e​n​t > ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ <​/ R​i​c​h​T​e​x​t​B​o​x​.​D​o​c​u​m​e​n​t > ​ ​ ​ ​ ​ ​ ​ <​/ R​i​c​h​T​e​x​t​B​o​x >

这样就往控件里面添加了图片了。

备注:FlowDocument里面的LineHeight 属性是文字段落的间距。默认间距很大,所以这里调整一下!

当然,这样未必能够完全满足要求,因为有时候我们需要在程序运行的时候点击按钮选取图片进行添加。代码如下:

<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ -->p​r​i​v​a​t​e v​o​i​d ​A​d​d​J​P​G​_​C​l​i​c​k​( o​b​j​e​c​t ​s​e​n​d​e​r​,​ ​R​o​u​t​e​d​E​v​e​n​t​A​r​g​s​ ​e​)​ ​ ​ ​ ​ ​ ​ ​ ​{​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ s​t​r​i​n​g ​f​i​l​e​p​a​t​h​ = "​" ;​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ s​t​r​i​n​g ​f​i​l​e​n​a​m​e​ = "​" ;​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​O​p​e​n​F​i​l​e​D​i​a​l​o​g​ ​o​p​e​n​f​i​l​e​j​p​g​ = n​e​w ​O​p​e​n​F​i​l​e​D​i​a​l​o​g​(​)​;​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​o​p​e​n​f​i​l​e​j​p​g​.​F​i​l​t​e​r​ = " j​p​g​图​片​(​*​.​j​p​g​)​|​*​.​j​p​g​|​g​i​f​图​片​(​*​.​g​i​f​)​|​*​.​g​i​f " ;​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​o​p​e​n​f​i​l​e​j​p​g​.​F​i​l​t​e​r​I​n​d​e​x​ = 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); // 插入图片到选定位置 } }
分享到:
评论
1 楼 raze 2010-04-22  
你好,最近在研究CC与CPPUNIT集成的事情,基本上已经解决了编译和构建的问题,现在就只剩下两个问题了:
  (1)故意在测试的VC工程里面做了一个小错误,导致编译失败,但是编译失败的时候,CC仍然提示:
    [exec] Result: 1
2010-04-09 22:26:46,614 [Thread-30] INFO  ScriptRunner     -
2010-04-09 22:26:46,614 [Thread-30] INFO  ScriptRunner     - BUILD SUCCESSFUL
    这也导致编译后在dashboard中显示每次编译都是绿色成功的。

   (2)执行单元测试程序后,输出的内容无法显示到dashboard中,不知道你是怎么解决的?
   看你的文章好像说的不是很清楚。无论是把CPPUNIT按照编译输出还是XML输出都无法显示。
   我的邮箱: yan.jun2@zte.com.cn  非常感谢!

相关推荐

Global site tag (gtag.js) - Google Analytics