相当直截了当的问题,是否可以在VB6中的组合框中对齐文本(左,右,中心等...)? 如果是这样,怎么样?
当文本长于组合框的宽度时,文本显示的内容从文本中间的某处开始。
答案 0 :(得分:1)
遗憾的是答案并不那么简单。
这里有一个代码示例:http://vbnet.mvps.org/index.html?code/comboapi/comborightalignstylebits.htm,它将右对齐组合框中的列表项。但是没有将文本居中的例子。
答案 1 :(得分:0)
以下是我的一位朋友写的一些代码,用于将组合框自动调整为其内容。
代码
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
答案 2 :(得分:0)
这是一个link to VB6 code,用于自动调整组合框的下拉部分以适合内容。
vbAccelerator.com通常具有高质量的代码。有趣的是,vbAccelerator代码看起来与this answer中的代码非常相似(直到评论),但是当组合框与其父表单具有不同的字体时,vbAccelerator代码可以工作,这与其他答案中的代码不同。