有时候我们为了特殊的需要,设置COMBOX的下拉的宽度和高度,或者得到宽度,使用下面的3个函数即可
要改变COMBOX的本来控件高度(非下拉高度),直接修改字体大小即可得到效果!
Private Declare Function SendMessage Lib _
"USER32" Alias "SendMessageA" _
(ByVal hwnd As Long, ByVal Msg As Long, _
ByVal wParam As Long, ByVal lParam As _
Long) As Long
Private Const CB_GETDROPPEDWIDTH = &H15F
Private Const CB_SETDROPPEDWIDTH = &H160
Private Const CB_ERR = -1
Private Declare Function MoveWindow Lib "user32" _
(ByVal hwnd As Long, ByVal x As Long, ByVal y As _
Long, ByVal nWidth As Long, ByVal nHeight As Long, _
ByVal bRepaint As Long) As Long
'函数:
' 取得 Combo 下拉的宽度
' 可以利用该函数比例放大或缩小宽度
Public Function GetDropdownWidth(cboHwnd As Long) As Long
Dim lRetVal As Long
lRetVal = SendMessage(cboHwnd, CB_GETDROPPEDWIDTH, 0, 0)
If lRetVal <> CB_ERR Then
GetDropdownWidth = lRetVal
'单位为 pixels
Else
GetDropdownWidth = 0
End If
End Function'设置 Combo 下拉的宽度
'单位为 pixels
Public Function SetDropdownWidth(cboHwnd As _
Long, NewWidthPixel As Long) As Boolean
Dim lRetVal As Long
lRetVal = SendMessage(cboHwnd, _
CB_SETDROPPEDWIDTH, NewWidthPixel, 0)
If lRetVal <> CB_ERR Then
SetDropdownWidth = True
Else
SetDropdownWidth = False
End If
End Function'设置 Combo 下拉高度,能显示更多的ITEMS
Public Sub SetComboHeight(oComboBox As ComboBox, lNewHeight As Long)
Dim oldscalemode As Integer
If TypeOf oComboBox.Parent Is Frame Then Exit Sub
oldscalemode = oComboBox.Parent.ScaleMode
oComboBox.Parent.ScaleMode = vbPixels
MoveWindow oComboBox.hwnd, oComboBox.Left, _
oComboBox.Top, oComboBox.Width, lNewHeight, 1
oComboBox.Parent.ScaleMode = oldscalemode
End Sub
Private Sub Command1_Click()Call SetDropdownWidth(Combo1.hwnd, 80)' 注:80是代码pixels. 设置宽度
Call SetComboHeight(Combo1, 170) ' 注:170是代码pixels. 设置高度
End Sub
‘初始化COMBOBOX数据
Private Sub Form_Load()
Dim i As Integer
For i = 0 To 100
Combo1.AddItem Format(i, "00000000")
Next
Combo1.ListIndex = 0
End Sub