Results 1 to 7 of 7

Thread: Different colors

  1. #1

    Thread Starter
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    This question has been asked several times, and I've yet to see anyone post a way of doing it with a listbox.

  2. #2
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    Originally posted by Hack
    This question has been asked several times, and I've yet to see anyone post a way of doing it with a listbox.
    ooh.

    i've done it with a combobox, i'll have a go at it later and post my results
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  3. #3

    Thread Starter
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    And I've done it with an MSFlexGrid, but I've never seen it done with a ListBox or a Combo box, so I'd be interested in seeing your code.

  4. #4
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    phew, did it!!

    I found some code written by Aaron Young (on here) and modified it to make it simpler, also commented the hell out of it too

    VB Code:
    1. 'In a form
    2. Option Explicit
    3.  
    4. Private Sub Form_Load()
    5.     Dim i As Integer
    6.    
    7.     '*************************************
    8.     'NEED TO SET THIS LINE IN DESIGN TIME AS READONLY PROPERTY!!!
    9.     'List1.Style = 1 '(checkbox)
    10.     '*************************************
    11.    
    12.     For i = 0 To 15
    13.         'Set to the QBColours 0 - 15
    14.         List1.AddItem "Colour " & i
    15.         List1.itemData(List1.NewIndex) = QBColor(i)
    16.     Next
    17.    
    18.     'Subclass the form
    19.     lPrevWndProc = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf SubClassedList)
    20. End Sub
    21.  
    22. Private Sub Form_Unload(Cancel As Integer)
    23.     'Release the subClassing
    24.     Call SetWindowLong(hwnd, GWL_WNDPROC, lPrevWndProc)
    25. End Sub
    26.  
    27.  
    28.  
    29. 'In a module
    30. Option Explicit
    31.  
    32. 'Co ordinates for rectangle items
    33. Public Type RECT
    34.         Left As Long
    35.         Top As Long
    36.         Right As Long
    37.         Bottom As Long
    38. End Type
    39.  
    40. 'Info for each draw item
    41. Public Type DRAWITEMSTRUCT
    42.         CtlType As Long
    43.         CtlID As Long
    44.         itemID As Long
    45.         itemAction As Long
    46.         itemState As Long
    47.         hwndItem As Long
    48.         hdc As Long
    49.         rcItem As RECT
    50.         itemData As Long
    51. End Type
    52.  
    53.  
    54. Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
    55.  
    56. 'Used for subclassing
    57. Public lPrevWndProc As Long
    58. Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    59. 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
    60. Public Const GWL_WNDPROC = (-4)
    61. Public Const WM_DRAWITEM = &H2B
    62. Public Const ODT_LISTBOX = 2
    63.  
    64. 'used to get the text in the listitem
    65. 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
    66. Public Const LB_GETTEXT = &H189
    67.  
    68. 'used to create a brush for drawing
    69. Public Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Long) As Long
    70. Public Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
    71.  
    72. 'used to set the background colour and text colours etc
    73. Public Declare Function FillRect Lib "user32" (ByVal hdc As Long, lpRect As RECT, ByVal hBrush As Long) As Long
    74. Public Declare Function SetBkColor Lib "gdi32" (ByVal hdc As Long, ByVal crColor As Long) As Long
    75. Public Declare Function SetTextColor Lib "gdi32" (ByVal hdc As Long, ByVal crColor As Long) As Long
    76. 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
    77.  
    78. 'used to draw the dotted focus rectangle when something has the focus
    79. Public Declare Function DrawFocusRect Lib "user32" (ByVal hdc As Long, lpRect As RECT) As Long
    80. Public Const ODS_FOCUS = &H10
    81.  
    82. 'used to get system colours
    83. Public Declare Function GetSysColor Lib "user32" (ByVal nIndex As Long) As Long
    84. Public Const COLOR_HIGHLIGHT = 13
    85. Public Const COLOR_HIGHLIGHTTEXT = 14
    86.  
    87.  
    88.  
    89. Public Function SubClassedList(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    90.     Dim tItem As DRAWITEMSTRUCT
    91.     Dim sBuff As String * 255
    92.     Dim sItem As String
    93.     Dim lBack As Long
    94.     Dim lngBkColour As Long
    95.     Dim lngTextColour As Long
    96.    
    97.     'if windows (OS) is drawing items on the form (window)
    98.     If Msg = WM_DRAWITEM Then 'Redraw the listbox
    99.        
    100.         'This function only passes the Address of the DrawItem Structure, so we need to
    101.         'use the CopyMemory API to Get a Copy into the Variable we setup:
    102.         Call CopyMemory(tItem, ByVal lParam, Len(tItem))
    103.        
    104.         'Make sure we're dealing with a Listbox (listbox items)
    105.         If tItem.CtlType = ODT_LISTBOX Then
    106.            
    107.             'DECIDE WHAT COLOURS WE WANT TO USE DEPENDING ON WHETHER THE LIST ITEM
    108.             'HAS FOCUS OR NOT
    109.             If (tItem.itemState And ODS_FOCUS) Then
    110.                 'Item has focus, so highlight it
    111.                 lngBkColour = GetSysColor(COLOR_HIGHLIGHT)
    112.                 lngTextColour = GetSysColor(COLOR_HIGHLIGHTTEXT)
    113.             Else
    114.                 'Item doesnt have focus so draw colour we specified in itemdatda
    115.                 lngBkColour = tItem.itemData
    116.                 'make sure text will be visible
    117.                 lngTextColour = IIf(tItem.itemData = vbBlack, vbWhite, vbBlack)
    118.             End If
    119.            
    120.             'DRAW THE BACKGROUND COLOUR
    121.             'Create a Brush
    122.             lBack = CreateSolidBrush(lngBkColour)
    123.             'Paint the item area
    124.             Call FillRect(tItem.hdc, tItem.rcItem, lBack)
    125.            
    126.             'GET THE ITEM TEXT
    127.             Call SendMessage(tItem.hwndItem, LB_GETTEXT, tItem.itemID, ByVal sBuff)
    128.             sItem = Left(sBuff, InStr(sBuff, Chr(0)) - 1)
    129.            
    130.             'SET THE TEXT COLOURS
    131.             'Set the text background colour
    132.             Call SetBkColor(tItem.hdc, lngBkColour)
    133.             'Set the text foreground colour
    134.             Call SetTextColor(tItem.hdc, lngTextColour)
    135.            
    136.             'DRAW THE TEXT ITSELF
    137.             TextOut tItem.hdc, tItem.rcItem.Left, tItem.rcItem.Top, ByVal sItem, Len(sItem)
    138.        
    139.             'dont forget to draw the focus rectangle if it had the focus
    140.             If (tItem.itemState And ODS_FOCUS) Then DrawFocusRect tItem.hdc, tItem.rcItem
    141.            
    142.             'Release the brush object from memory that we created
    143.             Call DeleteObject(lBack)
    144.            
    145.             Exit Function
    146.                    
    147.         End If
    148.            
    149.     End If
    150.    
    151.     SubClassedList = CallWindowProc(lPrevWndProc, hwnd, Msg, wParam, lParam)
    152.    
    153. End Function

    who's ya daddy!
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  5. #5
    Hyperactive Member Janaka's Avatar
    Join Date
    Nov 2001
    Posts
    277
    i cut and pasted the code.
    I get the list.
    But it is in Black...

  6. #6
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    The only Listbox i know of that can do that is The listview.... And My RichListBox OCx. The listview can only change the forecolor Mine can do fore and back colors. Among Many other things.

    But My Ocx is $9 But worth every peny i tell yah!
    Last edited by Arc; Apr 11th, 2002 at 12:24 AM.
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  7. #7
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    Originally posted by Janaka
    i cut and pasted the code.
    I get the list.
    But it is in Black...
    at design time set the Style property of the listbox to CheckBox
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

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