Option Explicit 
         
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 
Private Const CB_SHOWDROPDOWN = &H14F
Private Const CB_GETITEMHEIGHT = &H154
         
Private Type POINTAPI
   x As Long
   y As Long
End Type
Private Type RECT
   Left As Long
   Top As Long
   Right As Long
   Bottom As Long
End Type
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 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
Private Declare Function GetWindowRect Lib "user32" _
  (ByVal hWnd As Long, _
   lpRect As RECT) As Long
Private Declare Function ScreenToClient Lib "user32" _
  (ByVal hWnd As Long, _
   lpPoint As POINTAPI) As Long
Private Sub Form_Load()
   Dim i As Integer
   
  '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.Caption = "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)
   
  'it's 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 |