Jump to content

Multicolor List?


Recommended Posts

I recall browsing through the forums finding a way to color the text of a line in a seperate color.

Like having a list of flowers in a shop and all roses in the list would apear red and grass green etc...

Me having a bad memory or?

[center][u]WoW Machinima Tool[/u] (Tool for Machinima Artists) [/center]

Link to comment
Share on other sites

I recall browsing through the forums finding a way to color the text of a line in a seperate color.

Like having a list of flowers in a shop and all roses in the list would apear red and grass green etc...

Me having a bad memory or?

Hi! There is nice example from Siao, little modified :D

#include <GuiConstants.au3>
#include <GuiListBox.au3>
#include <WinAPI.au3>

Global Const $ODT_LISTBOX = 2
Global Const $ODA_DRAWENTIRE = 0x1
Global Const $ODA_SELECT = 0x2
Global Const $ODS_SELECTED = 0x1

$hGUI = GUICreate("Test GUI", 300, 200)

$hList = _GUICtrlListBox_Create($hGUI, "", 10, 10, 280, 180, BitOR($LBS_HASSTRINGS, $LBS_OWNERDRAWFIXED))

For $i = 1 To 5
    _GUICtrlListBox_AddString($hList, "String " & $i)
Next

GUIRegisterMsg($WM_DRAWITEM, "WM_DRAWITEM")

GUISetState()

Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE

Func WM_DRAWITEM($hWnd, $Msg, $wParam, $lParam)
    Local $tDRAWITEMS, $iBrushColor, $cID, $itmID, $itmAction, $itmState, $hItm, $hDC
    
    $tDRAWITEMS = DllStructCreate("uint cType;uint cID;uint itmID;uint itmAction;uint itmState;" & _
                                  "hwnd hItm;hwnd hDC;dword itmRect[4];dword itmData", $lParam)
    If DllStructGetData($tDRAWITEMS, "cType") <> $ODT_LISTBOX Then Return $GUI_RUNDEFMSG
    
    $cID = DllStructGetData($tDRAWITEMS, "cID")
    $itmID = DllStructGetData($tDRAWITEMS, "itmID")
    $itmAction = DllStructGetData($tDRAWITEMS, "itmAction")
    $itmState = DllStructGetData($tDRAWITEMS, "itmState")
    $hItm = DllStructGetData($tDRAWITEMS, "hItm")
    $hDC = DllStructGetData($tDRAWITEMS, "hDC")
    
    Switch $itmAction
        Case $ODA_DRAWENTIRE, $ODA_SELECT
            If $itmState = $ODS_SELECTED Then
                $iBrushColor = 0xEEBB99
            Else
                $iBrushColor = 0x66BB99
            EndIf
            
            Local $aBrush = DLLCall("gdi32.dll","hwnd","CreateSolidBrush", "int", $iBrushColor)
            Local $aBrushOld = _WinAPI_SelectObject($hDC, $aBrush[0])
            DLLCall("user32.dll","int","FillRect", "hwnd", $hDC, "ptr", DllStructGetPtr($tDRAWITEMS, "itmRect"), "hwnd", $aBrush[0])
            _WinAPI_SelectObject($hDC, $aBrushOld)
            DLLCall("gdi32.dll","int","DeleteObject", "hwnd", $aBrush[0])
            DLLCall("gdi32.dll","int","SetBkMode", "hwnd", $hDC, "int", 1)
            
            Local $tBuffer = DllStructCreate("char[256]")
            DllCall("user32.dll", "int", "SendMessage", "hwnd", $hItm, "int", $LB_GETTEXT, "int", $itmID, "ptr", DllStructGetPtr($tBuffer))
            $itmText = DllStructGetData($tBuffer, 1)

            DllStructSetData($tDRAWITEMS, "itmRect", DllStructGetData($tDRAWITEMS, "itmRect", 1) + 5, 1)
            DllStructSetData($tDRAWITEMS, "itmRect", DllStructGetData($tDRAWITEMS, "itmRect", 3) - 5, 4)
            
            DllCall("user32.dll", "int", "DrawText", "hwnd", $hDC, "str", $itmText, "int", StringLen($itmText), _
                    "ptr", DllStructGetPtr($tDRAWITEMS, "itmRect"), "int", $DT_LEFT)
    EndSwitch
    
    Return $GUI_RUNDEFMSG
