Jump to content

ListView Sort and row Background Color


 Share

Recommended Posts

Hi

i need some help with Listview Sort and Background Color.

Example:

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


$Gui = GUICreate("Test", 300, 200)
$hListView = GUICtrlCreateListView("Client | Status", 5, 0, 290, 250)
GUICtrlSendMsg($hListView, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_GRIDLINES, $LVS_EX_GRIDLINES)

For $a = 1 To 10
        if $a = 4 then
            GUICtrlCreateListViewItem("PC" & $a & "| Offline", $hListView)
            GUICtrlSetBkColor(-1, 0xFF0000)
        else
            GUICtrlCreateListViewItem("PC" & $a & "| Online", $hListView)
        EndIf
Next

GUISetState()

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
Global $B_DESCENDING[_GUICtrlListView_GetColumnCount($hListView)]

While 1
    $msg = GUIGetMsg()
    Switch $msg
    Case $GUI_EVENT_CLOSE
        ExitLoop
    EndSwitch
    Sleep( 10 )
WEnd
Exit

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndListView
    $hWndListView = $hListView
    If Not IsHWnd($hListView) Then $hWndListView = GUICtrlGetHandle($hListView)

    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iCode = DllStructGetData($tNMHDR, "Code")

    Switch $hWndFrom
    Case $hWndListView
        Switch $iCode
        Case $LVN_COLUMNCLICK
            Local $tInfo = DllStructCreate($tagNMLISTVIEW, $ilParam)
            _GUICtrlListView_SimpleSort($hWndListView, $B_DESCENDING, DllStructGetData($tInfo, "SubItem"))
        EndSwitch
    EndSwitch
EndFunc

I want to highlight all Offline clients. But how can I highlight the offline Clients if the order change? I read something about $NM_CUSTOMDRAW and read the post from Siao but i dont understand this. Need some small Example/help please.

Link to comment
Share on other sites

Here ya go :blink:...

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


$Gui = GUICreate("Test", 300, 200)
$hListView = GUICtrlCreateListView("Client | Status", 5, 0, 290, 250)
GUICtrlSendMsg($hListView, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_GRIDLINES, $LVS_EX_GRIDLINES)

For $a = 1 To 10
    If $a = 4 Then
        GUICtrlCreateListViewItem("PC" & $a & "| Offline", $hListView)
    Else
        GUICtrlCreateListViewItem("PC" & $a & "| Online", $hListView)
    EndIf
Next

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

Global $B_DESCENDING[_GUICtrlListView_GetColumnCount($hListView)]

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            ExitLoop
    EndSwitch
    Sleep(10)
WEnd
Exit

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndListView
    $hWndListView = $hListView
    If Not IsHWnd($hListView) Then $hWndListView = GUICtrlGetHandle($hListView)

    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iCode = DllStructGetData($tNMHDR, "Code")

    Switch $hWndFrom
        Case $hWndListView
            Switch $iCode
                Case $LVN_COLUMNCLICK
                    Local $tInfo = DllStructCreate($tagNMLISTVIEW, $ilParam)
                    _GUICtrlListView_SimpleSort($hWndListView, $B_DESCENDING, DllStructGetData($tInfo, "SubItem"))
                Case $NM_CUSTOMDRAW
                    Local $tCustDraw = DllStructCreate($tagNMLVCUSTOMDRAW, $ilParam)
                    Local $iDrawStage = DllStructGetData($tCustDraw, "dwDrawStage")
                    Switch $iDrawStage
                        Case $CDDS_PREPAINT
                            Return $CDRF_NOTIFYITEMDRAW ;request custom drawing of items
                        Case $CDDS_ITEMPREPAINT
                            Return $CDRF_NOTIFYSUBITEMDRAW ;request drawing each cell separately
                    EndSwitch
                    Local $iItem = DllStructGetData($tCustDraw, "dwItemSpec")
                    Local $iSubItem = DllStructGetData($tCustDraw, "iSubItem")
                    Local $hDC = DllStructGetData($tCustDraw, 'hdc')
                    If $iSubItem = 1 Then
                        If _GUICtrlListView_GetItemText($hWndListView, $iItem, 1) = " Offline" Then
                            ;ConsoleWrite(_GUICtrlListView_GetItemText($hWndListView, $iItem, 1) & @CRLF)
                            DllStructSetData($tCustDraw, "clrTextBk", 0x0000FF) ; in BGR
                        EndIf
                    EndIf

                    Return $CDRF_NEWFONT


            EndSwitch
    EndSwitch
Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

Edit: Added "Return $GUI_RUNDEFMSG" to the Notify functions.

Edited by KaFu
Link to comment
Share on other sites

Good morning

I change the code to highlight the complete row. But two more question I have.

1. Is the code modification the right way ? It works for me but I am a little bit confusion about the Return Values $CDRF_NEWFONT and $CDRF_NOTIFYSUBITEMDRAW

2. Is there a simple way to highlight only the "offline" cell ?

thx for help

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


$Gui = GUICreate("Test", 300, 200)
$hListView = GUICtrlCreateListView("Client | Status| test", 5, 0, 290, 250)
GUICtrlSendMsg($hListView, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_GRIDLINES, $LVS_EX_GRIDLINES)

For $a = 1 To 10
    If $a = 4 Then
        GUICtrlCreateListViewItem("PC" & $a & "|Offline|" & $a, $hListView)
    Else
        GUICtrlCreateListViewItem("PC" & $a & "|Online|" & $a, $hListView)
    EndIf
Next

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

