如何准确调整RichTextBox控件的大小?

时间:2018-10-19 18:42:31

标签: vb.net richtextbox rtf

我需要根据其中包含的富文本格式确定RichTextBox的尺寸。

我在StackOverflow上找到了一些有关如何执行此操作的“答案”,其中一些甚至被标记为已接受的答案,但是到目前为止,我尝试过的所有方法均未真正起作用。

这是我正在使用的一些测试RTF:

{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Segoe UI;}{\f1\fnil\fcharset0 Tahoma;}}
{\colortbl ;\red255\green0\blue0;}
\viewkind4\uc1\pard\sa180\cf1\lang1024\b\f0\fs18 Do you wish to save changes?\line\line\pard\cf0\b0 These changes have been temporarily saved. Do you want to permentently saves all changes?\cf1\b\f1\fs16\par
}

方法1 -ContentsResized事件

Private Sub RichTextBox1_ContentsResized(sender As Object, e As ContentsResizedEventArgs) Handles RichTextBox1.ContentsResized
  RichTextBox1.ClientSize = New Size(e.NewRectangle.Width, e.NewRectangle.Height)
End Sub

这有时是正确的结果。当自动换行打开时,它会做一些奇怪的事情。每当事件触发时,它将使richtextedit越来越窄。

方法2 -GetPerferredSize

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  Dim perferredSize As Size = RichTextBox1.GetPreferredSize(Size.Empty)

  RichTextBox1.Width = perferredSize.Width
  RichTextBox1.Height = perferredSize.Height
End Sub

这是结果,结果是高度,且两者都太短: enter image description here

方法3 -GetScrollRange()-当前仅设置垂直。我也打算做水平的,但是一旦我意识到垂直已经不行了,就放弃了,所以继续沿着水平的道路前进毫无意义。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  RichTextBox1.Height = CalculateRichTextHeight(RichTextBox1.Rtf)
End Sub

<StructLayout(LayoutKind.Sequential)>
Public Structure RECT
  Public left As Integer
  Public top As Integer
  Public right As Integer
  Public bottom As Integer
End Structure

<StructLayout(LayoutKind.Sequential)>
Public Structure SCROLLBARINFO
  Public cbSize As Integer
  Public rcScrollBar As RECT
  Public dxyLineButton As Integer
  Public xyThumbTop As Integer
  Public xyThumbBottom As Integer
  Public reserved As Integer
  <MarshalAs(UnmanagedType.ByValArray, SizeConst:=6)>
  Public rgstate() As Integer
End Structure

Private SB_VERT As UInt32 = 1
Private OBJID_VSCROLL As UInt32 = &HFFFFFFFBUI

<DllImport("user32.dll")>
Private Shared Function GetScrollRange(hWnd As IntPtr, nBar As UInt32, ByRef lpMinPos As Int32, ByRef lpMaxPos As Int32) As Int32
End Function

<DllImport("user32.dll")>
Private Shared Function GetScrollBarInfo(hWnd As IntPtr, idObject As UInt32, ByRef psbi As SCROLLBARINFO) As Int32
End Function

Private Function CalculateRichTextHeight(sRTF As String) As Integer
  Dim height As Integer = 0
  Dim richTextBox As New RichTextBox
  richTextBox.Rtf = sRTF
  richTextBox.Height = Me.Bounds.Height
  richTextBox.Width = RichTextBox1.Width
  richTextBox.WordWrap = True
  Dim nHeight As Integer = 0
  Dim nMax As Integer = 0
  Dim nMin As Integer = 0

  Dim psbi As New SCROLLBARINFO()

  psbi.cbSize = Marshal.SizeOf(psbi)

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

  Dim nResult As Integer = GetScrollBarInfo(richTextBox.Handle, OBJID_VSCROLL, psbi)
  If (psbi.rgstate(0) = 0) Then
    GetScrollRange(richTextBox.Handle, SB_VERT, nMin, nMax)
    height = (nMax - nMin)
  End If

  Return height
End Function

结果是高度太短: enter image description here

我的想法不多了。有谁知道我能如何准确地测量这种富文本?

0 个答案:

没有答案