Ajouter une scrollbar horizontale a une listbox

Contenu du snippet

Dans VB, le contrôle ListBox possède une barre de défilement verticale mais pas horizontale. Ce qui peut être très embêtant si le texte inclus dans la liste est plus long que la taille du contrôle. Voici comment ajouter une barre horizontale... (A coller dans un module).

Source / Exemple :


'DECLARATION API
Declare Function DrawText Lib "user32" Alias "DrawTextA" (ByVal hdc As Long, ByVal lpStr As String, ByVal nCount As Long, lpRect As RECT, ByVal wFormat As Long) As Long

Declare Function GetSystemMetrics Lib "user32" _
  (ByVal nIndex As Long) As Long

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

'DECLARATION CONSTANTES
Public Const LB_GETHORIZONTALEXTENT = &H193
Public Const LB_SETHORIZONTALEXTENT = &H194
ublic Const DT_CALCRECT = &H400
Public Const SM_CXVSCROLL = 2

'DECLARATION TYPE
Public Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
End Type

Public Sub ApplyScrollBar(fForm As Form, fList As ListBox)

   Dim c As Long
   Dim rcText As RECT
   Dim newWidth As Long
   Dim itemWidth As Long
   Dim sysScrollWidth As Long
   
   fForm.Font.Name = fList.Font.Name
   fForm.Font.Bold = fList.Font.Bold
   fForm.Font.Size = fList.Font.Size
   
   sysScrollWidth = GetSystemMetrics(SM_CXVSCROLL)
   
   For c = 0 To fList.ListCount - 1
   
      Call DrawText(fForm.hdc, (fList.List(c)), -1&, rcText, DT_CALCRECT)
      itemWidth = rcText.Right + sysScrollWidth
         
      If itemWidth >= newWidth Then
         newWidth = itemWidth
      End If
      
   Next
   
   Call SendMessage(fList.hwnd, LB_SETHORIZONTALEXTENT, newWidth, ByVal 0&)
   
End Sub

Conclusion :


Utilisation:
ApplyScrollBar Me, Me.List1

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.