0

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

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

有没有办法做到这一点?

4

1 回答 1

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)
于 2013-04-08T12:28:08.453 回答