EndFunc
:D
Link to comment
Share on other sites

If you are using ListView (and not ListBox) then you may simply change color of text (but not background):

$ListView1 = GUICtrlCreateListView("Column1|Column2", 8, 116, 837, 396, BitOR($LVS_REPORT,$LVS_SINGLESEL,$LVS_SHOWSELALWAYS,$WS_HSCROLL,$WS_VSCROLL,$WS_BORDER), BitOR($WS_EX_CLIENTEDGE,$LVS_EX_FULLROWSELECT))
...
GUICtrlCreateListViewItem('data1|data2', $ListView1)
GUICtrlSetColor(-1, 0xff0000)
Edited by Zedna
Link to comment
Share on other sites

I recall browsing through the forums finding a way to color the text of a line in a seperate color.

Like having a list of flowers in a shop and all roses in the list would apear red and grass green etc...

Me having a bad memory or?

Something like this?

CODE

#include <GUIConstants.au3>

AutoItSetOption("GUIOnEventMode",1)

$GUITitle = "GUI"

$GUIWidth = 400

$GUIHeight = 300

$GUI = GUICreate($GUITitle,$GUIWidth,$GUIHeight,(@DesktopWidth-$GUIWidth)/2,(@DesktopHeight - $GUIHeight)/2)

$listStuff = GUICtrlCreateListView("Column 1|Column 2",10,10,$GUIWidth -20,$GUIHeight-50)

$buttonselected = GUICtrlCreateButton("Set Color",10,$GUIHeight -30,$GUIWidth-20,20)

$iListviewCount = 20

For $i = 1 To $iListviewCount

GUICtrlCreateListViewItem("Row " &$i & " Col 1|" &"Row " &$i & " Col 2",$listStuff)

Next

GUISetState()

#Region Events list

GUISetOnEvent($GUI_EVENT_CLOSE,"_ExitIt")

GUICtrlSetOnEvent($buttonselected,"_ColorIt")

#EndRegion Events list

While 1

Sleep(10)

WEnd

Func _ExitIt()

Exit

EndFunc

Func _ColorIt()

$hSelected = GUICtrlRead($listStuff)

$sSelectedText = GUICtrlRead($hSelected)

GUICtrlSetBkColor($hSelected,0x000000)

GUICtrlSetColor($hSelected,0xFFFFFF)

ConsoleWrite($sSelectedText &@CRLF)

EndFunc

Edited by Monamo

- MoChr(77)& Chr(97)& Chr(100)& Chr(101)& Chr(32)& Chr(121)& Chr(97)& Chr(32)& Chr(108)& Chr(111)& Chr(111)& Chr(107)-------I've told you 100,000 times not to exaggerate!-------Don't make me hit you with my cigarette hand...-------My scripts:Random Episode Selector, Keyboard MouseMover, CopyPath v2.1, SmartRename for XP,Window Tracer[sup]New![/sup]

Link to comment
Share on other sites

If you are using ListView (and not ListBox) then you may simply change color of text (but not background):

$ListView1 = GUICtrlCreateListView("Column1|Column2", 8, 116, 837, 396, BitOR($LVS_REPORT,$LVS_SINGLESEL,$LVS_SHOWSELALWAYS,$WS_HSCROLL,$WS_VSCROLL,$WS_BORDER), BitOR($WS_EX_CLIENTEDGE,$LVS_EX_FULLROWSELECT))
...
GUICtrlCreateListViewItem('data1|data2', $ListView1)
GUICtrlSetColor(-1, 0xff0000)

This was JUST what i was looking for..

2 Colors.. same list...

Thanks!

[center][u]WoW Machinima Tool[/u] (Tool for Machinima Artists) [/center]

Link to comment
Share on other sites

  • 1 year later...

Regarding the Rasim/Saio example in post #2.

I've updated the #includes and, to demonstrate some behavior, made it to display first a standard listbox, and then an owner-drawn listbox:

#include <GuiListBox.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>

Global Const $ODT_LISTBOX = 2
Global Const $ODA_DRAWENTIRE = 0x1
Global Const $ODA_SELECT = 0x2
Global Const $ODS_SELECTED = 0x1

$hGUI = GUICreate("Test GUI", 300, 180)


GUIRegisterMsg($WM_DRAWITEM, "WM_DRAWITEM")

