Jump to content
Sign in to follow this  
SkellySoul

List Box Selected

Recommended Posts

SkellySoul

Hey, I am trying to change the color of a "selected item in a normal list box" or even a way to get rid of the "blue highlight" color all together.

I found this code and it does exactly what I want, however I wish the code was much more simplified, if possible.

It also is using "_GUICtrlListView_Create" when I just want to use "GUICtrlCreateList"

So if its not possible with "GUICtrlCreateList" , is it possible to remove the blue select color all together.

#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
#include <WinAPI.au3>

Global Const $ODT_LISTVIEW = 102

Global Const $ODA_DRAWENTIRE = 0x1
Global Const $ODA_SELECT = 0x2
Global Const $ODA_FOCUS = 0x4

Global Const $ODS_SELECTED = 0x0001

$hGUI = GUICreate("Test GUI", 300, 200)

$hListView = _GUICtrlListView_Create($hGUI, "Items|SubItems", 10, 10, 280, 180, BitOR($LVS_REPORT, $LVS_OWNERDRAWFIXED))

_GUICtrlListView_SetExtendedListViewStyle($hListView, $LVS_EX_FULLROWSELECT)

For $i = 1 To 10
_GUICtrlListView_AddItem($hListView, "Item " & $i)
_GUICtrlListView_AddSubItem($hListView, $i - 1, "SubItem " & $i, 1)
Next

GUIRegisterMsg($WM_DRAWITEM, "WM_DRAWITEM")

GUISetState()

Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE

Func WM_DRAWITEM($hWnd, $Msg, $wParam, $lParam)
Local $tagDRAWITEMSTRUCT, $iBrushColor, $cID, $itmID, $itmAction, $itmState, $hItm, $hDC, $bSelected

$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_LISTVIEW 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")

$bSelected = BitAND($itmState, $ODS_SELECTED)

Switch $itmAction
     Case $ODA_DRAWENTIRE
         If $itmState = $bSelected Then
             $iBrushColor = 0xFFFFFF
         Else
             $iBrushColor = 0x0000FF
         EndIf

         Local $aBrush = DLLCall("gdi32.dll","hwnd","CreateSolidBrush", "int", $iBrushColor)

         Local $aBrushOld = _WinAPI_SelectObject($hDC, $aBrush[0])

         Local $iLeft = DllStructGetData($tagDRAWITEMSTRUCT, "itmRect", 1)
         DllStructSetData($tagDRAWITEMSTRUCT, "itmRect", $iLeft + 5, 1)

         _WinAPI_FillRect($hDC, DllStructGetPtr($tagDRAWITEMSTRUCT, "itmRect"), $aBrush[0])

         _WinAPI_SelectObject($hDC, $aBrushOld)

         _WinAPI_DeleteObject($aBrush[0])

         Local $itmText = _GUICtrlListView_GetItemText($hListView, $itmID)

         DllStructSetData($tagDRAWITEMSTRUCT, "itmRect", DllStructGetData($tagDRAWITEMSTRUCT, "itmRect", 1) + 2, 1)

         DllCall("user32.dll", "int", "DrawText", "hwnd", $hDC, "str", $itmText, "int", StringLen($itmText), _
                 "ptr", DllStructGetPtr($tagDRAWITEMSTRUCT, "itmRect"), "int", $DT_LEFT)

         Local $iSubItmText = _GUICtrlListView_GetItemText($hListView, $itmID, 1)
         Local $aSubItmRect = _GUICtrlListView_GetSubItemRect($hListView, $itmID, 1)

         Local $iSubItmRect = DllStructCreate("int[4]")

         DllStructSetData($iSubItmRect, 1, $aSubItmRect[0], 1)
         DllStructSetData($iSubItmRect, 1, $aSubItmRect[1], 2)
         DllStructSetData($iSubItmRect, 1, $aSubItmRect[2], 3)
         DllStructSetData($iSubItmRect, 1, $aSubItmRect[3], 4)

         DllCall("user32.dll", "int", "DrawText", "hwnd", $hDC, "str", $iSubItmText, "int", StringLen($iSubItmText), _
                 "ptr", DllStructGetPtr($iSubItmRect), "int", $DT_LEFT)
EndSwitch

Return $GUI_RUNDEFMSG
EndFunc

Thanks

Edited by SkellySoul

Share this post


Link to post
Share on other sites
Zedna

You have inconsistent questions.

You are talking about 3 diferent controls: ListBox (in title), ListView (example) and Edit (GUICtrlCreateInput).

What type of control do you really want to use?

Share this post


Link to post
Share on other sites
SkellySoul

You have inconsistent questions.

You are talking about 3 diferent controls: ListBox (in title), ListView (example) and Edit (GUICtrlCreateInput).

What type of control do you really want to use?

wow sorry, I just realized that. Forgive me, I meant "GUICtrlCreateList"

Edited by SkellySoul

Share this post


Link to post
Share on other sites
SkellySoul

I found exactly what I was looking for. You see I am making a very custom GUI that doesn't use any external resources such as, jpg, bmp, gif...

However, I ran into a problem with the solution I found. This will change the theme of all windows instead of just my GUI.

So if anyone is able to come up with a solution to this problem that would be great.

#include <WinAPI.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

_Test()

Func _Test()
    $Test = $COLOR_ACTIVECAPTION
    _XP_Style(1)
    GUICreate("My GUI", 300, 200)
    _XP_Style(0)
    $Save = _WinAPI_GetSysColor($Test)
    _WinAPI_SetSysColors($Test , 255)

    GUISetState()

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
        EndSwitch
    WEnd

    GUIDelete()

    _WinAPI_SetSysColors($Test, $Save)

    Exit
EndFunc

Func _XP_Style($OnOff = 1)
    $XS_n = ""
    If $OnOff And StringInStr(@OSTYPE, "WIN32_NT") Then
        $XS_n = DllCall("uxtheme.dll", "int", "GetThemeAppProperties")
        DllCall("uxtheme.dll", "none", "SetThemeAppProperties", "int", 0)
        Return 1
    ElseIf StringInStr(@OSTYPE, "WIN32_NT") And IsArray($XS_n) Then
        DllCall("uxtheme.dll", "none", "SetThemeAppProperties", "int", $XS_n[0])
        $XS_n = ""
        Return 1
    EndIf
    Return 0
EndFunc

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  

×