Visual Basic Combo API
SetWindowLong: Right-Align List Contents in a Combo
     
Posted:   Saturday November 03, 2001
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6
Developed with:   VB6, Windows XP
OS restrictions:   None
Author:   Harald Genauck, VBnet - Randy Birch
     

Related:  

VBnet CoolTabs
GetTextExtentPoint32: Right-Align List Box Data
SetWindowLong: Right-Align List Box Data and/or the Scrollbar
SetWindowLong: Right-Align List Contents in a Combo
SendMessage: Align Text Box Contents Using Tabstops

SendMessage: Align List Box Contents Using Tabstops
WM_LBUTTONDOWN: Substitute a Tabbed List for a Combo's Dropdown List
WM_LBUTTONDOWN: Substitute a ListView for a Combo's Dropdown List
       
 Prerequisites
None.

Like the sister listbox routines, this shows how to apply some interesting, if not unconventional, UI changes to a common VB combo box. And like the list method this page is based on the style code provided by mvp Harald M. Genauck.

Note that the third option will only affect the edit portion of the combo if the combo style is 2 - dropdown.

 BAS Module Code
None.

 Form Code
To a form containing a combo (Combo1), and three check boxes (Check1 - Check3), add the following code:

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 GWL_EXSTYLE As Long = (-20)
Private Const WS_EX_RIGHT As Long = &H1000
Private Const WS_EX_LEFTSCROLLBAR As Long = &H4000
Private Const CB_SHOWDROPDOWN = &H14F

Private Enum AlignConstants
   alignLeft = 0
   alignRight = 1
End Enum

Private Type RECT
   Left As Long
   Top As Long
   Right As Long
   Bottom As Long
End Type

Private Type COMBOBOXINFO
   cbSize As Long
   rcItem As RECT
   rcButton As RECT
   stateButton  As Long
   hwndCombo  As Long
   hwndEdit  As Long
   hwndList As Long
End Type

Private Declare Function GetWindowLong Lib "user32" _
   Alias "GetWindowLongA" _
  (ByVal hwnd As Long, _
   ByVal nIndex As Long) As Long

Private Declare Function SetWindowLong Lib "user32" _
   Alias "SetWindowLongA" _
  (ByVal hwnd As Long, _
   ByVal nIndex As Long, _
   ByVal dwNewLong As Long) As Long
   
Private Declare Function GetComboBoxInfo Lib "user32" _
  (ByVal hwndCombo As Long, _
   CBInfo As COMBOBOXINFO) As Long

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 Sub Form_Load()

   Dim x As Long
   
   For x = 0 To Screen.FontCount - 1
      Combo1.AddItem Screen.Fonts(x)
   Next

End Sub


Private Sub Check1_Click()
   
  'Since the values of the alignment Enum
  'correspond to the two values of a checkbox,
  'the checkbox value property can be used
  'directly as the alignment style parameter.
   ComboListAlign Combo1, Check1.Value
   
End Sub

Private Sub Check2_Click()


   ComboScrollAlign Combo1, Check2.Value

End Sub


Private Sub Check3_Click()

   ComboFlipButtonEdit Combo1, Check3.Value

End Sub


Private Sub ComboListAlign(cb As ComboBox, _
                           Optional ByVal Align As AlignConstants = alignLeft)

   Dim hList As Long
   Dim nStyle As Long

  'obtain the handle to the list
  'portion of the combo
   hList = GetComboListHandle(cb)

  'if valid, change the style
   If hList <> 0 Then
   
      nStyle = GetWindowLong(hList, GWL_EXSTYLE)
      
      Select Case Align
          Case alignRight
              nStyle = nStyle Or WS_EX_RIGHT
          Case Else
              nStyle = nStyle And Not WS_EX_RIGHT
      End Select
      
      SetWindowLong hList, GWL_EXSTYLE, nStyle
   
   End If
   
  'drop the combo
   Call SendMessage(cb.hwnd, CB_SHOWDROPDOWN, True, ByVal 0)
   
End Sub


Private Sub ComboScrollAlign(cb As ComboBox, _
                             Optional ByVal Align As AlignConstants = alignRight)

   Dim hList As Long
   Dim nStyle As Long

  'obtain the handle to the list
  'portion of the combo
   hList = GetComboListHandle(cb)

  'if valid, change the style
   If hList <> 0 Then
   
      nStyle = GetWindowLong(hList, GWL_EXSTYLE)
      
      Select Case Align
          Case alignRight
              nStyle = nStyle Or WS_EX_LEFTSCROLLBAR
          Case Else
              nStyle = nStyle And Not WS_EX_LEFTSCROLLBAR
      End Select
      
      SetWindowLong hList, GWL_EXSTYLE, nStyle
   
   End If
   
  'drop the combo
   Call SendMessage(cb.hwnd, CB_SHOWDROPDOWN, True, ByVal 0)
      
End Sub


Private Sub ComboFlipButtonEdit(cb As ComboBox, _
                                Optional ByVal Align As AlignConstants = alignLeft)

   Dim nStyle As Long

  'get the style for the combo
   nStyle = GetWindowLong(cb.hwnd, GWL_EXSTYLE)
   
  'apply or remove the WS_EX_RIGHT
  'window style
   Select Case Align
       Case alignRight
           nStyle = nStyle Or WS_EX_RIGHT
       Case Else
           nStyle = nStyle And Not WS_EX_RIGHT
   End Select
   
   SetWindowLong cb.hwnd, GWL_EXSTYLE, nStyle
   
  'the code above flips the list
  'portion too, so  reset the list
  'to the correct alignment
   ComboListAlign cb, alignLeft

End Sub


Private Function GetComboListHandle(ctl As ComboBox) As Long

   Dim CBI As COMBOBOXINFO

   CBI.cbSize = Len(CBI)
   Call GetComboBoxInfo(ctl.hwnd, CBI)
   GetComboListHandle = CBI.hwndList

End Function
 Comments
 

 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright 1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

 

Hit Counter