Sign in to follow this  
Followers 0
erezlevi

help with listview colors

8 posts in this topic

can someone of the masters here tell me why some of the listview items background are black and font is larger then others when pressing a line?

Opt("GUIOnEventMode", 1)
#include <File.au3>
#include <Array.au3>
#include <GUIconstants.au3>
#include <String.au3>
#include <FontConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiListView.au3>
#include <ListViewConstants.au3>
#include <StructureConstants.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>
;===============================================starting with login to "pbx"=======================
;==================================================================================================
Global $skill330[11], $skill910[11], $skill106[11]

for $i=1 to 10
    $skill330[$i] = $i
Next
for $i=1 to 10
    $skill106[$i] = $i+10
Next
for $i=1 to 10
    $skill910[$i] = $i+20
Next
;=====================Displaying the results for the first time!====================
#region - GUI
$GUI=GUICreate("ACD Status 199", 1000, 500, 10, 10);,$WS_POPUP)
GUISetBkColor(0xFFFFFF) ; will change background color
GUISetOnEvent($GUI_EVENT_CLOSE, '_exit')
$cListView = GUICtrlCreateListView("1|2|3|4|5|6|7|8|9", 30, 20, 925, 400, -1, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT))
$hListView1 = GUICtrlGetHandle($cListView)
$item1 = GUICtrlCreateListViewItem($skill330[10]&"|"&$skill330[9] & "|" & $skill330[8] & "|" & $skill330[7] & "|" & $skill330[6] & "|" & $skill330[5] & "|" & $skill330[4] & "|" & $skill330[3] & "|" & $skill330[2], $cListView)
$item2 = GUICtrlCreateListViewItem($skill910[10]&"|"&$skill910[9] & "|" & $skill910[8] & "|" & $skill910[7] & "|" & $skill910[6] & "|" & $skill910[5] & "|" & $skill910[4] & "|" & $skill910[3] & "|" & $skill910[2], $cListView)
$item3 = GUICtrlCreateListViewItem($skill106[10]&"|"&$skill106[9] & "|" & $skill106[8] & "|" & $skill106[7] & "|" & $skill106[6] & "|" & $skill106[5] & "|" & $skill106[4] & "|" & $skill106[3] & "|" & $skill106[2], $cListView)
#EndRegion - GUI

$inCol = '0xFF0000'
$inBkCol = '0x3DF8FF'


#Region - Global settings (needed, whenever you want to use formatting)
; create an array for every LV with same count of elements like in LV
; IMPORTANT:
; By deleting an LV-Item it's required to delete also the according item from array!
; Also by insert an item in LV or sort LV you must modulate the array!
; [Item][SubItem][0] = iBkCol
; [Item][SubItem][1] = iCol
; [Item][SubItem][2] = iSize
; [Item][SubItem][3] = iWeight
; [Item][SubItem][4] = sFont
Global $aLV1[_GUICtrlListView_GetItemCount($hListView1)][_GUICtrlListView_GetColumnCount($hListView1)][5]
; create array to hold ListView-handle and accordingly array
Global $ahWndSets[2][2] = [[$hListView1, $aLV1],[1,1]]
;_ArrayDisplay ($ahWndSets)
Global $hFont, $defColLV = 0x000000, $defBkColLV = 0xFFFFFF
#EndRegion - Global settings (needed, whenever you want to use formatting)

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
GUISetState(@SW_SHOW, $GUI)

While True
    Sleep(2000)
    _btSet()
    Sleep(1000)
    _btOff()
WEnd

Func _exit()
    _WinAPI_DeleteObject($hFont)
    Exit
EndFunc   ;==>_exit

Func _GetLV()
        Return $hListView1
EndFunc   ;==>_GetLV


Func _btSet()
    $setIndex = '1'
    $setSubIndex = '1'
    If ($setIndex <> '' And $setSubIndex <> '') Then
        _SetItemParam($hListView1, $setIndex, $setSubIndex, $inBkCol, $inCol, 14, 600, 'Comic Sans MS')
    EndIf
EndFunc   ;==>_btSet

Func _btOff()
    $setIndex = '1'
    $setSubIndex = '1'
    If ($setIndex <> '' And $setSubIndex <> '') And BitAND(_GUICtrlListView_GetItemParam($hListView1, $setIndex), 2 ^ $setSubIndex) Then _
            _SetItemParam($hListView1, $setIndex, $setSubIndex, -1, -1, -1, -1, -1)
