Private Sub Form_Load() Combo1.Text = "" Combo1.AddItem "项目0" Combo1.AddItem "项目1" Combo1.AddItem "项目2" Combo1.AddItem "项目3" Combo1.AddItem "项目4" Combo1.AddItem "项目5" . . . Combo1.AddItem "项目12" End Sub
声明: Private Declare Function SendMessage Lib _ "USER32" Alias "SendMessageA" _ (ByVal hwnd As Long, ByVal Msg As Long, _ ByVal wParam As Long, ByVal lParam As _ Long) As Long Private Const CB_GETDROPPEDWIDTH = &H15F Private Const CB_SETDROPPEDWIDTH = &H160 Private Const CB_ERR = -1 函数: ' 取得 Combo 下拉的宽度 ' 可以利用该函数比例放大或缩小宽度 Public Function GetDropdownWidth(cboHwnd As Long) As Long Dim lRetVal As Long lRetVal = SendMessage(cboHwnd, CB_GETDROPPEDWIDTH, 0, 0) If lRetVal <> CB_ERR Then GetDropdownWidth = lRetVal '单位为 pixels Else GetDropdownWidth = 0 End If End Function '设置 Combo 下拉的宽度 '单位为 pixels Public Function SetDropdownWidth(cboHwnd As _ Long, NewWidthPixel As Long) As Boolean Dim lRetVal As Long lRetVal = SendMessage(cboHwnd, _ CB_SETDROPPEDWIDTH, NewWidthPixel, 0) If lRetVal <> CB_ERR Then SetDropdownWidth = True Else SetDropdownWidth = False End If End Function
我的意思是要在combobox的下拉框中一次显示12行. 楼上的同志是在设置下拉框的宽度.
改变 ComboBox 下拉框的高度Changing the Combo Dropdown Height
Posted: Friday July 24, 1998 Updated: Monday October 29, 2001
Applies to: VB4-32, VB5, VB6, and VB3, VB4-16 with appropriate declarations (untested) Developed with: VB6, Windows NT4 OS restrictions: None Author: Karl E. Peterson, VBnet - Randy Birch
The Visual Basic combo box dropdown - unlike its C counterpart - is limited to displaying only eight items. This page shows how to change the dropdown height to any number greater than eight. As often is the case, credit must be extended to others for providing some of the framework used here. This time the kudos go to Karl E. Peterson for his adjustment routine.
BAS Module Code
Add the following code to a BAS module: --------------------------------------------------------------------------------
Add a combo box (Combo1) a label (Label1) and a command button (Command1) to a form, and add following code: --------------------------------------------------------------------------------
Option ExplicitPrivate 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)
'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 '--end block--'
Comments This routine has been tested under Win95 and Win98, and should work without modification under NT4 as well. The system on which this code was tested was running small fonts; there may be a tweak needed in the calculations should large fonts affect a different outcome.
Combo1.Text = ""
Combo1.AddItem "项目0"
Combo1.AddItem "项目1"
Combo1.AddItem "项目2"
Combo1.AddItem "项目3"
Combo1.AddItem "项目4"
Combo1.AddItem "项目5"
.
.
.
Combo1.AddItem "项目12"
End Sub
Private Declare Function SendMessage Lib _
"USER32" Alias "SendMessageA" _
(ByVal hwnd As Long, ByVal Msg As Long, _
ByVal wParam As Long, ByVal lParam As _
Long) As Long
Private Const CB_GETDROPPEDWIDTH = &H15F
Private Const CB_SETDROPPEDWIDTH = &H160
Private Const CB_ERR = -1
函数:
' 取得 Combo 下拉的宽度
' 可以利用该函数比例放大或缩小宽度
Public Function GetDropdownWidth(cboHwnd As Long) As Long
Dim lRetVal As Long
lRetVal = SendMessage(cboHwnd, CB_GETDROPPEDWIDTH, 0, 0)
If lRetVal <> CB_ERR Then
GetDropdownWidth = lRetVal
'单位为 pixels
Else
GetDropdownWidth = 0
End If
End Function
'设置 Combo 下拉的宽度
'单位为 pixels
Public Function SetDropdownWidth(cboHwnd As _
Long, NewWidthPixel As Long) As Boolean
Dim lRetVal As Long
lRetVal = SendMessage(cboHwnd, _
CB_SETDROPPEDWIDTH, NewWidthPixel, 0)
If lRetVal <> CB_ERR Then
SetDropdownWidth = True
Else
SetDropdownWidth = False
End If
End Function
楼上的同志是在设置下拉框的宽度.
改变 ComboBox 下拉框的高度Changing the Combo Dropdown Height
Posted: Friday July 24, 1998
Updated: Monday October 29, 2001
Applies to: VB4-32, VB5, VB6, and VB3, VB4-16 with appropriate declarations (untested)
Developed with: VB6, Windows NT4
OS restrictions: None
Author: Karl E. Peterson, VBnet - Randy Birch
Prerequisites
None. --------------------------------------------------------------------------------
The Visual Basic combo box dropdown - unlike its C counterpart - is limited to displaying only eight items. This page shows how to change the dropdown height to any number greater than eight.
As often is the case, credit must be extended to others for providing some of the framework used here. This time the kudos go to Karl E. Peterson for his adjustment routine.
BAS Module Code
Add the following code to a BAS module: --------------------------------------------------------------------------------
Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2001 VBnet, Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' You are free to use this code within your own applications,
' but you are expressly forbidden from selling or otherwise
' distributing this source code without prior written consent.
' This includes both posting free demo projects made from this
' code as well as reproducing the code in text or html format.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''Public Type POINTAPI
x As Long
y As Long
End TypePublic Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End TypePublic Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) As LongPublic 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 LongPublic Declare Function GetWindowRect Lib "user32" _
(ByVal hWnd As Long, _
lpRect As RECT) As LongPublic Declare Function ScreenToClient Lib "user32" _
(ByVal hWnd As Long, _
lpPoint As POINTAPI) As LongPublic Const CB_SHOWDROPDOWN = &H14F
Public Const CB_GETITEMHEIGHT = &H154
'--end block--'
Form Code
Add a combo box (Combo1) a label (Label1) and a command button (Command1) to a form, and add following code: --------------------------------------------------------------------------------
Option ExplicitPrivate 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)
'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
'--end block--'
Comments
This routine has been tested under Win95 and Win98, and should work without modification under NT4 as well.
The system on which this code was tested was running small fonts; there may be a tweak needed in the calculations should large fonts affect a different outcome.
我的意思是要在combobox的下拉框中一次显示12行.
楼上的同志是在设置下拉框的宽度.你有没有试试我的代码?