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
Public Function SetComboBoxHeight(objCB As ComboBox, _
TheHeight As Single) As Boolean
'PURPOSE: Sets dropdown height of combo box
' in Inches
'函数:设置combo下拉框的长度
'SAMPLE USAGE:
' SetComboBoxHeight combo1, 4
'
'RETURNS: True if successful, false otherwise
'
'USAGE NOTES: The Function must be placed in the
' Same Form Module that contains the
' ComboBox
'
' The combo box's parent must be a form
' not a container such as picture box or
' a frame
'
' The scale mode of the form must
' be twips
'
' Only works to increase drop-down
' height, not decrease it
'
' The increase height won't go beyond
' the maximum viewable space of the
' combo box (i.e., there will be
' no padded blank space on the bottom
' of the combo box
On Error Resume Next
Dim lHeight As Single
lHeight = TheHeight * 1440 'inches to pixels
lHeight = lHeight / Screen.TwipsPerPixelY
Me.ScaleMode = vbPixels
With objCB
MoveWindow .hwnd, .Left, .Top, .Width, lHeight, 1
End With
Me.ScaleMode = vbTwips
SetComboBoxHeight = Err.Number = 0 And Err.LastDllError = 0
Public Sub SetDropHeight(Cbo As Object, numItemsToDisplay As Long)
Dim pt As POINTAPI
Dim rc As RECT
Dim cWidth As Long
Dim newHeight As Long
Dim oldScaleMode As Long
Dim itemHeight As Long
'Save the current form scalemode, then switch to pixels
oldScaleMode = frmMain.ScaleMode
frmMain.ScaleMode = vbPixels
'the width of the combo, used below
cWidth = Cbo.Width
'get the system height of a single combo box list item
itemHeight = SendMessage(Cbo.hwnd, CB_GETITEMHEIGHT, 0, ByVal 0)
'Calculate the new height of the combo box. This
'is the number of items times the item height
'plus two. The 'plus two' is required to allow
'the calculations to take into account the size
'of the edit portion of the combo as it relates
'to item height. In other words, even if the
'combo is only 21 px high (315 twips), if the
'item height is 13 px per item (as it is with
'small fonts), we need to use two items to achieve this height.
newHeight = itemHeight * (numItemsToDisplay + 2)
'Get the co-ordinates of the combo box relative to the screen
Call GetWindowRect(Cbo.hwnd, rc)
pt.x = rc.Left
pt.Y = rc.Top
'Then translate into co-ordinates relative to the form.
Call ScreenToClient(frmMain.hwnd, pt)
'Using the values returned and set above,
'call MoveWindow to reposition the combo box
Call MoveWindow(Cbo.hwnd, pt.x, pt.Y, Cbo.Width, newHeight, True)
'Its done, so show the new combo height
'Call SendMessage(Combo1.hwnd, CB_SHOWDROPDOWN, True, ByVal 0)
'restore the original form scalemode before leaving
frmMain.ScaleMode = oldScaleMode
End Sub