Results 1 to 6 of 6

Thread: Making check boxes scroll with text ????

  1. #1

    Thread Starter
    Hyperactive Member Al Smith's Avatar
    Join Date
    May 1999
    Location
    Marcellus, MI. USA
    Posts
    330

    Post

    Hi,
    What I'm trying to do is have a box that can be checked at the end of several lines of text. The text is in a list box which gives you a verticle scroll bar when the lines exceed the height of the list box. I'd like a box at the end of each line that also scrolls with the text.
    The check boxes don't really need to be "Check Boxes" per se as they don't need to respond to a click event. They're just an indicator.

    Thanks,
    Al.

  2. #2
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Post

    Have you tried setting the Listbox Style Property to 1 - Checkbox, this places a Checkbox to the Left of every List Item.

    If you want to be really picky and must have something on the Right, then you could Subclass the Listbox and Draw the Indicator(s) yourself.

    ------------------
    Aaron Young
    Analyst Programmer
    [email protected]
    [email protected]


  3. #3
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Post

    In case you are Picky.. And you have that right..
    Here's an example I've put together which uses Subclassing to achieve a Right Aligned Indicator.

    Add a Listbox to a Form with the Style Property set to 1 - Checkbox:

    In a Module..
    Code:
    Public Type RECT
            Left As Long
            Top As Long
            Right As Long
            Bottom As Long
    End Type
    
    Public Type DRAWITEMSTRUCT
            CtlType As Long
            CtlID As Long
            itemID As Long
            itemAction As Long
            itemState As Long
            hwndItem As Long
            hdc As Long
            rcItem As RECT
            itemData As Long
    End Type
    
    Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
    
    Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam 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
    Public Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Long) As Long
    Public Declare Function FillRect Lib "user32" (ByVal hdc As Long, lpRect As RECT, ByVal hBrush As Long) As Long
    Public Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
    Public Declare Function SetBkColor Lib "gdi32" (ByVal hdc As Long, ByVal crColor As Long) As Long
    Public Declare Function SetTextColor Lib "gdi32" (ByVal hdc As Long, ByVal crColor As Long) As Long
    Public Declare Function TextOut Lib "gdi32" Alias "TextOutA" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal lpString As String, ByVal nCount As Long) As Long
    Public Declare Function DrawFocusRect Lib "user32" (ByVal hdc As Long, lpRect As RECT) As Long
    Public Declare Function GetSysColor Lib "user32" (ByVal nIndex As Long) As Long
    
    Public Const COLOR_HIGHLIGHT = 13
    Public Const COLOR_HIGHLIGHTTEXT = 14
    Public Const COLOR_BACKGROUND = 1
    Public Const COLOR_WINDOW = 5
    
    Public Const LB_GETTEXT = &H189
    Public Const LB_GETITEMDATA = &H199
    
    Public Const LBS_OWNERDRAWFIXED = &H10&
    
    Public Const WM_DRAWITEM = &H2B
    Public Const GWL_WNDPROC = (-4)
    Public Const GWL_STYLE = (-16)
    
    Public Const ODS_FOCUS = &H10
    Public Const ODT_LISTBOX = 2
    
    Public lPrevWndProc As Long
    
    Public Function SubClassedList(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
        Dim tItem As DRAWITEMSTRUCT
        Dim tPic As DRAWITEMSTRUCT
        Dim sBuff As String * 255
        Dim sItem As String
        Dim lBack As Long
        
        If Msg = WM_DRAWITEM Then
            'Redraw the listbox
            'This function only passes the Address of the DrawItem Structure, so we need to
            'use the CopyMemory API to Get a Copy into the Variable we setup:
            Call CopyMemory(tItem, ByVal lParam, Len(tItem))
            'Make sure we're dealing with a Listbox
            If tItem.CtlType = ODT_LISTBOX Then
                'Get the Item Text
                Call SendMessage(tItem.hwndItem, LB_GETTEXT, tItem.itemID, ByVal sBuff)
                sItem = Left(sBuff, InStr(sBuff, Chr(0)) - 1)
                tItem.rcItem.Left = 1
                
                If (tItem.itemState And ODS_FOCUS) Then
                    'Item has Focus, Highlight it
                    lBack = CreateSolidBrush(GetSysColor(COLOR_HIGHLIGHT))
                    Call FillRect(tItem.hdc, tItem.rcItem, lBack)
                    Call SetBkColor(tItem.hdc, GetSysColor(COLOR_HIGHLIGHT))
                    Call SetTextColor(tItem.hdc, GetSysColor(COLOR_HIGHLIGHTTEXT))
                    TextOut tItem.hdc, tItem.rcItem.Left, tItem.rcItem.Top + 1, ByVal sItem, Len(sItem)
                    DrawFocusRect tItem.hdc, tItem.rcItem
                Else
                    'Standard Unselected Item, use the Standard Colors
                    lBack = CreateSolidBrush(GetSysColor(COLOR_WINDOW))
                    Call FillRect(tItem.hdc, tItem.rcItem, lBack)
                    'Set the Text Colors
                    Call SetBkColor(tItem.hdc, GetSysColor(COLOR_WINDOW)) 'tItem.itemData)
                    Call SetTextColor(tItem.hdc, vbBlack)
                    'Display the Item Text
                    TextOut tItem.hdc, tItem.rcItem.Left, tItem.rcItem.Top + 1, ByVal sItem, Len(sItem)
                End If
                
                'Create a Brush for the Indicator, (Black Outline, Filled Red if Selected)
                tPic = tItem
                With tPic.rcItem
                    .Left = .Right - 17
                    .Top = .Top + 1
                    .Bottom = .Bottom - 1
                    .Right = .Right - 1
                End With
                lBack = CreateSolidBrush(vbBlack)
                Call FillRect(tItem.hdc, tPic.rcItem, lBack)
                With tPic.rcItem
                    .Left = .Left + 1
                    .Top = .Top + 1
                    .Bottom = .Bottom - 1
                    .Right = .Right - 1
                End With
                lBack = CreateSolidBrush(IIf(tItem.itemData, vbRed, vbWhite))
                Call FillRect(tItem.hdc, tPic.rcItem, lBack)
                Call DeleteObject(lBack)
                'Don't Need to Pass a Value on as we've just handled the Message ourselves
                SubClassedList = 0
                Exit Function
            End If
        End If
        SubClassedList = CallWindowProc(lPrevWndProc, hwnd, Msg, wParam, lParam)
    End Function
    In the Form..
    Code:
    Private Sub Form_Load()
        Dim I As Integer
        For I = 1 To 10
            List1.AddItem "Item " & I
            'Randomly set the Items Selected Marker using the ItemData Property
            List1.itemData(List1.NewIndex) = (Int(Rnd * 10) < 5)
        Next
        'Subclass the "Form", to Capture the Listbox Notification Messages
        lPrevWndProc = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf SubClassedList)
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
        'Release the SubClassing, Very Import to Prevent Crashing!
        Call SetWindowLong(hwnd, GWL_WNDPROC, lPrevWndProc)
    End Sub
    
    Private Sub List1_DblClick()
        'Select or Deselect an Item.
        List1.itemData(List1.ListIndex) = Not List1.itemData(List1.ListIndex)
    End Sub
    ------------------
    Aaron Young
    Analyst Programmer
    [email protected]
    [email protected]


  4. #4

    Thread Starter
    Hyperactive Member Al Smith's Avatar
    Join Date
    May 1999
    Location
    Marcellus, MI. USA
    Posts
    330

    Post

    Aaron,
    Thank you.
    I'm not that picky and will use your first suggestion, which I didn't know about, to get this thing done. I'm going to use your sample code to learn how you did this and maybe use it in the future.
    Again, Thank you.
    Al.


    ------------------
    A computer is a tool, not a toy.
    <A HREF="mailto:[email protected]
    [email protected]">[email protected]
    [email protected]</A>


  5. #5
    Hyperactive Member Gimpster's Avatar
    Join Date
    Oct 1999
    Location
    Redmond, WA 98052
    Posts
    331

    Post

    Aaron, you absolutely AMAZE me sometimes! How do you come up with this stuff so fast?

    ------------------
    Ryan

  6. #6
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Post

    There isn't much I haven't done at some point, once you do something, like subclassing, a few times, it becomes second nature.

    I also have alot of years of experience under my belt, the last 4 in VB.

    I love programming, that's a plus, and of course, practice, practice, practice.



    ------------------
    Aaron Young
    Analyst Programmer
    [email protected]
    [email protected]


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width