Jump to content
Sign in to follow this  
DigDeep

Color Text in List view and single click

Recommended Posts

Hi,

Can someone please help me here.

I would like to color the Texts in Green and be able to do single click option.

I am able to get the underline for the texts but color and single click are not working.

Local $g_hDC = _WinAPI_GetDC(0) ; Control for Green text

    Local $hFont1 = _WinAPI_CreateFont(13.5, 6.5, 0, 0, $FW_BOLD, False, False, False, _
            $DEFAULT_CHARSET, $OUT_DEFAULT_PRECIS, $CLIP_DEFAULT_PRECIS, $PROOF_QUALITY, $DEFAULT_PITCH, 'Verdana')
    Local $hFont2 = _WinAPI_CreateFont(13.5, 6, 0, 0, $FW_Medium, False, True, False, _
            $DEFAULT_CHARSET, $OUT_DEFAULT_PRECIS, $CLIP_DEFAULT_PRECIS, $PROOF_QUALITY, $DEFAULT_PITCH, 'Verdana')

    $ListLable = GUICtrlCreateListView("  |  A|   B|  C|  D|", 200, 310, 505, 200, -1, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT))
    _GUICtrlListView_SetColumnWidth($ListLable, 0, 153)
    _GUICtrlListView_SetColumnWidth($ListLable, 1, 87)
    $hLV_Handle = GUICtrlGetHandle($ListLable)

    $hHeader = HWnd(_GUICtrlListView_GetHeader($hLV_Handle)) ; get handle to header control

    ; or set different font for header and items
    _WinAPI_SetFont($hLV_Handle, $hFont2, True)
    _WinAPI_SetFont($hHeader, $hFont1, True)

    Local $g_hOldFont = _WinAPI_SelectObject($g_hDC, $hFont2) ; Control for Green text
    _WinAPI_SetTextColor($g_hDC, 0x008000) ; Control for Green text

    ; Add items
    _GUICtrlListView_AddItem($ListLable, "Test 1", 0)
    _GUICtrlListView_AddItem($ListLable, "Test 2", 1)
    _GUICtrlListView_AddItem($ListLable, "Test 3", 2)
    _GUICtrlListView_AddItem($ListLable, "Test 4", 3)

 

Edited by DigDeep

Share this post


Link to post
Share on other sites

And the question is?
To be more precise :) Did you search the forum? IIRC this subject has just been asked / answered a few hours ago.

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2020-09-05 - Version 1.5.1.1) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2020-06-27 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (NEW 2020-06-27 - Version 1.3.2.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
Task Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - Wiki

Tutorials:
ADO - Wiki, WebDriver - Wiki

 

Share this post


Link to post
Share on other sites

Hey

 

This is a cool topic. I am by no means an expert, but have struggled with this in the past.  Sadly, I have not figured it out yet.

However, what I want to ask you is this 

  • Have you tried changing the parameters when creating your ListView?
  • How do you detect the click?
  • Which update functions have you used on the ListView Items?

Skysnake


Skysnake

Why is the snake in the sky?

Share this post


Link to post
Share on other sites

Did you want to color whole line with same color use native GUICtrlCreateListView, GUICtrlCreateListViewItem and GUICtrlSetColor.

With UDF created LV it is possible to color each subitem different:

;https://autoit.de/index.php/Thread/84279-Unterschiedliche-Textfarben-innerhalb-einer-ListView/?postID=674295#post674295
#include <GUIConstantsEx.au3>
#include <GuiListView.au3>
#include <array.au3>
#include <ListViewConstants.au3>
#include <StructureConstants.au3>
#include <WindowsConstants.au3>
#include <FontConstants.au3>

Global $iDllGDI = DllOpen("gdi32.dll")

$aColBK = IniReadSection(@ScriptDir & "\lv_format.ini", "ColBKColor")
_ArrayDelete($aColBK, 0)
;_ArrayDisplay($aColBK)
$aRowBK = IniReadSection(@ScriptDir & "\lv_format.ini", "RowBKColor")
_ArrayDelete($aRowBK, 0)
;_ArrayDisplay($aColBK)
$aColText = IniReadSection(@ScriptDir & "\lv_format.ini", "ColTextColor")
_ArrayDelete($aColText, 0)
;_ArrayDisplay($aColBK)
$aRowText = IniReadSection(@ScriptDir & "\lv_format.ini", "RowTextColor")
_ArrayDelete($aRowText, 0)
;_ArrayDisplay($aRowText)

Global $bColorHeader = True ;wenn true werden auch die Spaltenüberschriften mit eingefärbt.


$GUI = GUICreate("Listview Farbig", 1024, 300, 0, 0) ;<==== Breite geändert
$cListView = GUICtrlCreateListView("", 2, 2, 1020, 294, -1, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT)) ;<==== Breite geändert
;_GUICtrlListView_SetExtendedListViewStyle($cListView, $LVS_EX_FULLROWSELECT) ; BitOR($LVS_EX_DOUBLEBUFFER, $LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT))
$hListView = GUICtrlGetHandle($cListView)
For $i = 1 To 31
    _GUICtrlListView_InsertColumn($hListView, $i - 1, $i, 25)
    _GUICtrlListView_SetColumnWidth($hListView, $i - 1, 32) ;<==== eingefügt für Spaltenbreite
