调整RichTextBox宽度的大小以适合指定最大高度的文本

时间:2013-04-08 11:35:14

标签: .net vb.net controls

我有一个RichTextBox,一个最大高度和一个可变宽度。

我想调整框的宽度,以便在没有滚动条的情况下适合所有文本。

有办法吗?

1 个答案:

答案 0 :(得分:1)

如果控件有滚动条,你可以得到:

Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As IntPtr, ByVal nIndex As Integer) As Integer

然后在更改文本后调用类似这样的函数:

Private Sub ValidateTextBox(tb As RichTextBox)
    Dim Hdl As IntPtr = tb.Handle
    Dim Style, VBar As Integer

    tb.Width = iMinWidth
    Style = GetWindowLong(Hdl, GWL_STYLE)
    VBar = Style And WS_VSCROLL

    While VBar > 0 AndAlso tb.Width < iMaxWidth
        tb.Width += 24

        Style = GetWindowLong(Hdl, GWL_STYLE)
        VBar = Style And WS_VSCROLL
    End While
End Sub

Public Const WS_VSCROLL As Integer = &H200000
Public Const GWL_STYLE As Integer = (-16)