Autor Tema: [SRC] [Tip] AlignListBox [by Mr. Frog ©]  (Leído 2910 veces)

0 Usuarios y 1 Visitante están viendo este tema.

Psyke1

  • Megabyte
  • ***
  • Mensajes: 130
  • Reputación: +11/-7
  • VBManiac
    • Ver Perfil
    • h-Sec
[SRC] [Tip] AlignListBox [by Mr. Frog ©]
« en: Diciembre 12, 2010, 06:16:08 pm »
Me encontre con estas constantes para alinear un ListBox e hice esta sencilla función, poner en un módulo:
Solo incluyo alineamiento de items a la derecha e izquierda, porque para centrarlos hay que hacerlo de forma diferente. :silbar:

Código: (vb) [Seleccionar]
Option Explicit
'=========================================================
' º Function : AlignListBox
' º Author   : Mr. Frog ©
' º Mail     : vbpsyke1@mixmail.com
' º Recommended Websites :
'       http://visual-coders.com.ar
'       http://InfrAngeluX.Sytes.Net
'       http://twitter.com/#!/PsYkE1
'=========================================================

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 Const GWL_EXSTYLE                       As Long = (-20)
Private Const WS_EX_RIGHT                       As Long = &H1000&
Private Const WS_EX_LEFT                        As Long = &H0&
Private Const WS_EX_LEFTSCROLLBAR               As Long = &H4000&
Private Const WS_EX_RIGHTSCROLLBAR              As Long = &H0&
 
Public Enum AlignConstants
   aLeft = 0
   aRight = 1
End Enum
 
Public Enum OptionAlign
   Items = 0
   ScollBar = 1
End Enum
 
Public Function AlignListBox(ByVal myListBox As ListBox, _
                               ByVal ThingToAlign As OptionAlign, _
                               Optional ByVal Align As AlignConstants = aLeft) As Long
Dim lStyle                                              As Long
Dim lHwnd                                               As Long
    If Not (myListBox Is Nothing) Then
        lHwnd = myListBox.hwnd
        lStyle = GetWindowLong(lHwnd, GWL_EXSTYLE)
        If Align = aRight Then
            If ThingToAlign = Items Then
                lStyle = lStyle Or WS_EX_RIGHT
            Else
                lStyle = lStyle And WS_EX_RIGHTSCROLLBAR
            End If
        Else
            If ThingToAlign = Items Then
                lStyle = lStyle And WS_EX_LEFT
            Else
                lStyle = lStyle Or WS_EX_LEFTSCROLLBAR
            End If
        End If
        AlignListBox = SetWindowLong(lHwnd, GWL_EXSTYLE, lStyle)
    End If
End Function

Ejemplo:

Código: (vb) [Seleccionar]
Option Explicit

Private Sub Form_Load()
Dim Q                               As Long
    For Q = 0 To (Screen.FontCount - 1)
        List1.AddItem Screen.Fonts(Q)
    Next Q
   
    AlignListBox List1, Items, aRight
    'AlignListBox List1, Items, aLeft
    AlignListBox List1, ScollBar, aLeft
    'AlignListBox List1, ScollBar, aRight
End Sub

Resultado:

DoEvents! :P
« última modificación: Diciembre 12, 2010, 07:51:20 pm por Mr. Frog »