Jump to content

ListBox Set Selection Color


Recommended Posts

My companys new "color" is a dark blue, so I am trying to make my new GUI as color standard as possible, but I'm having one problem.

I have set the back color of a Listbox as dark blue and the text color as white. The issue is that when you select an item in the listbox you can barely see the selection because it's such a close color to the controls back color.

Is there any way to specify the highlighted color?

Thanks,

Mike

Link to comment
Share on other sites

I think this topic belongs to GUI help and support forum.

#include <GUIConstants.au3>

Opt("GUIOnEventMode",1)

Global Const $WM_MEASUREITEM = 0x2C
Global Const $WM_DRAWITEM = 0x2B
Global Const $LBS_OWNERDRAWFIXED = 0x10
Global Const $LBS_HASSTRINGS = 0x40

Global $iListBkColor = 0x0000CC
Global $iListSelColor = 0xCCCCFF

$hGui = GuiCreate("Ownerdraw Listbox demo",300,200)
GUISetOnEvent($GUI_EVENT_CLOSE, "SysEvents")

GUIRegisterMsg($WM_MEASUREITEM, "MsgEvents")
GUIRegisterMsg($WM_DRAWITEM, "MsgEvents")

$cLBox1 = GUICtrlCreateList("", 10,25,135,170, $LBS_SORT+$WS_BORDER+$WS_VSCROLL+$LBS_OWNERDRAWFIXED+$LBS_HASSTRINGS)
GUICtrlSetData(-1, "1a|1b|1c|1d|1e")
GUICtrlSetBkColor(-1, $iListBkColor)

$cLBox2 = GUICtrlCreateList("", 155,25,135,170)
GUICtrlSetData(-1, "2a|2b|2c|2d|2e")
GUICtrlSetBkColor(-1, $iListBkColor)
;~ GUICtrlSetState(-1, $GUI_FOCUS)

$lbl1 = GUICtrlCreateLabel("Ownerdraw:", 10, 5, 135, 15)
$lbl1 = GUICtrlCreateLabel("Normal:", 155, 5, 135, 15)

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

Func SysEvents()
    Switch @GUI_CtrlId
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
EndFunc

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)
;draw item text
                    DllCall("user32.dll", "int", "DrawText", _
                    "hwnd", $hDC, _
                    "str", $itmText, "int", StringLen($itmText), _
                    "ptr", DllStructGetPtr($tRECT), _
                    "int", $DT_RIGHT)
                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

"be smart, drink your wine"

Link to comment
Share on other sites

Siao,

That works great to change the color but one little question.

Why is the text right alligned now? I can't find any variable that I can set to have the listbox Left alligned without breaking the color change.

Thanks,

Mike

#include <GUIConstants.au3>

Opt("GUIOnEventMode",1)

Global Const $WM_MEASUREITEM = 0x2C
Global Const $WM_DRAWITEM = 0x2B
Global Const $LBS_OWNERDRAWFIXED = 0x10
Global Const $LBS_HASSTRINGS = 0x40

Global $iListBkColor = 0x0000CC
Global $iListSelColor = 0xCCCCFF

$hGui = GuiCreate("Ownerdraw Listbox demo",300,200)
GUISetOnEvent($GUI_EVENT_CLOSE, "SysEvents")

GUIRegisterMsg($WM_MEASUREITEM, "MsgEvents")
GUIRegisterMsg($WM_DRAWITEM, "MsgEvents")

$cLBox1 = GUICtrlCreateList("", 10,25,135,170, $LBS_SORT+$WS_BORDER+$WS_VSCROLL+$LBS_OWNERDRAWFIXED+$LBS_HASSTRINGS)
GUICtrlSetData(-1, "1a|1b|1c|1d|1e")
GUICtrlSetBkColor(-1, $iListBkColor)

$cLBox2 = GUICtrlCreateList("", 155,25,135,170)
GUICtrlSetData(-1, "2a|2b|2c|2d|2e")
GUICtrlSetBkColor(-1, $iListBkColor)
;~ GUICtrlSetState(-1, $GUI_FOCUS)

$lbl1 = GUICtrlCreateLabel("Ownerdraw:", 10, 5, 135, 15)
$lbl1 = GUICtrlCreateLabel("Normal:", 155, 5, 135, 15)

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

Func SysEvents()
    Switch @GUI_CtrlId
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
EndFunc

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)
;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

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

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...