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.dll" _
(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