GUISetState()
;-------------------------------------------------------------------------------

$hList = _GUICtrlListBox_Create($hGUI, "normal listbox", 10, 10, 280, 170, $LBS_NOTIFY)
For $i = 2 To 12
    _GUICtrlListBox_AddString($hList, "String " & $i)
Next

Sleep(2500)

$hList = _GUICtrlListBox_Create($hGUI, "owner-drawn listbox", 10, 10, 280, 170, BitOR($LBS_HASSTRINGS, $LBS_OWNERDRAWFIXED))
For $i = 2 To 12
    _GUICtrlListBox_AddString($hList, "String " & $i)
Next

Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE

;-------------------------------------------------------------------------------
Func WM_DRAWITEM($hWnd, $Msg, $wParam, $lParam)
    Local $tDRAWITEMS, $iBrushColor, $cID, $itmID, $itmAction, $itmState, $hItm, $hDC

    $tDRAWITEMS = DllStructCreate("uint cType;uint cID;uint itmID;uint itmAction;uint itmState;" & _
                                  "hwnd hItm;hwnd hDC;dword itmRect[4];dword itmData", $lParam)
    If DllStructGetData($tDRAWITEMS, "cType") <> $ODT_LISTBOX Then Return $GUI_RUNDEFMSG

    $cID = DllStructGetData($tDRAWITEMS, "cID")
    $itmID = DllStructGetData($tDRAWITEMS, "itmID")
    $itmAction = DllStructGetData($tDRAWITEMS, "itmAction")
    $itmState = DllStructGetData($tDRAWITEMS, "itmState")
    $hItm = DllStructGetData($tDRAWITEMS, "hItm")
    $hDC = DllStructGetData($tDRAWITEMS, "hDC")

    Switch $itmAction
        Case $ODA_DRAWENTIRE, $ODA_SELECT
            If $itmState = $ODS_SELECTED Then
                $iBrushColor = 0xEEBB99
            Else
                $iBrushColor = 0x66BB99
            EndIf

            Local $aBrush = DLLCall("gdi32.dll","hwnd","CreateSolidBrush", "int", $iBrushColor)
            Local $aBrushOld = _WinAPI_SelectObject($hDC, $aBrush[0])
            DLLCall("user32.dll","int","FillRect", "hwnd", $hDC, "ptr", DllStructGetPtr($tDRAWITEMS, "itmRect"), "hwnd", $aBrush[0])
            _WinAPI_SelectObject($hDC, $aBrushOld)
            DLLCall("gdi32.dll","int","DeleteObject", "hwnd", $aBrush[0])
            DLLCall("gdi32.dll","int","SetBkMode", "hwnd", $hDC, "int", 1)

            Local $tBuffer = DllStructCreate("char[256]")
            DllCall("user32.dll", "int", "SendMessage", "hwnd", $hItm, "int", $LB_GETTEXT, "int", $itmID, "ptr", DllStructGetPtr($tBuffer))
            $itmText = DllStructGetData($tBuffer, 1)

            DllStructSetData($tDRAWITEMS, "itmRect", DllStructGetData($tDRAWITEMS, "itmRect", 1) + 5, 1)
            DllStructSetData($tDRAWITEMS, "itmRect", DllStructGetData($tDRAWITEMS, "itmRect", 3) - 5, 4)

            DllCall("user32.dll", "int", "DrawText", "hwnd", $hDC, "str", $itmText, "int", StringLen($itmText), _
                    "ptr", DllStructGetPtr($tDRAWITEMS, "itmRect"), "int", $DT_LEFT)
    EndSwitch

    Return $GUI_RUNDEFMSG
EndFunc

I'm wishing to maintain the same verticle (line) spacing with the owner-drawn LB as a standard LB.

As you can see, whereas 12 lines fit into a standard listbox, one of the same dimensions that is owner-drawn will only contain 10 lines of data. Is there a way to maintain the original spacing and still modify background colors by line?

Thanks.

Edit: Poop! This old thread was right on topic for me, I didn't notice it had been posted in the wrong forum. But since I'm here... ideas for a fix, anyone? (PS: Switching to a ListView also forces increased line-spacing. Listviews only behave well down to a line height of 15 pixels).

Edited by Spiff59
Link to comment
Share on other sites

