回复 4楼 wei855198
我实在太菜了,可以告诉我这个涵数怎么用吗? SetComboWidth ((4))为何提示类型不匹配?
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long
Private Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long) As Long
Public Sub SetComboWidth(cboIn As ComboBox)
' Resize the with of the dropdown portion of a combobox to accomodate the longest item
Dim nCount As Long
Dim nNumChars As Long
Dim nLongestComboItem As Long
Dim nNewDropDownWidth As Long
Dim nOldScaleMode As Integer
Dim nLongestComboItemIndex As Integer
Dim oOldFont As Font
' Loop through the cboIn entries, to determine the longest item
For nCount = 0 To cboIn.ListCount - 1
nNumChars = SendMessage(cboIn.hwnd, CB_GETLBTEXTLEN, nCount, CLng(0))
If nNumChars > nLongestComboItem Then
nLongestComboItem = nNumChars
nLongestComboItemIndex = nCount
End If
Next
' Save the parent's font and scale mode
nOldScaleMode = cboIn.Parent.ScaleMode
Set oOldFont = cboIn.Parent.Font
cboIn.Parent.ScaleMode = vbPixels
Set cboIn.Parent.Font = cboIn.Font
' Get Width of longest item in pixels
nNewDropDownWidth = cboIn.Parent.TextWidth(cboIn.List(nLongestComboItemIndex))
cboIn.Parent.ScaleMode = nOldScaleMode
Set cboIn.Parent.Font = oOldFont
' Add width of vertical scrollbar
If cboIn.ListCount > 8 Then
nNewDropDownWidth = nNewDropDownWidth + GetSystemMetrics(SM_CYVSCROLL) + 7
Else
nNewDropDownWidth = nNewDropDownWidth + 7
End If
' Resize the dropdown portion of the cboIn box
SendMessage cboIn.hwnd, CB_SETDROPPEDWIDTH, nNewDropDownWidth, CLng(0)
End Sub
[
本帖最后由 yuk_yu 于 2010-3-19 12:03 编辑 ]