ComboBox list height with api - whats wrong with this code
Hi,
i got a code that suppose to change the vertical length of a combobox list.
Code:
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
Code:
Private Sub ComboHeightInit_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
'change this number to the number you want to display
numItemsToDisplay = 10
'Save the current form scalemode, then
'switch to pixels
oldScaleMode = FrmInit.ScaleMode
FrmInit.ScaleMode = vbPixels
'the width of the combo, used below
cWidth = ComboMod.Width
'get the system height of a single
'combo box list item
itemHeight = SendMessage(ComboMod.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(ComboMod.hwnd, rc)
pt.x = rc.Left
pt.y = rc.Top
'Then translate into co-ordinates
'relative to the form.
Call ScreenToClient(FrmInit.hwnd, pt)
'Using the values returned and set above,
'call MoveWindow to reposition the combo box
Call MoveWindow(ComboMod.hwnd, pt.x, pt.y, ComboMod.Width, newHeight, True)
'Its done, so show the new combo height
Call SendMessage(ComboMod.hwnd, CB_SHOWDROPDOWN, True, ByVal 0)
'restore the original form scalemode
'before leaving
FrmInit.ScaleMode = oldScaleMode
End Sub
the problem is; when i applied this code to my program my combobox disappeared.
when i disabled the line:
Call MoveWindow(ComboMod.hwnd, pt.x, pt.y, ComboMod.Width, newHeight, True)
the combobox appeared but the code didn't set the new list length.
it seems that this code also places my combobox somewhere in the screen and not where it originaly was.
this code works for 'RealisticGraphics' (he gave it to me).
i also applied it to a new project and it worked.
on my project it doesn't and also when i removed every other form and every code from my module the project still didn't work correctly (the combobox moved to somewhere else on the screen).
if you could help me somehow it'll be really great.