设置COMBOBOX下拉宽度和高度函数举例

  有时候我们为了特殊的需要,设置COMBOX的下拉的宽度和高度,或者得到宽度,使用下面的3个函数即可

  要改变COMBOX的本来控件高度(非下拉高度),直接修改字体大小即可得到效果!

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

  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  
   

'函数:
  '   取得   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

'设置   Combo   下拉高度,能显示更多的ITEMS

  Public   Sub   SetComboHeight(oComboBox   As   ComboBox,   lNewHeight   As   Long)  
            Dim   oldscalemode   As   Integer  
            If   TypeOf   oComboBox.Parent   Is   Frame   Then   Exit   Sub  
           oldscalemode   =   oComboBox.Parent.ScaleMode  
          oComboBox.Parent.ScaleMode   =   vbPixels  
          MoveWindow   oComboBox.hwnd,   oComboBox.Left,   _  
          oComboBox.Top,   oComboBox.Width,   lNewHeight,   1  
          oComboBox.Parent.ScaleMode   =   oldscalemode  
  End   Sub  
  Private   Sub   Command1_Click()  

       Call SetDropdownWidth(Combo1.hwnd, 80)' 注:80是代码pixels. 设置宽度

        Call   SetComboHeight(Combo1,   170)   ' 注:170是代码pixels. 设置高度
  End   Sub   
‘初始化COMBOBOX数据  
Private Sub Form_Load()
  Dim i As Integer
  For i = 0 To 100
      Combo1.AddItem Format(i, "00000000")
   Next
    Combo1.ListIndex = 0
End Sub


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值