Jump to content
Sign in to follow this  

Icons in List/ListBox by rasim, Prog@ndy, MrCreatoR, cyberbit

Recommended Posts


Just thought I'd repost this as an FYI. This was >originally in the GUI Help/Support thread, but now that it's solved it might as well be in here.

I modified the WM_DRAWITEM brush/text colors to reflect the system's theme, so the List control looks more accurate in context with the OS.  ;)

#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Constants.au3>
#include <GuiListBox.au3>
#include <GuiImageList.au3>
#include <WinAPI.au3>

; Example by rasim
; modified by Prog@ndy / MsCreatoR (Index storing issue, UDF merge, and tidy source :) )
; Selection color mod by cyberbit

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, 6, 1, 4, 90)
_GUIImageList_SetBkColor($hListBoxIMGList, $CLR_NONE)


$ListBox = GUICtrlCreateList("", 10, 10, 280, 180, BitOR($LBS_HASSTRINGS, $LBS_OWNERDRAWFIXED, $WS_VSCROLL))
$hListBox = GUICtrlGetHandle($ListBox)

For $i = 1 To 10
    _GUICtrlListBox_AddStringEx($hListBox, "String " & $i, $hListBoxIMGList, @SystemDir & "\shell32.dll", $i)




Func _GUICtrlListBox_AddStringEx($hWnd, $sText, $hImageList = 0, $sIconFile = "", $iIndex = 0)
    If $hImageList = 0 Then
        Return _GUICtrlListBox_AddString($hWnd, $sText)
        Local $iIconIDX = _GUIImageList_AddIcon($hImageList, $sIconFile, $iIndex, 0)
        Local $iStrIndex = _GUICtrlListBox_AddString($hWnd, $sText)
        _GUICtrlListBox_SetItemData($hWnd, $iStrIndex, $iIconIDX)
        Return $iStrIndex

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)

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
            If $itmState = $ODS_SELECTED Then
                $iBrushColor = _WinAPI_GetSysColor($COLOR_HIGHLIGHT)
               _WinAPI_SetTextColor($hDC, _WinAPI_GetSysColor($COLOR_HIGHLIGHTTEXT))
                $iBrushColor = _WinAPI_GetSysColor($COLOR_WINDOW)
               _WinAPI_SetTextColor($hDC, _WinAPI_GetSysColor($Color_WINDOWTEXT))
            $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)
            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))



Edited by cyberbit

_ArrayConcatenate2D · Aero Flip 3D · EPOCH (destroy timestamps) · File Properties Modifier · _GetFileType · UpdateEngine<new> · In-line Case (_ICase) <new>


50% of the time, it works all the time. -PezoFaSho

Share this post

Link to post
Share on other sites

You should change the title, this is a Listbox, not a listview. :)

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

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  


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.