将WinForms RichTextBox自动调整到其内容

时间:2009-04-09 09:56:05

标签: winforms richtextbox autosize

有谁知道如何动态地将RichTextBox控件调整为其内容?

7 个答案:

答案 0 :(得分:8)

我想我太迟了但是看看this

这只是两个代码行:

private void rtb_ContentsResized(object sender, ContentsResizedEventArgs e)
{
    (RichTextBox)sender.Height = e.NewRectangle.Height + 5;
}

答案 1 :(得分:7)

再次假设固定字体可以执行以下操作:

using (Graphics g = CreateGraphics())
{
    richTextBox.Height = (int)g.MeasureString(richTextBox.Text,
        richTextBox.Font, richTextBox.Width).Height;
}

答案 2 :(得分:4)

这有点痛苦 - C#RichTextBox经常令人沮丧。你是否试图将盒子放大到足以容纳其内容而没有任何滚动条?

如果RichTextBox具有常量字体,则可以使用TextRenderer.MeasureText来简单地测量所需的大小,并将框的宽度作为约束传递。

ContentsResized事件为您提供ContentsResizedEventsArgs,它为您提供一个NewRectangle,它告诉您文本区域有多大。但它只会在文本发生变化时触发,如果你只是想测量一个现有的richtextbox,那就没那么有用了(虽然你可能只是做一些hacky,比如把盒子的文本设置为自己,触发这个事件)。

还有一堆Win32 api调用,比如使用EM_GETLINECOUNT(http://ryanfarley.com/blog/archive/2004/04/07/511.aspx)等。

答案 3 :(得分:3)

请参阅此link

您只需使用ContentsResized事件:

private void rtb_ContentsResized(object sender, ContentsResizedEventArgs e)
{
      ((RichTextBox)sender).Height = e.NewRectangle.Height + 5;
}

答案 4 :(得分:2)

一个非常便宜的解决方案(可能充满问题的解决方案)是使用相同的字体和大小同时使用文本填充自动调整标签,然后将标签的宽度复制到RTB的宽度。

所以,就像这样:

RichTextBox rtb = new RichTextBox();
rtb.Text = "this is some text";
rtb.Font = new Font("Franklin Gothic Medium Cond", 10, FontStyle.Regular);

Label fittingLabel = new Label();
fittingLabel.Text = rtb.Text;
fittingLabel.Font = rtb.Font;
fittingLabel.AutoSize = true;

//Not sure if it's necessary to add the label to the form for it to autosize...
fittingLabel.Location = new Point(-1000,-1000);
this.Controls.Add(fittingLabel);

rtb.Width = fittingLabel.Width;

this.Controls.Remove(fittingLabel);

答案 5 :(得分:0)

我找到了Rich文本框高度问题的解决方案..我已将其修改为一般用途..

在您的应用程序中创建以下结构....

[StructLayout(LayoutKind.Sequential)]
    public struct RECT {
        public Int32 left;
        public Int32 top;
        public Int32 right;
        public Int32 bottom;
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct SCROLLBARINFO {
        public Int32 cbSize;
        public RECT rcScrollBar;
        public Int32 dxyLineButton;
        public Int32 xyThumbTop;
        public Int32 xyThumbBottom;
        public Int32 reserved;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]
        public Int32[] rgstate;
    }

在您的类中为表单创建以下私有变量(您需要计算富文本高度)

private UInt32 SB_VERT = 1;
        private UInt32 OBJID_VSCROLL = 0xFFFFFFFB;

        [DllImport("user32.dll")]
        private static extern
            Int32 GetScrollRange(IntPtr hWnd, UInt32 nBar, out Int32 lpMinPos, out Int32 lpMaxPos);

        [DllImport("user32.dll")]
        private static extern
            Int32 GetScrollBarInfo(IntPtr hWnd, UInt32 idObject, ref SCROLLBARINFO psbi);

将以下方法添加到您的课程表格

private int CalculateRichTextHeight(string richText) {
            int height = 0;
            RichTextBox richTextBox = new RichTextBox();
            richTextBox.Rtf = richText;
            richTextBox.Height = this.Bounds.Height;
            richTextBox.Width = this.Bounds.Width;
            richTextBox.WordWrap = false;
            int nHeight = 0;
            int nMin = 0, nMax = 0;

            SCROLLBARINFO psbi = new SCROLLBARINFO();
            psbi.cbSize = Marshal.SizeOf(psbi);

            richTextBox.Height = 10;
            richTextBox.ScrollBars = RichTextBoxScrollBars.Vertical;

            int nResult = GetScrollBarInfo(richTextBox.Handle, OBJID_VSCROLL, ref psbi);
            if (psbi.rgstate[0] == 0) {
                GetScrollRange(richTextBox.Handle, SB_VERT, out nMin, out nMax);
                height = (nMax - nMin);
            }

            return height;
        }

您可能需要修改上述方法,使其按照您的要求运行... 确保将Rtf字符串作为参数发送到方法而不是普通文本,并确保为方法中的Richtextbox变量指定可用的宽度和高度...

您可以根据需要使用WordWrap ...

答案 6 :(得分:0)

如本答案中所述,使用GetPreferredSize要容易得多。然后,您不需要等待ContentsResized事件。