Jump to content

Colored Text Lisbox


Recommended Posts

Hi,

I have found some code which allows colour text entries in Listbox.

#include <GUIConstants.au3>
#include <GuiListBox.au3>


Global $iListBkColor = 0xFFFFFF
Global $iListSelColor = 0x326496

$hGui = GuiCreate("Ownerdraw Listbox demo",300,200)
GUIRegisterMsg($WM_MEASUREITEM, "MsgEvents")
GUIRegisterMsg($WM_DRAWITEM, "MsgEvents")

$cLBox1 = GUICtrlCreateList("", 10,25,135,170, $LBS_OWNERDRAWFIXED+$LBS_HASSTRINGS)
GUICtrlSetFont(-1, 12, 800, 0, "Arial Narrow")
GUICtrlSetData(-1, "1a|1b|1c|1d|1e")


GUISetState(@SW_SHOW)
While 1
    Sleep(1000)
WEnd

Func MsgEvents($hWnd, $Msg, $wParam, $lParam)
    Local Const $NM_CUSTOMDRAW = -12
    Local Const $ODT_LISTBOX = 2
    Local Const $ODA_DRAWENTIRE = 0x1
    Local Const $ODA_FOCUS = 0x4
    Local Const $ODA_SELECT = 0x2
    Local Const $ODS_SELECTED = 0x1
    Local Const $DT_RIGHT = 0x2
    Local Const $DT_LEFT = 0x0

    Switch $Msg
        Case $WM_MEASUREITEM
            $tMEASUREITEMS = DllStructCreate("uint cType;uint cID;uint itmID;uint itmW;uint itmH;ptr itmData", $lParam)
            DllStructSetData($tMEASUREITEMS, "itmH", 13)
            Return 1
         Case $WM_DRAWITEM
               $tDRAWITEMS = DllStructCreate("uint cType;" & _
                                "uint cID;" & _
                                "uint itmID;" & _
                                "uint itmAction;" & _
                                "uint itmState;" & _
                                "hwnd hItm;" & _
                                "hwnd hDC;" & _
                                "dword itmRect[4];" & _
                                "dword itmData", _
                                $lParam)
            If DllStructGetData($tDRAWITEMS, "cType") <> $ODT_LISTBOX Then Return $GUI_RUNDEFMSG
            $cID = DllStructGetData($tDRAWITEMS, "cID")
            $itmID = DllStructGetData($tDRAWITEMS, "itmID")
            $itmAction = DllStructGetData($tDRAWITEMS, "itmAction")
            $itmState = DllStructGetData($tDRAWITEMS, "itmState")
            $hItm = DllStructGetData($tDRAWITEMS, "hItm")
            $hDC = DllStructGetData($tDRAWITEMS, "hDC")
            Switch $itmAction
                Case $ODA_DRAWENTIRE, $ODA_SELECT
                    $tRECT = DllStructCreate("int;int;int;int",DllStructGetPtr($tDRAWITEMS, "itmRect"))
;select bkground color
                    If $itmState = $ODS_SELECTED Then
                        $iBrushColor = RGB2BGR($iListSelColor)
                    Else
                        $iBrushColor = RGB2BGR($iListBkColor)
                  EndIf






;draw item bkground
                    $aBrush = DLLCall("gdi32.dll","hwnd","CreateSolidBrush", "int", $iBrushColor)
                    $aBrushOld = DLLCall("gdi32.dll","hwnd","SelectObject", "hwnd", $hDC, "hwnd", $aBrush[0])
                    DLLCall("user32.dll","int","FillRect", "hwnd", $hDC, "ptr", DllStructGetPtr($tRECT), "hwnd", $aBrush[0])
                    DLLCall("gdi32.dll","hwnd","SelectObject", "hwnd", $hDC, "hwnd", $aBrushOld[0])
                    DLLCall("gdi32.dll","int","DeleteObject", "hwnd", $aBrush[0])
                  DLLCall("gdi32.dll","int","SetBkMode", "hwnd", $hDC, "int", 1)
;get the text to be drawn
                    $tBuffer = DllStructCreate("char[4096]")
                    DllCall("user32.dll", "int", "SendMessage", "hwnd", $hItm, "int", 0x189, "int", $itmID, "ptr", DllStructGetPtr($tBuffer))
                    $itmText = DllStructGetData($tBuffer, 1)
;text indent
                    DllStructSetData($tRECT, 1, DllStructGetData($tRECT, 1)+2)
                    DllStructSetData($tRECT, 3, DllStructGetData($tRECT, 3)-2)


                     Local $Temp = _GUICtrlListBox_GetText($cLBox1, $itmID)
                     If StringInStr($Temp, "1a") then
                        DLLCall("gdi32.dll","int","SetTextColor", "hwnd", $hDC, "int", RGB2BGR(0xED1C24)) ;Red
                     elseif StringInStr($Temp, "1b") then
                        DLLCall("gdi32.dll","int","SetTextColor", "hwnd", $hDC, "int", RGB2BGR(0xFF7F27)) ;Orange
                     elseif StringInStr($Temp, "1C") Then
                        DLLCall("gdi32.dll","int","SetTextColor", "hwnd", $hDC, "int", RGB2BGR(0x22B14C));Green
                     Else
                        DLLCall("gdi32.dll","int","SetTextColor", "hwnd", $hDC, "int", RGB2BGR(0xED1C24));Green
                     EndIf

;draw item text
                    DllCall("user32.dll", "int", "DrawText", _
                    "hwnd", $hDC, _
                    "str", $itmText, "int", StringLen($itmText), _
                    "ptr", DllStructGetPtr($tRECT), _
                    "int", $DT_LEFT)


                Case $ODA_FOCUS
                  ;enable for focus rectangle
                    ; Return $GUI_RUNDEFMSG
            EndSwitch
            Return 1

    EndSwitch
EndFunc

Func RGB2BGR($iColor)
    Return BitAND(BitShift(String(Binary($iColor)), 8), 0xFFFFFF)
EndFunc

I don't understand how the Dllcalls work, but I think the one that controls how big the draws are is

$tRECT = DllStructCreate("int;int;int;int",DllStructGetPtr($tDRAWITEMS, "itmRect"))

If that is correct, how would I change this to make it "bigger". As I am finding when I increase text size causes it to clip.

Would anyone know how to fix this?

Ian

Edited by IanN1990
Link to comment
Share on other sites

This line defines the height of the listbox items:

DllStructSetData($tMEASUREITEMS, "itmH", 13)

Replace 13 with the height you want.

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
 Share

  • Recently Browsing   0 members

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