; Von BugFix mit leichten Modifikationen
; Siehe http://www.autoit.de/index.php?page=Thread&postID=171681#post171681

#include <GUIConstantsEx.au3>
#include <GuiListView.au3>
#include <StructureConstants.au3>
#include <WindowsConstants.au3>

Global $GuiWidth = 500, $GuiHeight = 300 ; GUI-Größe
Local $MaxCols = 10
Local $MaxRows = 1000

$gui = GUICreate('Test 2', $GuiWidth, $GuiHeight)
$ListView1 = GUICtrlCreateListView("", 15, 15, 300, 250)
$hLV = GUICtrlGetHandle(-1)

GUIRegisterMsg($WM_NOTIFY, 'WM_NOTIFY')

; add columns
For $c = 1 To $MaxCols
	_GUICtrlListView_InsertColumn($ListView1, $c - 1, "Column " & $c, 100)
Next
; fill content
Local $aToFill[$MaxRows][$MaxCols]
For $r = 0 To $MaxRows - 1
	For $c = 0 To $MaxCols - 1
		; $aToFill[$i][$c] = Random(10000, 1000000, 1)
		$aToFill[$r][$c] = "Row " & $r & ", Col " & $c & ", " & Random(0, 99999, 1)
	Next
Next

_GUICtrlListView_AddArray($hLV, $aToFill)

GUISetState()

; Absichtlich nach GuiSetState um zu sehen, ob _GUICtrlListView_BeginUpdate und ..EndUpdate funktioniert
SetOptimalColumnWidth()

Do
Until GUIGetMsg() = -3
GUIDelete($gui)

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")
	If $hWndFrom = $hLV Then
		Switch $iCode
			Case $NM_CUSTOMDRAW
				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')
						Switch Mod($iItem, 2)
							Case 0 ; geradzahlig
								DllStructSetData($tCustDraw, 'clrTextBk', 0x23FFFF)
							Case 1 ; ungeradzahlig
								DllStructSetData($tCustDraw, 'clrTextBk', 0x0044FF)
						EndSwitch
						Return $CDRF_NEWFONT
				EndSwitch
		EndSwitch
	EndIf
	Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY



; Funktion, die die Spaltenbreiten optimal breit macht, sodass der Text rein passt
Func SetOptimalColumnWidth()

	_GUICtrlListView_BeginUpdate($ListView1)
	For $c = 1 To $MaxCols
		_GUICtrlListView_SetColumnWidth($ListView1, $c - 1, $LVSCW_AUTOSIZE)
	Next
	_GUICtrlListView_EndUpdate($ListView1)

EndFunc   ;==>SetOptimalColumnWidth