If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

[VB6] How Can I Make A ListBox Display Colours?

Q: Can An Ordinary ListBox Show Colours?

A: Yes, it can, but with a lot of effort. What we would need to do is to override some of the ListBox's Properties. This means that instead of behaving as a normal ListBox, we make it behave and act differently.

Q: You Talk About Overriding The ListBox Properties, What Does This Entail And How?

A: Well, in order to do this, involves 2 APIs. SetWindowLong and SendMessage. It also involves a procedure / function that replaces the previous "normal" ListBox behaviour. For this example we will show the System Colours inside the ListBox.

Q: Where Do We Start?

A: Add a Module to your project, and add the following APIs and their associated Constants :

Code:

Option Explicit
'APIs To "OwnerDraw" A ListBox, And Override Any Of The ListBox's Events
'Describes the width, height, and location of a rectangle.
Public Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
'Provides necessary information the owner window to determine how to paint an owner-drawn control or menu item.
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
'Copies a block of memory from one location to another
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
'The SetWindowLong function changes an attribute of the specified window.
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
'The CallWindowProc function passes message information to the specified window procedure.
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
'Sends the specified message to a window or windows
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
'This function creates a logical brush that has the specified solid color
Public Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Long) As Long
'This function fills a rectangle using the specified brush
Public Declare Function FillRect Lib "user32" (ByVal hdc As Long, lpRect As RECT, ByVal hBrush As Long) As Long
'This function deletes a logical pen, brush, font, bitmap, region, or palette, freeing all system resources associated with the object
Public Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
'The SetBkColor function sets the current background color to the specified color value
Public Declare Function SetBkColor Lib "gdi32" (ByVal hdc As Long, ByVal crColor As Long) As Long
'The SetTextColor function sets the text color for the specified device context to the specified color.
Public Declare Function SetTextColor Lib "gdi32" (ByVal hdc As Long, ByVal crColor As Long) As Long
'The TextOut function writes a character string at the specified location, using the currently selected font, background color, and text color
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
'Draws a rectangle in the style used to indicate that the rectangle has the focus
Public Declare Function DrawFocusRect Lib "user32" (ByVal hdc As Long, lpRect As RECT) As Long
'Retrieves the current color of the specified display element
Public Declare Function GetSysColor Lib "user32" (ByVal nIndex As Long) As Long
Public Const LB_GETTEXT = &H189 'Gets a string from a list box
Public Const WM_DRAWITEM = &H2B 'Sent to the parent window of an owner-drawn button, combo box, list box, or menu when a visual aspect of the button, combo box, list box, or menu has changed.
Public Const GWL_WNDPROC = (-4) 'Sets a new address for the window procedure
Public Const ODS_FOCUS = &H10
Public Const ODT_LISTBOX = 2
Public lPrevWndProc As Long

* The Perfect Platform for Game Developers: Android
Developing rich, high performance Android games from the ground up is a daunting task. Intel has provided Android developers with a number of tools that can be leveraged by Android game developers.

* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.