Как выровнять текст в комбинированных списках VB6? - PullRequest
1 голос
/ 15 июня 2009

Достаточно прямой вопрос, возможно ли выровнять текст (слева, справа, по центру и т. Д.) В выпадающих списках в VB6? Если да, то как?

Когда текст длиннее ширины поля со списком, то текст, который отображается, начинается где-то посередине текста.

Ответы [ 3 ]

1 голос
/ 15 июня 2009

К сожалению, ответ не так прост.

Здесь приведен пример кода: http://vbnet.mvps.org/index.html?code/comboapi/comborightalignstylebits.htm, который выровняет по праву элементы списка в поле со списком. Однако нет примера центрирования текста.

0 голосов
/ 15 июня 2009

Вот ссылка на код VB6 для автоматического определения размера раскрывающейся части поля со списком в соответствии с содержимым.

vbAccelerator.com обычно имеет код высокого качества. Интересно, что код vbAccelerator очень похож на код в этом ответе (вплоть до комментариев), но код vbAccelerator работает, когда в поле со списком другой шрифт отличается от родительского, в отличие от кода в другой ответ.

0 голосов
/ 15 июня 2009

Вот код, который мой друг написал для автоматического изменения размера поля со списком.

Код

Public Sub ComboDropDownWidth(ByRef cboThis As ComboBox, _
                              ByVal lWidth As Long)
    'PS 11/05/06 PUK5023 Added to automatically size a combo drop-down width to the widest entry
    'PS 11/05/06 PUK5023 This function will custom re-size a combo drop-down width (see also ComboDropDownWidthFromContents)
    On Error GoTo ComboDropDownWidth_Err
    SendMessageLong cboThis.hWnd, CB_SETDROPPEDWIDTH, lWidth, 0
Exit_Point:
    Exit Sub
    '' Error Handling
ComboDropDownWidth_Err:
    LogError "modNYFixLibrary.ComboDropDownWidth", Err.Number, Err.Description
    GoTo Exit_Point
    Resume
End Sub
Public Sub ComboDropDownWidthFromContents(ByRef cboThis As ComboBox, _
                                          Optional ByVal lMaxWidth = -1)
    'PS 11/05/06 PUK5023 Added to automatically size a combo drop-down width to the widest entry
    Dim i As Long
    Dim tR As RECT
    Dim lW As Long
    Dim lWidth As Long
    Dim lHDC As Long
    On Error GoTo ComboDropDownWidthFromContents_Err
    ' Cache the HDC of the parent form for speed:
    lHDC = cboThis.Parent.HDC
    ' Loop through each combo box list item & get its
    ' width, storing the largest:
    For i = 0 To cboThis.ListCount - 1
        DrawText lHDC, cboThis.List(i), -1, tR, DT_CALCRECT
        lW = tR.Right - tR.Left + 8
        If (lW > lWidth) Then
            lWidth = lW
        End If
    Next i
    ' Don't allow width to exceed specified max
    ' width, or the width of the screen:
    If lMaxWidth <= 0 Then
        lMaxWidth = Screen.Width \ Screen.TwipsPerPixelX - 16
    End If
    If (lWidth > lMaxWidth) Then
        lWidth = lMaxWidth
    End If
    ' Combo box looks a bit strange when the
    ' drop down portion is smaller than the
    ' combo box itself:
    If (lWidth < cboThis.Width \ Screen.TwipsPerPixelX) Then
        lWidth = cboThis.Width \ Screen.TwipsPerPixelX
        ComboDropDownWidth cboThis, lWidth
    Else
        'If it is longer Set the drop down width and add a little for good measure otherwise it still obscures the last character
        ComboDropDownWidth cboThis, lWidth + 20
    End If
Exit_Point:
    Exit Sub
    '' Error Handling
ComboDropDownWidthFromContents_Err:
    LogError "modNYFixLibrary.ComboDropDownWidthFromContents", Err.Number, Err.Description
    GoTo Exit_Point
    Resume
End Sub
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...