by bader (7 Submissions)
Category: Miscellaneous
Compatability: Visual Basic 4.0 (32-bit)
Difficulty: Unknown Difficulty
Originally Published: Sat 8th January 2000
Date Added: Mon 8th February 2021
Rating:
(1 Votes)
Changing the Combo Dropdown Height
API Declarations
Public Type POINTAPI
x As Long
y As Long
End Type
Public Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Public Declare Function SendMessage Lib _
"user32" Alias "SendMessageA" _
(ByVal hWnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long
Public 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 Declare Function GetWindowRect Lib _
"user32" (ByVal hWnd As Long, _
lpRect As RECT) As Long
Public Declare Function ScreenToClient Lib _
"user32" (ByVal hWnd As Long, _
lpPoint As POINTAPI) As Long
Public Const CB_SHOWDROPDOWN = &H14F
Public Const CB_GETITEMHEIGHT = &H154
Option Explicit
Private Sub Form_Load()
Dim i As Integer
Command1.Caption = "Resize combo"
'add a few strings to the combo
For i = 1 To 50
Combo1.AddItem CStr(i) & " - combo box string number "
Next
End Sub
Private Sub Command1_Click()
Dim pt As POINTAPI
Dim rc As RECT
Dim cWidth As Long
Dim newHeight As Long
Dim oldScaleMode As Long
Dim numItemsToDisplay As Long
Dim itemHeight As Long
'how many items should appear in the dropdown?
numItemsToDisplay = 16
Label1 = "Items displayed = " & numItemsToDisplay
'Save the current form scalemode, then
'switch to pixels
oldScaleMode = Form1.ScaleMode
Form1.ScaleMode = vbPixels
'the width of the combo, used below
cWidth = Combo1.Width
'get the system height of a single
'combo box list item
itemHeight = SendMessage(Combo1.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(Combo1.hWnd, rc)
pt.x = rc.Left
pt.y = rc.Top
'Then translate into co-ordinates
'relative to the form.
Call ScreenToClient(Form1.hWnd, pt)
'Using the values returned and set above,
'call MoveWindow to reposition the combo box
Call MoveWindow(Combo1.hWnd, pt.x, pt.y, Combo1.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
Form1.ScaleMode = oldScaleMode
End Sub