As you can see, whereas 12 lines fit into a standard listbox, one ofthe same dimensions that is owner-drawn will only contain 10 lines ofdata. Is there a way to maintain the original spacing and still modifybackground colors by line?

This works for me
#include <GuiListBox.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>

Global Const $ODT_LISTBOX = 2
Global Const $ODA_DRAWENTIRE = 0x1
Global Const $ODA_SELECT = 0x2
Global Const $ODS_SELECTED = 0x1

$hGUI = GUICreate("Test GUI", 300, 180)

GUIRegisterMsg($WM_DRAWITEM, "WM_DRAWITEM")

GUISetState()
;-------------------------------------------------------------------------------

$hList = _GUICtrlListBox_Create($hGUI, "normal listbox", 10, 10, 280, 170, $LBS_NOTIFY)
For $i = 2 To 12
    _GUICtrlListBox_AddString($hList, "String " & $i)
Next

Sleep(2500)

$hList = _GUICtrlListBox_Create($hGUI, "owner-drawn listbox", 10, 10, 280, 170, BitOR($LBS_HASSTRINGS, $LBS_OWNERDRAWFIXED))
For $i = 2 To 12
    _GUICtrlListBox_AddString($hList, "String " & $i)
Next
_GUICtrlListBox_SetItemHeight($hList, 13)

Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE

;-------------------------------------------------------------------------------
Func WM_DRAWITEM($hWnd, $Msg, $wParam, $lParam)
    Local $tDRAWITEMS, $iBrushColor, $cID, $itmID, $itmAction, $itmState, $hItm, $hDC

    $tDRAWITEMS = DllStructCreate("uint cType;uint cID;uint itmID;uint itmAction;uint itmState;" & _
                                  "hwnd hItm;hwnd hDC;dword itmRect[4];dword itmData", $lParam)
                                  
    If DllStructGetData($tDRAWITEMS, "cType") <> $ODT_LISTBOX Then Return $GUI_RUNDEFMSG

    $cID = DllStructGetData($tDRAWITEMS, "cID")
    $itmID = DllStructGetData($tDRAWITEMS, "itmID")
    $itmAction = DllStructGetData($tDRAWITEMS, "itmAction")
    $itmState = DllStructGetData($tDRAWITEMS, "itmState")
    $hItm = DllStructGetData($tDRAWITEMS, "hItm")
    $hDC = DllStructGetData($tDRAWITEMS, "hDC")

    Switch $itmAction
        Case $ODA_DRAWENTIRE, $ODA_SELECT
            If $itmState = $ODS_SELECTED Then
                $iBrushColor = 0xEEBB99
            Else
                $iBrushColor = 0x66BB99
            EndIf

            Local $aBrush = DLLCall("gdi32.dll","hwnd","CreateSolidBrush", "int", $iBrushColor)
            Local $aBrushOld = _WinAPI_SelectObject($hDC, $aBrush[0])
            DLLCall("user32.dll","int","FillRect", "hwnd", $hDC, "ptr", DllStructGetPtr($tDRAWITEMS, "itmRect"), "hwnd", $aBrush[0])
            _WinAPI_SelectObject($hDC, $aBrushOld)
            DLLCall("gdi32.dll","int","DeleteObject", "hwnd", $aBrush[0])
            DLLCall("gdi32.dll","int","SetBkMode", "hwnd", $hDC, "int", 1)

            Local $tBuffer = DllStructCreate("char[256]")
            DllCall("user32.dll", "int", "SendMessage", "hwnd", $hItm, "int", $LB_GETTEXT, "int", $itmID, "ptr", DllStructGetPtr($tBuffer))
            $itmText = DllStructGetData($tBuffer, 1)

            DllStructSetData($tDRAWITEMS, "itmRect", DllStructGetData($tDRAWITEMS, "itmRect", 1) + 5, 1)
            DllStructSetData($tDRAWITEMS, "itmRect", DllStructGetData($tDRAWITEMS, "itmRect", 3) - 5, 4)

            DllCall("user32.dll", "int", "DrawText", "hwnd", $hDC, "str", $itmText, "int", StringLen($itmText), _
                    "ptr", DllStructGetPtr($tDRAWITEMS, "itmRect"), "int", $DT_LEFT)
    EndSwitch

    Return $GUI_RUNDEFMSG
EndFunc
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...