﻿id	summary	reporter	owner	description	type	status	milestone	component	version	severity	resolution	keywords	cc
2927	Adding Items with GUICtrlCreateListViewItem will cause GUI freeze if a subitem is empty	Trolleule		"When i add listview items with GUICtrlCreateListViewItem to an ownerdraw listview control and there is a empty subitem in the subitem chain and WM_NOTIFY is present, the LV control freezes. Adding items with UDF works.

I noticed that even when the display of the LV is frozen, the WM_NOTIFY function is still active and responding. I put a consolewrite in the WM_NOTIFY function and could see writing after the listview display froze. Surprisingly, minimizing the GUI unfreezes the listview and all controls on the GUI.

{{{
#include <GuiListView.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <ColorConstants.au3>


Global Const $ODT_LISTVIEW = 102
Global Const $ODA_DRAWENTIRE = 0x1
Global Const $ODA_SELECT = 0x2
Global Const $ODA_FOCUS = 0x4
Global Const $ODS_SELECTED = 0x0001


Global $GUI_main = GUICreate("""", 300, 300, -1, -1)
Global $hGUI_tab_listview[2][10]


$hGUI_tab_listview[0][0] = GUICtrlCreateListView(""A|B|C"", 15, 10, 250, 280, _
  BitOR($LVS_REPORT, $LVS_SHOWSELALWAYS, $LVS_OWNERDRAWFIXED, $WS_BORDER, $WS_CLIPCHILDREN), 0) ; + $LVS_EX_CHECKBOXES + $LVS_SINGLESEL
_GUICtrlListView_SetExtendedListViewStyle($hGUI_tab_listview[0][0], $LVS_EX_DOUBLEBUFFER)


For $i = 0 To 50
;~  _GUICtrlListView_AddItem(-1, ""test "" & $i)
;~  _GUICtrlListView_AddSubItem(-1, $i, $i, 1)
    If $i = 5 Then
        GUICtrlCreateListViewItem(""|"" & ""sdfdsfs"" & ""|"" & """", $hGUI_tab_listview[0][0]) ; if empty slot is present, gui gets freezed ; comment this lines out for check
        ContinueLoop
    EndIf
    GUICtrlCreateListViewItem(""|"" & ""sdfdsfs"" & ""|"" & $i, $hGUI_tab_listview[0][0])
Next

GUIRegisterMsg($WM_DRAWITEM, ""WM_DRAWITEM"")
GUIRegisterMsg($WM_NOTIFY, ""WM_NOTIFY"")
GUISetState(@SW_SHOW)


; Loop until the user exits.
While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE, $idOK
            ExitLoop
    EndSwitch
WEnd


Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    Return $GUI_RUNDEFMSG
EndFunc     ;==>WM_NOTIFY


Func WM_DRAWITEM($hWnd, $Msg, $wParam, $lParam)
    Local $tagDRAWITEMSTRUCT, $iBrushColor, $cID, $itmID, $itmAction, $itmState, $hItm, $hDC, $bSelected
    $tagDRAWITEMSTRUCT = DllStructCreate( _
       ""uint cType;"" & _
       ""uint cID;"" & _
       ""uint itmID;"" & _
       ""uint itmAction;"" & _
       ""uint itmState;"" & _
       ""hwnd hItm;"" & _
       ""handle hDC;"" & _
       ""long itmRect[4];"" & _
       ""ulong_ptr itmData"" _
       , $lParam)
    If DllStructGetData($tagDRAWITEMSTRUCT, ""cType"") <> $ODT_LISTVIEW Then Return $GUI_RUNDEFMSG
    $cID = DllStructGetData($tagDRAWITEMSTRUCT, ""cID"")
    $itmID = DllStructGetData($tagDRAWITEMSTRUCT, ""itmID"")
    $itmAction = DllStructGetData($tagDRAWITEMSTRUCT, ""itmAction"")
    $itmState = DllStructGetData($tagDRAWITEMSTRUCT, ""itmState"")
    $hItm = DllStructGetData($tagDRAWITEMSTRUCT, ""hItm"")
    $hDC = DllStructGetData($tagDRAWITEMSTRUCT, ""hDC"")
    $bSelected = BitAND($itmState, $ODS_SELECTED)
    Switch $cID ; will look for ControlID, not window handle.
      Case $hGUI_tab_listview[0][0]
       Switch $itmAction
        Case $ODA_DRAWENTIRE
         ; don't forget, this is BGR, not RGB
         If $itmState = $bSelected Then ; item is not selected
          $iBrushColor = 0xEEDDBB
         Else ; item is selected
          $iBrushColor = $COLOR_RED
         EndIf
         Local $aBrush = _WinAPI_CreateSolidBrush($iBrushColor)
         Local $aBrushOld = _WinAPI_SelectObject($hDC, $aBrush)
         Local $iLeft = DllStructGetData($tagDRAWITEMSTRUCT, ""itmRect"", 1)
         DllStructSetData($tagDRAWITEMSTRUCT, ""itmRect"", $iLeft + 1, 1) ; rectangle coordinates for coloring
         ; +1 is the left margin
         _WinAPI_FillRect($hDC, DllStructGetPtr($tagDRAWITEMSTRUCT, ""itmRect""), $aBrush)
         _WinAPI_SelectObject($hDC, $aBrushOld)
         _WinAPI_DeleteObject($aBrush)

         ; for all columns of the row:
         $local_alignment = $DT_LEFT
         For $i = 0 To _GUICtrlListView_GetColumnCount($hGUI_tab_listview[0][0]) - 1
          ; 1. get subitem text:
          Local $iSubItmText = _GUICtrlListView_GetItemText($hGUI_tab_listview[0][0], $itmID, $i)
          ; If $iSubItmText = """" Then ContinueLoop ;ConsoleWrite(""hier"")
          ; 2. get subitem coordinates for drawing its respective text
          Local $aSubItmRect = _GUICtrlListView_GetSubItemRect($hGUI_tab_listview[0][0], $itmID, $i)
          ; the function above accepts not only subitems (one-based index), but also main item (index=0)
          ; 3. pass the coordinates to a DLL struct
          Local $iSubItmRect = DllStructCreate(""long[4]"")
          DllStructSetData($iSubItmRect, 1, $aSubItmRect[0] + 6, 1) ; +6 is left margin (X)
          DllStructSetData($iSubItmRect, 1, $aSubItmRect[1] + (-2), 2) ; + (-2) is upper margin (Y)
          DllStructSetData($iSubItmRect, 1, $aSubItmRect[2], 3)
          DllStructSetData($iSubItmRect, 1, $aSubItmRect[3], 4)
          _WinAPI_SetTextColor($hDC, $COLOR_RED)

          DllCall(""user32.dll"", ""int"", ""DrawTextW"", ""hwnd"", $hDC, ""wstr"", $iSubItmText, ""int"", StringLen($iSubItmText), _
            ""ptr"", DllStructGetPtr($iSubItmRect), ""int"", $local_alignment)
         Next
         ;#ce
       EndSwitch
    EndSwitch
Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_DRAWITEM
}}}"	Bug	closed		AutoIt	3.3.12.0	None	Works For Me	GUICtrlCreateListViewItem, WM_NOTIFY, WM_DRAWITEM	
