Results 1 to 9 of 9

Thread: Images in DropDown List box

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2000
    Posts
    9
    Does any know how to / if you can add images to a dropdown list box. or know of and ActiveX objects to do this.

    Visual Basic 5 SP3 and JavaScript

  2. #2
    Lively Member
    Join Date
    Jul 2000
    Posts
    94
    In VB6 Under Microsoft Common Controls 1 There is an Image combo. Is this what you are looking for?

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2000
    Posts
    9
    It may be however I am using VB5 not VB6 any suggestions.

    Visual Basic 5 SP3 and JavaScript

  4. #4
    Guest
    Usage:
    1. Add an ImageList with 3 ListImages
    2. Add an ImageComboBox
    3. Add a CommandButton and insert the following code

    Code:
    Private Sub Command1_Click()
    
        ImageList1.ListImages(1).Key = "One"
        ImageList1.ListImages(2).Key = "Two"
        ImageList1.ListImages(3).Key = "Three"
        ImageCombo1.ComboItems.Add , "One", "One", "One"
        ImageCombo1.ComboItems.Add , "Two", "Two", "Two"
        ImageCombo1.ComboItems.Add , "Three", "Three", "Three"
    
    End Sub

  5. #5

    Thread Starter
    New Member
    Join Date
    Aug 2000
    Posts
    9
    Where Can I get An ImageComboBox ?

    Visual Basic 5 SP3 and JavaScript

  6. #6
    Guest
    Go to Project > Components. Scroll down to Microsoft Window Common Controls 6.0, check it, then click OK.

  7. #7

    Thread Starter
    New Member
    Join Date
    Aug 2000
    Posts
    9
    What I see is Microsoft Windows Common Controls-3 6.0
    When I Check it and click OK or APPLY I get the following error

    Error in loading DLL

    I tried registering the control with

    regsvr32.exe comct332.ocx

    and get this error

    LoadLibrary("comct332.ocx") failed
    GetLastError returns 0x00000485

    Any Suggestions?

    Visual Basic 5 SP3 and JavaScript

  8. #8
    Guest
    I don't think it would be in that set of controls. Do you have Microsoft Window Common Controls 6.0 listed?


    [Edited by Megatron on 08-09-2000 at 06:09 PM]

  9. #9
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177
    Here's an alternative solution if you don't have VB6 or access to the ImageCombo.

    It's some code I wrote a while ago to subclass a Combobox which I've adapted to make it into an Image Combo:

    In a Module:
    Code:
    Option Explicit
    
    Public oImageList As ImageList
    
    Private Type RECT
            Left As Long
            Top As Long
            Right As Long
            Bottom As Long
    End Type
    
    Private 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
    
    Private Type CWPSTRUCT
            lParam As Long
            wParam As Long
            message As Long
            Hwnd As Long
    End Type
    
    Private Type CREATESTRUCT
            lpCreateParams As Long
            hInstance As Long
            hMenu As Long
            hWndParent As Long
            cy As Long
            cx As Long
            y As Long
            x As Long
            style As Long
            'These next 2 are Normaly String, but need to be a fixed length
            'so we know how long they are when using CopyMemory,
            'We're only interested in the Style Property anyway.
            lpszName As Long
            lpszClass As Long
            ExStyle As Long
    End Type
    
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length 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 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
    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 Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
    Private Declare Function SetBkColor Lib "gdi32" (ByVal hdc As Long, ByVal crColor As Long) As Long
    Private Declare Function SetTextColor Lib "gdi32" (ByVal hdc As Long, ByVal crColor As Long) As Long
    Private Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Long) As Long
    Private Declare Function FillRect Lib "user32" (ByVal hdc As Long, lpRect As RECT, ByVal hBrush As Long) As Long
    
    Private 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
    Private Declare Function GetSysColor Lib "user32" (ByVal nIndex As Long) As Long
    Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal Hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
    
    Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
    Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal Hwnd As Long, ByVal nIndex As Long) As Long
    
    Private Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long
    Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
    Private Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal ncode As Long, ByVal wParam As Long, lParam As Any) As Long
    
    Private Const WH_CALLWNDPROC = 4
    
    Private Const CBS_OWNERDRAWVARIABLE = &H20&
    Private Const CB_GETLBTEXT = &H148
    Private Const CB_SETITEMHEIGHT = &H153
    
    Private Const COLOR_HIGHLIGHT = 13
    Private Const COLOR_HIGHLIGHTTEXT = 14
    Private Const COLOR_WINDOW = 5
    Private Const COLOR_WINDOWTEXT = 8
    
    Private Const GWL_WNDPROC = (-4)
    Private Const GWL_STYLE = (-16)
    
    Private Const ODS_SELECTED = &H1
    
    Private Const ODT_COMBOBOX = 3
    
    Private Const WM_CREATE = &H1
    Private Const WM_DRAWITEM = &H2B
    Private Const WM_MEASUREITEM = &H2C
    
    Private lPrevWndProc As Long
    Private lHook As Long
    Private lSubCombo As Long
    
    Private Function SubClassedForm(ByVal Hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
        Dim tItem As DRAWITEMSTRUCT
        Dim sItem As String
        Dim lBack As Long
        
        If Msg = WM_DRAWITEM Then
            '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))
            'If it's our Combobox..
            If tItem.CtlType = ODT_COMBOBOX Then
                'Get the Item Text
                sItem = Space(255)
    
                Call SendMessage(tItem.hwndItem, CB_GETLBTEXT, tItem.itemID, ByVal sItem)
                sItem = Left(sItem, InStr(sItem, Chr(0)) - 1)
                        
                'Select the Highlight Colors if this Item is currently selected
                If (tItem.itemState And ODS_SELECTED) Then
                    lBack = CreateSolidBrush(GetSysColor(COLOR_HIGHLIGHT))
                    Call SetBkColor(tItem.hdc, GetSysColor(COLOR_HIGHLIGHT))
                    Call SetTextColor(tItem.hdc, GetSysColor(COLOR_HIGHLIGHTTEXT))
                Else
                    'Otherwise, use the default Colors
                    lBack = CreateSolidBrush(GetSysColor(COLOR_WINDOW))
                    Call SetBkColor(tItem.hdc, GetSysColor(COLOR_WINDOW))
                    Call SetTextColor(tItem.hdc, IIf(tItem.itemData, tItem.itemData, GetSysColor(COLOR_WINDOWTEXT)))
                End If
                
                'Paint the Background of the Item
                Call FillRect(tItem.hdc, tItem.rcItem, lBack)
                
                'Draw the Image, if it's visible
                If tItem.rcItem.Left < 0 Or tItem.rcItem.Top < 0 Then Exit Function
                If oImageList.ListImages.Count > 0 And oImageList.ListImages.Count >= tItem.itemData Then
                    oImageList.ListImages(tItem.itemData).Draw tItem.hdc, tItem.rcItem.Left * Screen.TwipsPerPixelX, tItem.rcItem.Top * Screen.TwipsPerPixelY, imlTransparent
                End If
                
                'Display the Item Text, offset to allow for the Image
                TextOut tItem.hdc, tItem.rcItem.Left + 18, tItem.rcItem.Top + 2, ByVal sItem, Len(sItem)
                
                'Don't Return a Value as we've dealt with this Message ourselves
                SubClassedForm = 0
                Exit Function
            End If
        End If
        
        'Not our Combobox, so just process the Message as Normal
        SubClassedForm = CallWindowProc(lPrevWndProc, Hwnd, Msg, wParam, lParam)
    End Function
    
    Sub Main()
        'The Combobox is a little more tricky to manipulate than a Listbox
        'So we need to do a little extra work to make it an "Owner Drawn" Control.
        lHook = SetWindowsHookEx(WH_CALLWNDPROC, AddressOf HookApp, App.hInstance, App.ThreadID)
        Form1.Show
        'Once the Control. etc are Drawn, we can release the Hook
        Call UnhookWindowsHookEx(lHook)
    End Sub
    
    Private Function HookApp(ByVal lHookID As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    
        'This Function will get called when Initializing the Form
        'We want to Interupt it when it tries to create our Combobox..
        Dim tCWP As CWPSTRUCT
        Dim sClass As String
    
        Call CopyMemory(tCWP, ByVal lParam, Len(tCWP))
    
        If tCWP.message = WM_CREATE Then
            'Get the Control Classname
            sClass = Space(128)
            Call GetClassName(tCWP.Hwnd, ByVal sClass, 128)
            sClass = Left(sClass, InStr(sClass, Chr(0)) - 1)
            'If it's our Combobox, Sub-class it to Modify the Create Message..
            If sClass = "ComboLBox" Then
                lSubCombo = SetWindowLong(tCWP.Hwnd, GWL_WNDPROC, AddressOf SubComboCreate)
            End If
        End If
        'Continue the Hook Processing
        HookApp = CallNextHookEx(lHook, lHookID, wParam, ByVal lParam)
    
    End Function
    
    Private Function SubComboCreate(ByVal Hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
        'This Function will be called when the Combobox is about to be created
        Dim tCreate As CREATESTRUCT
        
        If Msg = WM_CREATE Then
            'Grab the Data that's going to be used to Create the Combobox
            Call CopyMemory(tCreate, ByVal lParam, Len(tCreate))
            'Alter it, to make the Combobox an "Owner Drawn" Control
            tCreate.style = tCreate.style Or CBS_OWNERDRAWVARIABLE
            'Copy the modified data back
            Call CopyMemory(ByVal lParam, tCreate, Len(tCreate))
            'Alter the Style to OwnerDrawn
            Call SetWindowLong(Hwnd, GWL_STYLE, tCreate.style)
            'Release this Subclassing Function
            Call SetWindowLong(Hwnd, GWL_WNDPROC, lSubCombo)
        End If
        'Let Windows Process the Modified Data
        SubComboCreate = CallWindowProc(lSubCombo, Hwnd, Msg, wParam, lParam)
        
    End Function
    
    Public Sub SubClassForm(ByVal Hwnd As Long)
        lPrevWndProc = SetWindowLong(Hwnd, GWL_WNDPROC, AddressOf SubClassedForm)
    End Sub
    
    Public Sub RemoveSubClassing(ByVal Hwnd As Long)
        Call SetWindowLong(Hwnd, GWL_WNDPROC, lPrevWndProc)
    End Sub
    In a Form with a Combobox and ImageList
    Code:
    Private Sub Form_Load()
        Dim I As Integer
        
        'This example assumes there are only 3 Images, so loops them
        For I = 0 To 29
            Combo1.AddItem "Item " & I
            
            'Store the Image Index No. in the Items "ItemData" property
            Combo1.itemData(Combo1.NewIndex) = (I Mod 3) + 1
        Next
        Combo1.ListIndex = 0
        
        'Set the Public ImageList Object variable to the ImageList Control added to the Form
        Set oImageList = ImageList1
        
        'Subclass the "Form", to Capture the Combobox Notification Messages
        SubClassForm Hwnd
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
        'Release the SubClassing, Very Import to Prevent Crashing!
        RemoveSubClassing Hwnd
    End Sub
    To use make sure your Startup Object is "Sub Main"

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