Global $B_DESCENDING[_GUICtrlListView_GetColumnCount($hListView)]

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            ExitLoop
    EndSwitch
    Sleep(10)
WEnd
Exit

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndListView
    $hWndListView = $hListView
    If Not IsHWnd($hListView) Then $hWndListView = GUICtrlGetHandle($hListView)

    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iCode = DllStructGetData($tNMHDR, "Code")

    Switch $hWndFrom
        Case $hWndListView
            Switch $iCode
                Case $LVN_COLUMNCLICK
                    Local $tInfo = DllStructCreate($tagNMLISTVIEW, $ilParam)
                    _GUICtrlListView_SimpleSort($hWndListView, $B_DESCENDING, DllStructGetData($tInfo, "SubItem"))
                Case $NM_CUSTOMDRAW
                    Local $tCustDraw = DllStructCreate($tagNMLVCUSTOMDRAW, $ilParam)
                    Local $iDrawStage = DllStructGetData($tCustDraw, "dwDrawStage")

                    Switch $iDrawStage
                        Case $CDDS_PREPAINT
                            Return $CDRF_NOTIFYITEMDRAW ;request custom drawing of items
                        Case  $CDDS_ITEMPREPAINT
                            Local $iItem = DllStructGetData($tCustDraw, "dwItemSpec")
                            If _GUICtrlListView_GetItemText($hWndListView, $iItem, 1) = "Offline" Then
                            DllStructSetData($tCustDraw, "clrTextBk", 0x0000FF)
                            EndIf
                            Return $CDRF_NOTIFYSUBITEMDRAW ;request drawing each cell separately
                    EndSwitch
#cs
                    Local $iItem = DllStructGetData($tCustDraw, "dwItemSpec")
                    Local $iSubItem = DllStructGetData($tCustDraw, "iSubItem")
                    ;Local $hDC = DllStructGetData($tCustDraw, 'hdc')
                    If $iSubItem = 1 Then
                        If _GUICtrlListView_GetItemText($hWndListView, $iItem, 1) = "Offline" Then
                            ConsoleWrite(_GUICtrlListView_GetItemText($hWndListView, $iItem, 1) & @CRLF)
                            DllStructSetData($tCustDraw, "clrTextBk", 0x0000FF) ; in BGR
                        EndIf
                    EndIf
                    Return $CDRF_NEWFONT
#ce
            EndSwitch
    EndSwitch
Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY
Link to comment
Share on other sites

It works for me but I am a little bit confusion about the Return Values $CDRF_NEWFONT and $CDRF_NOTIFYSUBITEMDRAW

Don't ask me about the why's, thats just the way its documented ;)...

2. Is there a simple way to highlight only the "offline" cell ?

According to this MSDN article, I would have expected a $CDDS_SUBITEM drawing stage... but case else also seems to work fine :blink:...

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


$Gui = GUICreate("Test", 300, 200)
$hListView = GUICtrlCreateListView("Client | Status| test", 5, 0, 290, 250)
GUICtrlSendMsg($hListView, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_GRIDLINES, $LVS_EX_GRIDLINES)

For $a = 1 To 10
    If $a = 4 Then
        GUICtrlCreateListViewItem("PC" & $a & "|Offline|" & $a, $hListView)
    Else
        GUICtrlCreateListViewItem("PC" & $a & "|Online|" & $a, $hListView)
    EndIf
Next

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

Global $B_DESCENDING[_GUICtrlListView_GetColumnCount($hListView)]

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            ExitLoop
    EndSwitch
    Sleep(10)
WEnd
Exit

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndListView
    $hWndListView = $hListView
    If Not IsHWnd($hListView) Then $hWndListView = GUICtrlGetHandle($hListView)

    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iCode = DllStructGetData($tNMHDR, "Code")

    Switch $hWndFrom
        Case $hWndListView
            Switch $iCode
                Case $LVN_COLUMNCLICK
                    Local $tInfo = DllStructCreate($tagNMLISTVIEW, $ilParam)
                    _GUICtrlListView_SimpleSort($hWndListView, $B_DESCENDING, DllStructGetData($tInfo, "SubItem"))

                Case $NM_CUSTOMDRAW
                    ; http://msdn.microsoft.com/en-us/library/bb761817(VS.85).aspx
                    Local $tCustDraw = DllStructCreate($tagNMLVCUSTOMDRAW, $ilParam)
                    Local $iDrawStage = DllStructGetData($tCustDraw, "dwDrawStage")

                    Switch $iDrawStage
                        Case $CDDS_PREPAINT
                            Return $CDRF_NOTIFYITEMDRAW ;request custom drawing of items

                        Case $CDDS_ITEMPREPAINT
                            Return $CDRF_NOTIFYSUBITEMDRAW

                        Case Else
                            ;case $CDDS_SUBITEM
                            Local $iItem = DllStructGetData($tCustDraw, "dwItemSpec")

                            Switch DllStructGetData($tCustDraw, "iSubItem")
                                Case 1
                                    If _GUICtrlListView_GetItemText($hWndListView, $iItem, 1) = "Offline" Then
                                        DllStructSetData($tCustDraw, "clrTextBk", 0x0000FF)
                                    Else
                                        DllStructSetData($tCustDraw, "clrTextBk", 0xffffff)
                                    EndIf

                                Case Else
                                    DllStructSetData($tCustDraw, "clrTextBk", 0xffffff)

                            EndSwitch
                            Return $CDRF_NEWFONT

                    EndSwitch
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY
Edited by KaFu
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...