#include <Constants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <WinAPI.au3>
#include <GUIListBox.au3>
#include <GUIImageList.au3>
; Example by rasim
; modified by Prog@ndy / MsCreatoR (Index storing issue, UDF merge, and tidy source :) )
Global Const $ODT_LISTBOX = 2
Global Const $ODA_DRAWENTIRE = 0x1
Global Const $ODA_SELECT = 0x2
Global Const $ODS_SELECTED = 0x1
$hGUI = GUICreate("ListBox with images - Demo!", 300, 200)
$hListBoxIMGList = _GUIImageList_Create(16, 16, 5, 1, 4, 90)
_GUIImageList_SetBkColor($hListBoxIMGList, $CLR_NONE)
GUIRegisterMsg($WM_MEASUREITEM, "WM_MEASUREITEM")
$ListBox = GUICtrlCreateList("", 10, 10, 280, 180, BitOR($LBS_HASSTRINGS, $LBS_OWNERDRAWFIXED, $WS_VSCROLL))
$hListBox = GUICtrlGetHandle($ListBox)
For $i = 1 To 10
_GUICtrlListBox_AddStringEx($ListBox, "String " & $i, $hListBoxIMGList, @SystemDir & "\shell32.dll", $i)
Next
GUIRegisterMsg($WM_DRAWITEM, "WM_DRAWITEM")
GUISetState()
Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE
Func _GUICtrlListBox_AddStringEx($hWnd, $sText, $hImageList = 0, $sIconFile = "", $iIndex = 0)
If $hImageList = 0 Then
Return _GUICtrlListBox_AddString($hWnd, $sText)
Else
Local $iIconIDX = _GUIImageList_AddIcon($hImageList, $sIconFile, $iIndex, 0)
Local $iStrIndex = _GUICtrlListBox_AddString($hWnd, $sText)
_GUICtrlListBox_SetItemData($hWnd, $iStrIndex, $iIconIDX)
Return $iStrIndex
EndIf
EndFunc
Func WM_MEASUREITEM($hWnd, $Msg, $wParam, $lParam)
Local $CtlType, $tagMEASUREITEMSTRUCT = "UINT CtlType;UINT CtlID;UINT itemID;UINT itemWidth;UINT itemHeight;ULONG_PTR itemData;"
Local $MEASUREITEMSTRUCT = DllStructCreate($tagMEASUREITEMSTRUCT, $lParam)
$CtlType = DllStructGetData($MEASUREITEMSTRUCT, "CtlType")
If $CtlType <> $ODT_LISTBOX Then Return $GUI_RUNDEFMSG
DllStructSetData($MEASUREITEMSTRUCT, 5, 18)
DllStructSetData($MEASUREITEMSTRUCT, 4, DllStructGetData($MEASUREITEMSTRUCT, 4) + 18)
Return
EndFunc
Func WM_DRAWITEM($hWnd, $Msg, $wParam, $lParam)
Local $tagDRAWITEMSTRUCT, $cID, $itmID, $itmAction, $itmState, $hItm, $hDC, $iBrushColor, $hBrush, $hBrushOld
$tagDRAWITEMSTRUCT = DllStructCreate("uint cType;uint cID;uint itmID;uint itmAction;uint itmState;" & _
"hwnd hItm;hwnd hDC;int itmRect[4];dword itmData", $lParam)
If DllStructGetData($tagDRAWITEMSTRUCT, "cType") <> $ODT_LISTBOX 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")
Switch $itmAction
Case $ODA_DRAWENTIRE, $ODA_SELECT
If $itmState = $ODS_SELECTED Then
$iBrushColor = 0x6495ED
Else
$iBrushColor = 0x66CDAA
EndIf
$hBrush = _WinAPI_CreateSolidBrush($iBrushColor)
$hBrushOld = _WinAPI_SelectObject($hDC, $hBrush)
DllCall("user32.dll", "int", "FillRect", _
"hwnd", $hDC, _
"ptr", DllStructGetPtr($tagDRAWITEMSTRUCT, "itmRect"), _
"hwnd", $hBrush)
_WinAPI_SelectObject($hDC, $hBrushOld)
_WinAPI_DeleteObject($hBrush)
DllCall("gdi32.dll", "int", "SetBkMode", "hwnd", $hDC, "int", 1)
Local $tBuffer = DllStructCreate("char[256]")
DllCall("user32.dll", "int", "SendMessage", _
"hwnd", $hItm, _
"int", $LB_GETTEXT, _
"int", $itmID, _
"ptr", DllStructGetPtr($tBuffer))
Local $itmText = DllStructGetData($tBuffer, 1)
Local $itmTextIMG = _GUICtrlListBox_GetItemData($hListBox, $itmID)
Local $tRECT = DllStructCreate("int Left;int Top;int Right;int Bottom")
DllStructSetData($tRECT, "Left", DllStructGetData($tagDRAWITEMSTRUCT, "itmRect", 1) + 18)
DllStructSetData($tRECT, "Top", DllStructGetData($tagDRAWITEMSTRUCT, "itmRect", 2))
DllStructSetData($tRECT, "Right", DllStructGetData($tagDRAWITEMSTRUCT, "itmRect", 3))
DllStructSetData($tRECT, "Bottom", DllStructGetData($tagDRAWITEMSTRUCT, "itmRect", 4))
DllCall("user32.dll", "int", "DrawText", "hwnd", $hDC, "str", $itmText, "int", StringLen($itmText), _
"ptr", DllStructGetPtr($tRECT), "int", $DT_LEFT)
Local $iX = DllStructGetData($tagDRAWITEMSTRUCT, "itmRect", 1) + 1
Local $iY = DllStructGetData($tagDRAWITEMSTRUCT, "itmRect", 2) + 1
Local $fIsSelected = ($itmState = $ODS_SELECTED) * 2
If $itmTextIMG >= 0 Then _GUIImageList_Draw($hListBoxIMGList, $itmTextIMG, $hDC, $iX, $iY, BitOR(1, $fIsSelected))
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc