Jump to content
Sign in to follow this  
supersonic

LV cell fore-/background color disap disappearing

Recommended Posts

supersonic

Hi,

I have found a code example to set the fore-/background color in ListViews:

http://www.autoit.de/index.php?page=Thread&threadID=25071 .

Setting the colors works fine but if I scroll down/up only the last color setting remains.

Here's the (modified) code example:

; [url="http://www.autoitscript.com/forum/topic/62749-alternating-row-background-color/page__view__findpost__p__470173"]http://www.autoitscript.com/forum/topic/62749-alternating-row-background-color/page__view__findpost__p__470173[/url]
; [url="http://www.autoit.de/index.php?page=Thread&threadID=25071"]http://www.autoit.de/index.php?page=Thread&threadID=25071[/url]

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

Global $___LV_BKCOLOR_CUSTOM = 0xFFFFFF
Global $___LV_BKCOLOR_DEFAULT = 0xFFFFFF
Global $___LV_COLOR_CUSTOM  = 0x000000
Global $___LV_COLOR_DEFAULT  = 0x000000
Global $___LV_ITEM  = -1
Global $___LV_SUBITEM   = -1

Local $hGUI   = GUICreate("", 400, 300, -1, -1)
Local $cListView = GUICtrlCreateListView("", 2, 2, 394, 268)
Local $hListView = GUICtrlGetHandle($cListView)

_GUICtrlListView_SetExtendedListViewStyle($hListView, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT))
_GUICtrlListView_InsertColumn($hListView, 0, "Column 1", 100)
_GUICtrlListView_InsertColumn($hListView, 1, "Column 2", 100)
_GUICtrlListView_InsertColumn($hListView, 2, "Column 3", 100)

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

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

_GUICtrlListView_SetItemBkColor($hListView, 4, 0, 0xFFFF00, 0xFF0000)
_GUICtrlListView_SetItemBkColor($hListView, 5, 1, 0xFFFF00, 0x00FF00)
_GUICtrlListView_SetItemBkColor($hListView, 6, 2, 0xFFFF00, 0x0000FF)

Do
Sleep(10)
Until GUIGetMsg() = $GUI_EVENT_CLOSE

Func _GUICtrlListView_SetItemBkColor($hWnd, $iItem, $iSubitem, $iColor = $___LV_COLOR_DEFAULT, $iBkColor = $___LV_BKCOLOR_DEFAULT)
Local $bReturn = False
If $iItem >= 0 Then
  If $iSubitem >= 0 Then
;~  If _GUICtrlListView_BeginUpdate($hWnd) = True Then
    $___LV_BKCOLOR_CUSTOM = $iBkColor
    $___LV_COLOR_CUSTOM  = $iColor
    $___LV_ITEM = $iItem
    $___LV_SUBITEM   = $iSubitem
    If _GUICtrlListView_SetItemText($hWnd, $___LV_ITEM, _GUICtrlListView_GetItemText($hListView, $___LV_ITEM, $___LV_SUBITEM), $___LV_SUBITEM) = True Then
;~   If _GUICtrlListView_EndUpdate($hWnd) = True Then
      $bReturn = True
;~   Else ; Error.
;~    SetError(5, 0)
;~   EndIf
    Else ; Error.
     SetError(4, 0)
    EndIf
;~  Else ; Error.
;~  SetError(3, 0)
;~  EndIf
  Else ; Error.
   SetError(2, 0)
  EndIf
Else ; Error.
  SetError(1, 0)
EndIf
Return $bReturn
EndFunc

Func _WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
#ForceRef $hWnd, $iMsg, $iwParam, $ilParam
Local $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
;~  Local $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
Local $iCode = DllStructGetData($tNMHDR, "Code")
Local $hWndFrom = DllStructGetData($tNMHDR, "hWndFrom")
;~  Local $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
Local $hWnd1 = $hListView
If Not IsHWnd($hListView) Then $hWnd1 = GUICtrlGetHandle($hListView)
Switch $hWndFrom
  Case $hWnd1
   Switch $iCode
    Case $NM_CUSTOMDRAW
     If Not _GUICtrlListView_GetViewDetails($hWndFrom) Then Return $GUI_RUNDEFMSG
;~   If _GUICtrlListView_GetViewDetails($hWndFrom) = True Then
      Local $tNMLVCUSTOMDRAW = DllStructCreate($tagNMLVCUSTOMDRAW, $ilParam)
      Switch DllStructGetData($tNMLVCUSTOMDRAW, "dwDrawStage")
       Case $CDDS_ITEMPREPAINT
        Return $CDRF_NOTIFYSUBITEMDRAW
       Case BitOR($CDDS_ITEMPREPAINT, $CDDS_SUBITEM)
        Local $iItem = DllStructGetData($tNMLVCUSTOMDRAW, "dwItemSpec") ; Row.
        Local $iSubitem = DllStructGetData($tNMLVCUSTOMDRAW, "iSubItem") ; Column.
        If $iItem = $___LV_ITEM And $iSubitem = $___LV_SUBITEM Then
         DllStructSetData($tNMLVCUSTOMDRAW, "clrText", BitAND(BitShift(String(Binary($___LV_COLOR_CUSTOM)), 8), 0xFFFFFF)) ; Custom foreground color.
         DllStructSetData($tNMLVCUSTOMDRAW, "clrTextBk", BitAND(BitShift(String(Binary($___LV_BKCOLOR_CUSTOM)), 8), 0xFFFFFF)) ; Custom background color.
        Else ; Non-error.
         DllStructSetData($tNMLVCUSTOMDRAW, "clrText", BitAND(BitShift(String(Binary($___LV_COLOR_DEFAULT)), 8), 0xFFFFFF)) ; Default foreground color.
         DllStructSetData($tNMLVCUSTOMDRAW, "clrTextBk", BitAND(BitShift(String(Binary($___LV_BKCOLOR_DEFAULT)), 8), 0xFFFFFF)) ; Default background color.
        EndIf
        Return $CDRF_NEWFONT
      EndSwitch
;~   EndIf
   EndSwitch
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

It seems that the settings only remains during item/subitem creation via _GUICtrlListView_AddItem/_GUICtrlListView_AddSubItem.

I'm trying to set the colors AFTER the ListView is created/filled - the colors are used to indicate a state.

I tried out "everything" I know but I cannot solve it on my own.

Please, could anyone help me to improve this?

Greets,

-supersonic.

Edited by supersonic

Share this post


Link to post
Share on other sites
qsek

This example is quite misconcepted.

There are Global variables for identifying Colored item but it can only hold the information for ONE colored item.

The _GUICtrlListView_SetItemBkColor itself updates the item on creation and renders the according color, but on an update on the listview, only the last item/color information can be redrawn. The previous item/color information is lost and therfore redered again in normal color.

Use this UDF instead:


Teamspeak 3 User Viewer - Quick and functional TS3 Query script, which shows online users.Cached Screenshot Deleter - Deletes older Fraps Screenshots if they exceed a specified limit.Unresolved Topics:Intercept and modify dragdrop text behaviour in scite

Share this post


Link to post
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
Sign in to follow this  

×