PDA

Click to See Complete Forum and Search --> : Images in a Listbox?


adistler
Dec 30th, 1999, 12:23 AM
I'm new to VB and was wondering if it is possible to have images or an image list in a listbox like you can in a listview.

-Tony

Aaron Young
Dec 30th, 1999, 12:30 AM
It is possible, but to do it you would have to Subclass the Listbox using the Windows API, which isn't particulary easy and if you're new to VB I wouldn't recommend diving head first into the APIs, instead there are plenty of 3rd Party Controls available from places like VB Accelerator (http://www.vbaccelerator.com/) which will do what you want, otherwise, you could just use the Listview.

------------------
Aaron Young
Analyst Programmer
aarony@redwingsoftware.com
ajyoung@pressenter.com

adistler
Dec 30th, 1999, 12:36 AM
I'm new to VB, but I've been doing Visual C for about two years. Mostly Api stuff and dialog boxes. Where's a good starting point for research into this (i.e. is there a listbox api that I should look into)?

Thanks!
-Tony

Aaron Young
Dec 30th, 1999, 12:55 AM
OK, here's a quick example, I'm using a Picturebox to hold the Image I want to display for Each Item in the Listbox just to make the process simpler to demonstrate, ideally you'd load this into memory and create a Compatiable Bitmap, etc..

Add a Listbox and Picturebox to a Form, set the Listbox Stype Property to 1 - Checkbox and load a 16x16 picture into the Picturebox which will be used as the Image for Each List Item..

In a Module..

Option Explicit

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
Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long

Public Const COLOR_HIGHLIGHT = 13
Public Const COLOR_HIGHLIGHTTEXT = 14
Public Const COLOR_WINDOW = 5
Public Const COLOR_WINDOWTEXT = 8
Public Const LB_GETTEXT = &H189
Public Const WM_DRAWITEM = &H2B
Public Const GWL_WNDPROC = (-4)
Public Const ODS_FOCUS = &H10
Public Const ODT_LISTBOX = 2
Private Const SRCCOPY = &HCC0020

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 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)
'Indent all Items to Leave Space for the Image(s)
tItem.rcItem.Left = tItem.rcItem.Left + 18
If tItem.itemData Then
'If the Item Data is Non-Zero draw an Image next to it.
With tItem.rcItem
'Draw the Image in the Picturebox for each Item in the List
BitBlt tItem.hdc, .Left - 17, .Top, 16, 15, Form1.Picture1.hdc, 0, 0, SRCCOPY
End With
End If
If (tItem.itemState And ODS_FOCUS) Then
'Item has Focus, Highlight it, I'm using the Default Focus
'Colors for this example.
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, ByVal sItem, Len(sItem)
DrawFocusRect tItem.hdc, tItem.rcItem
Else
lBack = CreateSolidBrush(GetSysColor(COLOR_WINDOW))
'Paint the Item Area
Call FillRect(tItem.hdc, tItem.rcItem, lBack)
'Set the Text Colors
Call SetBkColor(tItem.hdc, GetSysColor(COLOR_WINDOW))
Call SetTextColor(tItem.hdc, GetSysColor(COLOR_WINDOWTEXT))
'Display the Item Text
TextOut tItem.hdc, tItem.rcItem.Left, tItem.rcItem.Top, ByVal sItem, Len(sItem)
End If
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..

Private Sub Form_Load()
Dim I As Integer
For I = 1 To 10
List1.AddItem "Item " & I
'Set Every other Item to have an Image.
If I Mod 2 = 0 Then List1.itemData(List1.NewIndex) = 1
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



------------------
Aaron Young
Analyst Programmer
aarony@redwingsoftware.com
ajyoung@pressenter.com

adistler
Dec 30th, 1999, 01:27 AM
Wow, I had no idea you could do so much with VB. :)

I copied, pasted and hacked the code to allow for multiple images and eveything is working great.

Thanks alot!
-Tony