Textfarbe in Listviewitem färben

  • Hallo,
    mein Problem sieht wie folgt aus:
    Ich erstelle in einer For-Schleife mehrere Items für die ListView. Diese Items haben keine Variable, in der das Handle gespeichert wird (wegen der For-Schleife). Jetzt will ich in einer anderen For-Schleife diese Items auf etwas bestimmtes überprüfen und die Textfarbe verändern. Das einzige was ich von den Items jedoch habe ist der Index und das Subitem. Wie also kriege ich das geregelt durch den Index und Subitem die Farbe zu ändern?

    MfG

    Einmal editiert, zuletzt von NoName (6. September 2009 um 09:35)

    • Offizieller Beitrag

    So weit ich weiß, kann man den einzelnen Listview-Einträgen gar keine unterschiedlichen Farben zuordnen. Nur für alle zusammen oder mit:

    Zitat


    The special flag $GUI_BKCOLOR_LV_ALTERNATE can be used with Listview control to give alternate background of the ListviewItems lines.
    The odd lines will get the color set by GUICtrlSetBkColor of the Listview control.
    The even lines will get the color set by GUICtrlSetBkColor of the ListviewItem control.


    abwechselnd mit der einen und der anderen Hintergrundfarbe.

  • Spoiler anzeigen
    [autoit]

    #include <GUIConstants.au3>

    [/autoit] [autoit][/autoit] [autoit]

    GUICreate("listview items",220,250, 100,200,-1)
    GUISetBkColor (0x00E0FFFF)

    [/autoit] [autoit][/autoit] [autoit]

    $listview = GUICtrlCreateListView ("col1 |col2|col3 ", 10, 10, 200, 150)
    GUICtrlSetColor(-1, 0x0000A0)
    GUICtrlSetBkColor(-1, 0xBBCCEE)
    $button = GUICtrlCreateButton ("bCol Item2", 75, 170, 70, 20)
    $item1=GUICtrlCreateListViewItem("item1|col12|col13", $listview)
    ;GUICtrlSetColor(-1, 0x007000)
    GUICtrlSetBkColor(-1, 0xEE9999)
    $item2=GUICtrlCreateListViewItem("item3|col22|col23", $listview)
    GUICtrlSetState(-1, $GUI_DISABLE)
    GUICtrlSetColor(-1, 0xC00000)
    GUICtrlSetBkColor(-1, 0xCCEECC)
    $item3=GUICtrlCreateListViewItem("item3|col32|col33", $listview)
    GUICtrlSetColor(-1, 0x007000)
    GUICtrlSetBkColor(-1, 0xEE9999)
    $item4=GUICtrlCreateListViewItem("item4|col42|col43", $listview)
    GUICtrlSetColor(-1, 0xC00000)
    GUICtrlSetBkColor(-1, 0xCCEECC)

    [/autoit] [autoit][/autoit] [autoit]

    GUISetState()

    [/autoit] [autoit][/autoit] [autoit]

    Do
    $msg = GUIGetMsg ()

    Select
    Case $msg = $button
    ;~ MsgBox(0, "listview item", GUICtrlRead(GUICtrlRead($listview)), 2)
    GUICtrlSetBkColor($item2, 0x00008B)
    Case $msg = $listview
    MsgBox(0, "listview", "clicked=" & GUICtrlGetState($listview), 2)
    EndSelect
    Until $msg = $GUI_EVENT_CLOSE

    [/autoit]

    So ähnlich möchte ich es haben, nur dass ich kein Handle habe, damit ich GUICtrlSetColor anwenden könnte...

    • Offizieller Beitrag

    Nur um das auszuschließen: Du weißt, dass man dafür Arrays benutzen kann?

    Spoiler anzeigen
    [autoit]


    #include <GUIConstants.au3>

    [/autoit] [autoit][/autoit] [autoit]

    GUICreate("listview items", 220, 250, 100, 200, -1)
    GUISetBkColor(0x00E0FFFF)

    [/autoit] [autoit][/autoit] [autoit]

    $listview = GUICtrlCreateListView("col1 |col2|col3 ", 10, 10, 200, 150)
    GUICtrlSetColor(-1, 0x0000A0)
    GUICtrlSetBkColor(-1, 0xBBCCEE)
    $button = GUICtrlCreateButton("bCol Item2", 75, 170, 70, 20)
    Global $aItem[9]
    For $i = 0 To 8
    $aItem[$i] = GUICtrlCreateListViewItem("item1|col12|col13", $listview)
    Next

    [/autoit] [autoit][/autoit] [autoit]

    GUISetState()

    [/autoit] [autoit][/autoit] [autoit]

    Do
    $msg = GUIGetMsg()

    [/autoit] [autoit][/autoit] [autoit]

    Select
    Case $msg = $button
    GUICtrlSetBkColor($aItem[2], 0x00008B)
    Case $msg = $listview
    MsgBox(0, "listview", "clicked=" & GUICtrlGetState($listview), 2)
    EndSelect
    Until $msg = $GUI_EVENT_CLOSE

    [/autoit]
    • Offizieller Beitrag

    Ich weiß nicht mehr woher ich das Script habe , könnte dir aber eventuell helfen .

    Spoiler anzeigen
    [autoit]

    #include <ListViewConstants.au3>
    #include <StructureConstants.au3>
    #include <WinAPI.au3>
    #include <WindowsConstants.au3>
    #Include <GuiConstantsEx.au3>
    #Include <GuiListView.au3>

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    ;fonts for custom draw example
    ;bold
    Global $aFont1 = DLLCall("gdi32.dll","int","CreateFont", "int", 14, "int", 0, "int", 0, "int", 0, "int", 700, _
    "dword", 0, "dword", 0, "dword", 0, "dword", 0, "dword", 0, "dword", 0, "dword", 0, _
    "dword", 0, "str", "")
    ;italic
    Global $aFont2 = DLLCall("gdi32.dll","int","CreateFont", "int", 14, "int", 0, "int", 0, "int", 0, "int", 400, _
    "dword", 1, "dword", 0, "dword", 0, "dword", 0, "dword", 0, "dword", 0, "dword", 0, _
    "dword", 0, "str", "")

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    $GUI = GUICreate("Listview Custom Draw", 400, 300)

    [/autoit] [autoit][/autoit] [autoit]

    $cListView = GUICtrlCreateListView("", 2, 2, 394, 268, -1, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT))
    $hListView = GUICtrlGetHandle($cListView)

    [/autoit] [autoit][/autoit] [autoit]

    _GUICtrlListView_InsertColumn($hListView, 0, "Column 1", 100)
    _GUICtrlListView_InsertColumn($hListView, 1, "Column 2", 100)
    _GUICtrlListView_InsertColumn($hListView, 2, "Column 3", 100)

    [/autoit] [autoit][/autoit] [autoit]

    ; Add items
    For $i = 1 To 30
    _GUICtrlListView_AddItem($hListView, "Row" & $i & ": Col 1", $i-1)
    For $j = 1 To 2
    _GUICtrlListView_AddSubItem ($hListView, $i-1, "Row" & $i & ": Col " & $j+1, $j)
    Next
    Next

    [/autoit] [autoit][/autoit] [autoit]

    GUICtrlCreateInput("", 50, 275, 100, 15)

    [/autoit] [autoit][/autoit] [autoit]

    GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
    GUISetState()

    [/autoit] [autoit][/autoit] [autoit]

    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE
    DLLCall("gdi32.dll","int","DeleteObject", "hwnd", $aFont1[0])
    DLLCall("gdi32.dll","int","DeleteObject", "hwnd", $aFont2[0])
    Exit

    [/autoit] [autoit][/autoit] [autoit]

    Func WM_NOTIFY($hWnd, $Msg, $wParam, $lParam)
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR

    [/autoit] [autoit][/autoit] [autoit]

    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $hWndFrom
    Case $hListView
    Switch $iCode
    Case $NM_CUSTOMDRAW
    If Not _GUICtrlListView_GetViewDetails($hWndFrom) Then Return $GUI_RUNDEFMSG
    Local $tCustDraw = DllStructCreate('hwnd hwndFrom;int idFrom;int code;' & _
    'dword DrawStage;hwnd hdc;long rect[4];dword ItemSpec;int ItemState;dword Itemlparam;' & _
    'dword clrText;dword clrTextBk;int SubItem;' & _
    'dword ItemType;dword clrFace;int IconEffect;int IconPhase;int PartID;int StateID;long rectText[4];int Align', _ ;winxp or later
    $lParam), $iDrawStage, $iItem, $iSubitem, $hDC, $tRect, $iColor1, $iColor2, $iColor3
    $iDrawStage = DllStructGetData($tCustDraw, 'DrawStage')
    Switch $iDrawStage
    Case $CDDS_PREPAINT
    Return $CDRF_NOTIFYITEMDRAW
    Case $CDDS_ITEMPREPAINT
    Return $CDRF_NOTIFYSUBITEMDRAW
    Case $CDDS_ITEMPOSTPAINT
    Case BitOR($CDDS_ITEMPREPAINT, $CDDS_SUBITEM)
    $iItem = DllStructGetData($tCustDraw, 'ItemSpec')
    $iSubitem = DllStructGetData($tCustDraw, 'SubItem')
    If _GUICtrlListView_GetItemSelected($hWndFrom, $iItem) Then
    $hDC = _WinAPI_GetDC($hWndFrom)
    $tRect = DllStructCreate('long;long;long;long')
    If $iSubitem = 0 Then
    DllCall('user32.dll','int','SendMessage', 'hwnd',$hWndFrom, 'uint',$LVM_GETSUBITEMRECT, 'wparam',$iItem, 'lparam',DllStructGetPtr($tRect))
    DllStructSetData($tRect, 1, 2)
    DllCall('user32.dll', 'int', 'FillRect', 'ptr', $hDC, 'ptr', DllStructGetPtr($tRect), 'int', _WinAPI_GetStockObject(4))
    EndIf
    DllStructSetData($tRect, 1, 2)
    DllStructSetData($tRect, 2, $iSubitem)
    DllCall('user32.dll','int','SendMessage', 'hwnd',$hWndFrom, 'uint',$LVM_GETSUBITEMRECT, 'wparam',$iItem, 'lparam',DllStructGetPtr($tRect))
    Local $sText = _GUICtrlListView_GetItemText($hWndFrom, $iItem, $iSubitem)
    DLLCall("gdi32.dll","int","SetTextColor", "ptr", $hDC, "int", RGB2BGR(0xffff00))
    DLLCall("gdi32.dll","int","SetBkMode", "ptr", $hDC, "int", 1)
    DllStructSetData($tRect, 1, DllStructGetData($tRect, 1)+2)
    _WinAPI_DrawText($hDC, $sText, $tRect, 0x8000) ;; $DT_END_ELLIPSIS
    _WinAPI_ReleaseDC($hWndFrom, $hDC)
    Return $CDRF_SKIPDEFAULT
    EndIf
    Switch $iItem
    Case 0 To 9 ;for rows 1-10 lets do this
    $iColor1 = RGB2BGR(0xFBFFD8)
    $iColor2 = RGB2BGR(-1)
    $iColor3 = RGB2BGR(0xFF0000)
    If Mod($iSubitem, 2) Then ;odd columns
    DllStructSetData($tCustDraw, 'clrTextBk', $iColor1)
    DllStructSetData($tCustDraw, 'clrText', 0)
    Else ;even columns
    DllStructSetData($tCustDraw, 'clrTextBk', $iColor2)
    DllStructSetData($tCustDraw, 'clrText', $iColor3)
    EndIf
    Case 10 To 19 ;for rows 11-20 lets do this
    $iColor1 = RGB2BGR(0xFBFFD8)
    $iColor2 = RGB2BGR(0x3DF8FF)
    $hDC = DllStructGetData($tCustDraw, 'hdc')
    If Mod($iItem, 2) Then
    If Mod($iSubitem, 2) Then
    DllStructSetData($tCustDraw, 'clrTextBk', $iColor1)
    Else
    DllStructSetData($tCustDraw, 'clrTextBk', $iColor2)
    EndIf
    DLLCall("gdi32.dll","hwnd","SelectObject", "hwnd", $hDC, "hwnd", $aFont1[0]) ;select our chosen font into DC
    Else
    If Mod($iSubitem, 2) Then
    DllStructSetData($tCustDraw, 'clrTextBk', $iColor2)
    Else
    DllStructSetData($tCustDraw, 'clrTextBk', $iColor1)
    EndIf
    DLLCall("gdi32.dll","hwnd","SelectObject", "hwnd", $hDC, "hwnd", $aFont2[0])
    EndIf
    Case 20 To 29 ;for rows 21-30 lets do this
    $iColor1 = RGB2BGR(0xFBFFD8)
    $iColor2 = RGB2BGR(-1)
    If Mod($iItem, 2) Then ;odd rows
    DllStructSetData($tCustDraw, 'clrTextBk', $iColor2)
    Else
    DllStructSetData($tCustDraw, 'clrTextBk', $iColor1)
    EndIf
    EndSwitch
    Return $CDRF_NEWFONT
    Case BitOR($CDDS_ITEMPOSTPAINT, $CDDS_SUBITEM)
    Case Else
    EndSwitch
    EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
    EndFunc ;==>WM_NOTIFY

    [/autoit] [autoit][/autoit] [autoit]

    Func RGB2BGR($iColor)
    Return BitAND(BitShift(String(Binary($iColor)), 8), 0xFFFFFF)
    EndFunc

    [/autoit]
  • Ja ist mir schon bewusst, so habe ich das ja auch gemacht...Nur iwie hat meins nicht zum Erfolg geführt. Habe mein Script nochmal durchgeguckt, glaube ich weiß jetzt warum das mit den Arrays nicht ging, aber mal gucken.

    • Offizieller Beitrag

    Es geht. Du kannst jedem Subitem eigene Farben und Font zuweisen.
    Meine Lösung findest du hier: ListView - SubItem einzeln färben/formatieren

    Raupi : Das Skript hatte ich auch entdeckt und dann solange dran gebastelt, bis eine Variante entstand, mit der sich auch tatsächlich 'on the fly' die Parameter eines SubItem ändern lassen. In dem Vorlageskript wurde nur die Hintergrundfarbe geändert, ich habe ergänzend auch die Textfarbe frei wählbar gemacht.