EndFunc   ;==>_btOff

Func _SetInput($aRet) ; only for example
    ;If $aRet[0] = $hListView1 Then
    ;   GUICtrlSetState($rLV1, $GUI_CHECKED)
    ;Else
    ;   GUICtrlSetState($rLV2, $GUI_CHECKED)
    ;EndIf
    $aRet[1]='1'
    $aRet[2]='1'
    ;GUICtrlSetData($inItem, $aRet[1])
    ;GUICtrlSetData($inSubItem, $aRet[2])
EndFunc   ;==>_SetInput

Func WM_NOTIFY($hWnd, $Msg, $wParam, $lParam)
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR
    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $hWndFrom
        Case $hListView1
            Switch $iCode
                Case $NM_CLICK ; only to set index to input in example
                    Local $tInfo = DllStructCreate($tagNMITEMACTIVATE, $lParam)
                    Local $aRet[3] = [$hWndFrom, DllStructGetData($tInfo, "Index"), DllStructGetData($tInfo, "SubItem")]
                    Return _SetInput($aRet)
                Case $NM_CUSTOMDRAW
                    If Not _GUICtrlListView_GetViewDetails($hWndFrom) Then Return $GUI_RUNDEFMSG
                    Local $tCustDraw = DllStructCreate($tagNMLVCUSTOMDRAW, $lParam)
                    Local $iDrawStage, $iItem, $iSubitem, $hDC, $tRect
                    $iDrawStage = DllStructGetData($tCustDraw, 'dwDrawStage')
                    Switch $iDrawStage
                        Case $CDDS_ITEMPREPAINT
                            Return $CDRF_NOTIFYSUBITEMDRAW
                        Case BitOR($CDDS_ITEMPREPAINT, $CDDS_SUBITEM)
                            $iItem = DllStructGetData($tCustDraw, 'dwItemSpec')
                            $iSubitem = DllStructGetData($tCustDraw, 'iSubItem')
                            If BitAND(_GUICtrlListView_GetItemParam($hWndFrom, $iItem), 2 ^ $iSubitem) Then
                                _DrawItemCol($hDC, $tCustDraw, $hWndFrom, $iItem, $iSubitem)
                            Else
                                _DrawDefault($hDC, $tCustDraw)
                            EndIf
                            Return $CDRF_NEWFONT
                    EndSwitch
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

; use _SetItemParam() with defaults to set off
; to mark an SubItem as set, 2^SubItem-index are stored in ItemParam as sum for all SubItem,
; so the max. count of columns are 31 !!
Func _SetItemParam($hWnd, $iItem, $iSubitem, $iBkCol = -1, $iCol = -1, $iSize = -1, $iWeight = -1, $sFont = -1)
    Local $accArray, $sumParam = 0
    For $i = 0 To UBound($ahWndSets) - 1
        If $ahWndSets[$i][0] = $hWnd Then
            $accArray = $ahWndSets[$i][1] ; temp array
            ExitLoop
        EndIf
    Next
    If $iBkCol = -1 Then
        $iBkCol = $defBkColLV
        $sumParam += 1
    EndIf
    If $iCol = -1 Then
        $iCol = $defColLV
        $sumParam += 1
    EndIf
    If $iSize = -1 Then
        $iSize = 14
        $sumParam += 1
    EndIf
    If $iWeight = -1 Then
        $iWeight = 400
        $sumParam += 1
    EndIf
    If $sFont = -1 Then
        $sFont = 'Arial'
        $sumParam += 1
    EndIf
    $accArray[$iItem][$iSubitem][0] = $iBkCol
    $accArray[$iItem][$iSubitem][1] = $iCol
    $accArray[$iItem][$iSubitem][2] = $iSize
    $accArray[$iItem][$iSubitem][3] = $iWeight
    $accArray[$iItem][$iSubitem][4] = $sFont
    $ahWndSets[$i][1] = $accArray ; write back to original array
    ; if SubItem not registered in IParam OR all values by -1 (delete Sub from IParam) ==> switch Sub value in IParam
    If (Not BitAND(_GUICtrlListView_GetItemParam($hWnd, $iItem), 2 ^ $iSubitem)) Or ($sumParam = 5) Then _
            _GUICtrlListView_SetItemParam($hWnd, $iItem, BitXOR(_GUICtrlListView_GetItemParam($hWnd, $iItem), 2 ^ $iSubitem))
    If BitAND(_GUICtrlListView_GetItemParam($hWnd, $iItem), 2 ^ $iSubitem) Then _WinAPI_InvalidateRect($hWnd) ; only if values changed
