Jump to content

[SOLVED] Listview initially not drawing background color


Recommended Posts

#include <GuiListView.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <WinAPI.au3>
#include <GuiEdit.au3>

Global $tText = DllStructCreate("wchar[100]")
Global $aItems[100000][10], $aColors[100][10]
Global $hGuui, $hListView
Global $hEdit, $hDC, $hBrush, $Item = -1, $SubItem = 0

$hGUI = GUICreate("ListView Subitems edit in place", 300, 200)
$hListView = _GUICtrlListView_Create($hGUI, "Items|SubItems|hwhw", 2, 2, 296, 196,  $LVS_REPORT, BitOR( $WS_EX_CLIENTEDGE, $LVS_EX_DOUBLEBUFFER, $LVS_EX_FULLROWSELECT ))

_GUICtrlListView_SetExtendedListViewStyle($hListView, $LVS_EX_GRIDLINES)
For $i = 1 To 10
    _GUICtrlListView_AddItem($hListView, "Item " & $i)
    _GUICtrlListView_AddSubItem($hListView, $i - 1, "SubItem " & $i, 1)
    _GUICtrlListView_AddSubItem($hListView, $i - 1, "SubItem " & $i, 2)

GUISetState(@SW_SHOW, $hGuui)

For $row = 0 To 10 Step +1
    For $col = 0 To 2 Step +1
        $aColors[$row][$col] = 0xCCCCFF
GUICtrlSendMsg( $hListView, $LVM_SETITEMCOUNT, 10, 0 )


Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg, $iwParam
    Local $tNMHDR, $hWndFrom, $iCode
    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = DllStructGetData($tNMHDR, "hWndFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    Switch $hWndFrom
        Case $hListView
            Switch $iCode
                Case $NM_DBLCLK ; used for sub item edit
                    Local $tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam)
                    $SubItem = DllStructGetData($tInfo, "SubItem")
                    Switch $SubItem
                        Case 0
                            _GUICtrlListView_EditLabel($hWndFrom, DllStructGetData($tInfo, "Index"))
                        Case Else
                            Local $aHit = _GUICtrlListView_SubItemHitTest($hListView)
                            If ($aHit[0] <> -1) And ($aHit[1] > 0) Then
                                $Item = $aHit[0]
                                ;$SubItem = $aHit[1]
                                Local $iSubItemText = _GUICtrlListView_GetItemText($hListView, $Item, $SubItem)
                                Local $iLen = _GUICtrlListView_GetStringWidth($hListView, $iSubItemText)
                                Local $aRect = _GUICtrlListView_GetSubItemRect($hListView, $Item, $SubItem)
                                $hEdit = _GUICtrlEdit_Create($hGUI, $iSubItemText, $aRect[0] + 6, $aRect[1], $iLen + 10, 17, BitOR($WS_CHILD, $WS_VISIBLE, $ES_AUTOHSCROLL, $ES_LEFT))
                                _GUICtrlEdit_SetSel($hEdit, 0, -1)
                                $hDC = _WinAPI_GetWindowDC($hEdit)
                                $hBrush = _WinAPI_CreateSolidBrush(0)
                                ;FrameRect($hDC, 0, 0, $iLen + 10, 17, $hBrush)
                Case $NM_CUSTOMDRAW
                    Local $tNMLVCUSTOMDRAW = DllStructCreate($tagNMLVCUSTOMDRAW, $ilParam)
                    Local $dwDrawStage = DllStructGetData($tNMLVCUSTOMDRAW, "dwDrawStage")

                    Switch $dwDrawStage ; Holds a value that specifies the drawing stage

                        Case $CDDS_PREPAINT
                            ; Before the paint cycle begins
                            Return $CDRF_NOTIFYITEMDRAW ; Notify the parent window of any item-related drawing operations

                        Case $CDDS_ITEMPREPAINT
                            ; Before painting an item
                            Return $CDRF_NOTIFYSUBITEMDRAW ; Notify the parent window of any subitem-related drawing operations

                        Case BitOR($CDDS_ITEMPREPAINT, $CDDS_SUBITEM)
                            ; Before painting a subitem
                            ;Local $iItem = DllStructGetData($tNMLVCUSTOMDRAW, "dwItemSpec")                ; Item index
                            ;Local $iSubItem = DllStructGetData($tNMLVCUSTOMDRAW, "iSubItem")               ; Subitem index
                            ;DllStructSetData( $tNMLVCUSTOMDRAW, "ClrTextBk", $aColors[$iItem][$iSubItem] ) ; Backcolor of item/subitem
                            DllStructSetData($tNMLVCUSTOMDRAW, "ClrTextBk", $aColors[DllStructGetData($tNMLVCUSTOMDRAW, "dwItemSpec")][DllStructGetData($tNMLVCUSTOMDRAW, "iSubItem")])
                            Return $CDRF_NEWFONT ; $CDRF_NEWFONT must be returned after changing font or colors


EndFunc   ;==>WM_NOTIFY

Func FrameRect($hDC, $nLeft, $nTop, $nRight, $nBottom, $hBrush)
    Local $stRect = DllStructCreate("int;int;int;int")
    DllStructSetData($stRect, 1, $nLeft)
    DllStructSetData($stRect, 2, $nTop)
    DllStructSetData($stRect, 3, $nRight)
    DllStructSetData($stRect, 4, $nBottom)
    DllCall("user32.dll", "int", "FrameRect", "hwnd", $hDC, "ptr", DllStructGetPtr($stRect), "hwnd", $hBrush)
EndFunc   ;==>FrameRect

Func WM_COMMAND($hWnd, $Msg, $wParam, $ilParam)
    Local $iCode = BitShift($wParam, 16)
    Switch $ilParam
        Case $hEdit
            Switch $iCode
                Case $EN_KILLFOCUS
                    Local $iText = _GUICtrlEdit_GetText($hEdit)
                    _GUICtrlListView_SetItemText($hListView, $Item, $iText, $SubItem)
                    _WinAPI_ReleaseDC($hEdit, $hDC)
                    $Item = -1
                    $SubItem = 0
EndFunc   ;==>WM_COMMAND

The listview initially appears white and becomes colored only after it is placed below another window. (I'm using XP SP3.)


PS:  I don't own the script, Got this somewhere else in this site but I failed to save the source, sorry.

Edited by Mingre
Link to comment
Share on other sites

  • Moderators


Move the GUIRegisterMsg lines to before the GUISetState line - that way the handlers can do the colouring as the GUI is first displayed. As it is they only take affect on the first redraw after the initial display.


Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:


ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area


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

  • Recently Browsing   0 members

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