Next
;get handle to child SysHeader32 control of ListView
Global $hHeader = HWnd(GUICtrlSendMsg($cListView, $LVM_GETHEADER, 0, 0))
;Turn off theme for header
DllCall("uxtheme.dll", "int", "SetWindowTheme", "hwnd", $hHeader, "wstr", "", "wstr", "")

; Get the font of the Header control (credit KaFu)
Local $hDC = _WinAPI_GetDC($hHeader)
Local $hFont = _SendMessage($hHeader, $WM_GETFONT)
Local $hObject = _WinAPI_SelectObject($hDC, $hFont)
Local $tLogFont = DllStructCreate($tagLOGFONT)

_WinAPI_GetObject($hFont, DllStructGetSize($tLogFont), DllStructGetPtr($tLogFont))
_WinAPI_SelectObject($hDC, $hObject)
_WinAPI_ReleaseDC($hHeader, $hDC)

Local $iWeight = DllStructGetData($tLogFont, "Weight") ; Bold
DllStructSetData($tLogFont, "Weight", BitOR($iWeight, $FW_BOLD))
$hHdrFont = _WinAPI_CreateFontIndirect($tLogFont)


For $i = 0 To 21 ; alle Item/SubItem erstellen
    _GUICtrlListView_AddItem($hListView, "Item: " & $i, $i)
    For $j = 1 To 30
        _GUICtrlListView_AddSubItem($hListView, $i, "Sub: " & $j, $j)
    Next
Next
If $bColorHeader Then
    $iCols = _GUICtrlListView_GetColumnCount($hListView)
    Global Const $tagNMCUSTOMDRAW = "struct;" & $tagNMHDR & ";dword dwDrawStage;handle hdc;" & $tagRECT & _
            ";dword_ptr dwItemSpec;uint uItemState;lparam lItemlParam;endstruct"

    Global $aHdrData[$iCols][3] ;Headerdata erstellen wird beim einfärben benötigt
    For $i = 0 To $iCols - 1
        $aHdrData[$i][0] = _GUICtrlListView_GetColumn($hListView, $i)[5]
        $iIndex = _ArraySearch($aColText, $i, 0, 0, 0, 0, 1, 0)
        If $iIndex <> -1 Then
            $aHdrData[$i][1] = RGB2BGR($aColText[$iIndex][1])
        Else
            $aHdrData[$i][1] = 0x000000
        EndIf
        $iIndex = _ArraySearch($aColBK, $i, 0, 0, 0, 0, 1, 0)
        If $iIndex <> -1 Then
            $aHdrData[$i][2] = RGB2BGR($aColBK[$iIndex][1])
        Else
            $aHdrData[$i][2] = 0xEEEEFF
        EndIf
    Next
EndIf

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
GUISetState()

While True
    $msg = GUIGetMsg()
    Switch $msg
        Case -3
            ExitLoop
    EndSwitch
WEnd