EndFunc   ;==>_SetItemParam

Func _DrawItemCol(ByRef $hDC, ByRef $tCustDraw, $hWnd, $iItem, $iSubitem)
    Local $accArray
    For $i = 0 To UBound($ahWndSets) - 1
        If $ahWndSets[$i][0] = $hWnd Then
            $accArray = $ahWndSets[$i][1]
            ExitLoop
        EndIf
    Next
    Local $aDefFont[14] = [14, 0, 0, 0, $FW_NORMAL, False, False, False, _
            $DEFAULT_CHARSET, $OUT_DEFAULT_PRECIS, $CLIP_DEFAULT_PRECIS, $DEFAULT_QUALITY, 0, 'Arial']
    $aDefFont[0] = $accArray[$iItem][$iSubitem][2]
    $aDefFont[4] = $accArray[$iItem][$iSubitem][3]
    $aDefFont[13] = $accArray[$iItem][$iSubitem][4]
    $hDC = DllStructGetData($tCustDraw, 'hdc')
    DllStructSetData($tCustDraw, 'clrText', RGB2BGR($accArray[$iItem][$iSubitem][1]))
    DllStructSetData($tCustDraw, 'clrTextBk', RGB2BGR($accArray[$iItem][$iSubitem][0]))
    $hFont = _WinAPI_CreateFont($aDefFont[0], $aDefFont[1], $aDefFont[2], $aDefFont[3], $aDefFont[4], $aDefFont[5], $aDefFont[6], _
            $aDefFont[7], $aDefFont[8], $aDefFont[9], $aDefFont[10], $aDefFont[11], $aDefFont[12], $aDefFont[13])
    _WinAPI_SelectObject($hDC, $hFont)
EndFunc   ;==>_DrawItemCol

Func _DrawDefault(ByRef $hDC, ByRef $tCustDraw)
    $hDC = DllStructGetData($tCustDraw, 'hdc')
    DllStructSetData($tCustDraw, 'clrText', RGB2BGR($defColLV))
    DllStructSetData($tCustDraw, 'clrTextBk', RGB2BGR($defBkColLV))
    $hFont = _WinAPI_CreateFont(14, 0, 0, 0, $FW_NORMAL, False, False, False, $DEFAULT_CHARSET, $OUT_DEFAULT_PRECIS, _
            $CLIP_DEFAULT_PRECIS, $DEFAULT_QUALITY, 0, 'Arial')
    _WinAPI_SelectObject($hDC, $hFont)
EndFunc   ;==>_DrawDefault

Func RGB2BGR($iColor)
    Local $sH = Hex($iColor, 6)
    ;return '0xFFAAFF'
    Return '0x' & StringRight($sH, 2) & StringMid($sH, 3, 2) & StringLeft($sH, 2)
EndFunc   ;==>RGB2BGR

Share this post


Link to post
Share on other sites



Could you just snip out the section of code causing you the trouble? Makes things much simpler to debug.

This is the whole code. I don't know which line causing the problem. just run it in auto-it and see the problem.

Share this post


Link to post
Share on other sites

bump.

Share this post


Link to post
Share on other sites

BUMP!!!!!!!!!!!!!!!!!

Share this post


Link to post
Share on other sites

Your script seems a bit complicate to me. So it's hard to debug.

Could you tell us in simple words what you try to achieve? Maybe there is a simpler solution to get what you need.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

I am trying to get a table using List-View that I can change colors of cells according to changes made by another function.

for example:

if 2+2=4 paint row-1 line-4. if 2+4=6 paint row-1 line-6.

Share this post


Link to post
Share on other sites

As Volly said, a short reproduction script would help. That allows us to make any changes simply, without trying to isolate all the other parts of your code and find out what belongs to the listview and what doesn't. After all, who can better isolate parts of a script than the person who wrote it?

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  
Followers 0