Measure String inside RichTextBox Control

后端 未结 6 1017
刺人心
刺人心 2020-12-19 02:44

Can somebody please explain how I would go about measuring the string inside a richtextbox control so that the I can automatically resize the richtextbox control according t

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-19 02:59

    I found a solution for the Rich text box height issues.. i have modified it a for general use..

    Create following structs in your application....

    [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;
        }
    

    Create following private variables in your class for form (where ever you need to calculate rich text height)

    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);
    

    Add following method to your Class for form

    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;
            }
    

    You may need to modify above method to make it work as per your requirement... Make sure to send Rtf string as parameter to method not normal text and also make sure to assign available width and height to the Richtextbox variable in the method...

    You can play with WordWrap depending on your requirement...

提交回复
热议问题