zoukankan      html  css  js  c++  java
  • WPF RichTextBox自动调整高度

        大概两年前的这个时间段,当时做项目遇到了一个问题:环境VS2005、WinForm,需要RichTextBox根据内容自动调整高度。当时用了各种方法都没能解决,后来也尝试了好几次都没能完成这个功能。这个功能成了我的一个心病。

        这一段使用Silverlight,然后学习WPF,尝试用WPF中的RichTextBox来实现这个功能,没曾想还真实现了,而且不是很复杂。

      自定义一个控件,继承System.Windows.Controls.RichTextBox。

      关键代码:

     1         private void AdjustHeight()
    2 {
    3 Rect rectStart = Document.ContentStart.GetCharacterRect(LogicalDirection.Forward);
    4 Rect rectEnd = Document.ContentEnd.GetCharacterRect(LogicalDirection.Forward);
    5 var height = rectEnd.Bottom - rectStart.Top;
    6 var remainH = rectEnd.Height / 2.0;
    7 var myHeight = Math.Min(MaxHeight, Math.Max(MinHeight, height + remainH));
    8
    9 if (myHeight > BaseHeight)
    10 {
    11 this.Height = myHeight;
    12 }
    13 }

      关键的地方是,RichTextBox中能得到第一个字和最后一个字的位置。

    获取WPF中的内容:

    string ss = new TextRange(rtbENotes.Document.ContentStart, rtb.Document.ContentEnd).Text;

    this.rtb.Document = new FlowDocument(new Paragraph(new Run(sss)));

  • 相关阅读:
    HGOI20180822 五校联考卷
    HGOI20180817 (NOIP模拟Day1 task)
    HGOI2010816 (NOIP 提高组模拟赛 day1)
    HGOI20180815 (NOIP 提高组模拟赛 day2)
    HGOI20180814 (NOIP 模拟Day1)
    HGOI20180813 (NOIP2018 提高组 Day2 模拟试题)
    小工具
    HGOI20180812 (NOIP2018 提高组 Day1 模拟试题)
    浅谈高斯消元
    浅谈线性基
  • 原文地址:https://www.cnblogs.com/sshoub/p/2272013.html
Copyright © 2011-2022 走看看