Func WM_NOTIFY($hWnd, $msg, $wParam, $lParam)
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR
    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $hWndFrom
        Case $hHeader
            If $bColorHeader Then
                ;ConsoleWrite('Header: ' & $iCode & @CRLF)
                Switch $iCode
                    Case $NM_CUSTOMDRAW
                        Local $tNMCustomDraw = DllStructCreate($tagNMCUSTOMDRAW, $lParam)
                        Local $dwDrawStage = DllStructGetData($tNMCustomDraw, "dwDrawStage")
                        Switch $dwDrawStage ; Holds a value that specifies the drawing stage
                            Case $CDDS_PREPAINT ; Before the paint cycle begins
                                Return $CDRF_NOTIFYITEMDRAW ; Notify parent window of any item related drawing operations

                            Case $CDDS_ITEMPREPAINT ; Before an item is drawn: Default painting (frames and background)
                                Return $CDRF_NOTIFYPOSTPAINT ; Notify parent window of any post item related drawing operations

                            Case $CDDS_ITEMPOSTPAINT ; After an item is drawn: Custom painting (item texts)
                                Local $tRECT = DllStructCreate($tagRECT)
                                Local $iIndex = DllStructGetData($tNMCustomDraw, "dwItemSpec") ; Item index
                                Local $hDC = DllStructGetData($tNMCustomDraw, "hdc") ; Device context
                                _WinAPI_SelectObject($hDC, $hHdrFont) ; Set text font
                                _WinAPI_SetBkMode($hDC, $TRANSPARENT) ; Transparent background
                                _WinAPI_SetTextColor($hDC, $aHdrData[$iIndex][1]) ; Set text colour
                            ; Get header section size
                            DllStructSetData($tRECT, 1, DllStructGetData($tNMCustomDraw, 6) + 1)
                            DllStructSetData($tRECT, 2, DllStructGetData($tNMCustomDraw, 7))
                            DllStructSetData($tRECT, 3, DllStructGetData($tNMCustomDraw, 8) - 2)
                            DllStructSetData($tRECT, 4, DllStructGetData($tNMCustomDraw, 9) - 2)
                            ; Set and draw back colour
                            Local $hBrush = _WinAPI_CreateSolidBrush($aHdrData[$iIndex][2])
                                _WinAPI_FillRect($hDC, $tRECT, $hBrush)
                                ; Write text
                                If $iIndex < _GUICtrlListView_GetColumnCount($cListView) Then
                                    _WinAPI_DrawText($hDC, $aHdrData[$iIndex][0], $tRECT, $DT_CENTER + $DT_VCENTER)
                                EndIf
                                Return $CDRF_NEWFONT ; $CDRF_NEWFONT must be returned after changing font or colors
                        EndSwitch
                EndSwitch
            EndIf
        Case $hListView
            ;ConsoleWrite('Listview' & @CRLF)
            Switch $iCode
                Case $NM_CUSTOMDRAW
                    Local $tNMCustomDraw = DllStructCreate($tagNMLVCUSTOMDRAW, $lParam)
                    Local $iDrawStage, $iItem, $iSubitem, $hDC, $tRECT
                    $iDrawStage = DllStructGetData($tNMCustomDraw, 'dwDrawStage')
                    Switch $iDrawStage
                        Case $CDDS_ITEMPREPAINT
                            Return $CDRF_NOTIFYSUBITEMDRAW
                        Case BitOR($CDDS_ITEMPREPAINT, $CDDS_SUBITEM)
                            ; Item/SubItem das aktuell gezeichnet werden soll ermitteln
                            $iItem = DllStructGetData($tNMCustomDraw, 'dwItemSpec')
                            $iSubitem = DllStructGetData($tNMCustomDraw, 'iSubItem')
                            Switch $iItem ; Zeilenwahl
                                Case -1 ;kommt nicht vor ist aber notwendig, damit case else verwendet werden kann
                                Case Else
                                    $iIndex = _ArraySearch($aRowBK, $iItem, 0, 0, 0, 0, 1, 0)
                                    If $iIndex = -1 Then ;nicht im Array für spezielle Zeilen also weiß
                                        DllStructSetData($tNMCustomDraw, 'clrTextBk', RGB2BGR(0xFFFFFF))
                                    Else
                                        DllStructSetData($tNMCustomDraw, 'clrTextBk', RGB2BGR($aRowBK[$iIndex][1]))
                                        ;ConsoleWrite('Item '&$iItem&' BK: '&$aRowBK[$iIndex][1] & @CRLF)
                                    EndIf
                                    $iIndex = _ArraySearch($aRowText, $iItem, 0, 0, 0, 0, 1, 0)
                                    If $iIndex = -1 Then ;nicht im Array für spezielle Zeilen also schwarz
                                        DllStructSetData($tNMCustomDraw, 'clrText', RGB2BGR(0x000000))
                                    Else
                                        DllStructSetData($tNMCustomDraw, 'clrText', RGB2BGR($aRowText[$iIndex][1]))
                                        ;ConsoleWrite('Item '&$iItem&' Text: '&$aRowText[$iIndex][1] & @CRLF)
                                    EndIf
                            EndSwitch
                            Switch $iSubitem
                                Case -1 ;kommt nicht vor ist aber notwendig, damit case else verwendet werden kann
                                Case Else
                                    $iIndex = _ArraySearch($aColBK, $iSubitem, 0, 0, 0, 0, 1, 0)
                                    If $iIndex <> -1 Then
                                        DllStructSetData($tNMCustomDraw, 'clrTextBk', RGB2BGR($aColBK[$iIndex][1]))
                                        ;ConsoleWrite('SubItem '&$iSubitem&' BK: '&$aColBK[$iIndex][1] & @CRLF)
                                    EndIf
                                    $iIndex = _ArraySearch($aColText, $iSubitem, 0, 0, 0, 0, 1, 0)
                                    If $iIndex <> -1 Then
                                        DllStructSetData($tNMCustomDraw, 'clrText', RGB2BGR($aColText[$iIndex][1]))
                                        ;ConsoleWrite('SubItem '&$iSubitem&' Text: '&$aColText[$iIndex][1] & @CRLF)
                                    EndIf
                            EndSwitch
                            Return $CDRF_NEWFONT
                    EndSwitch
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

Func RGB2BGR($iColor)
    Local $sH = Hex($iColor, 6)
    Return '0x' & StringRight($sH, 2) & StringMid($sH, 3, 2) & StringLeft($sH, 2)
EndFunc   ;==>RGB2BGR

The au3 and the ini is specialited for calendars, but with your own wm_notify you can change to your needs.

LV_CustomRedraw.au3

lv_format.ini

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  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...