Jump to content
Sign in to follow this  
BugFix

ListView - for every SubItem set color, backcol and font

Recommended Posts

weirddave

I'm not sure how post 26 helps me, I'm not using those functions.

Could the problem be an incompatibility with using the standard function:

GUICtrlCreateListViewItem()

which is what I used in the scipt.

The custom listviews link looks interesting, If the standard functions won't work I might go that route.

At this point, it may be worth mentioning what I'm trying to do, in case there's a better way.

I'm creating a log window with 3 columns for time, event and event state. I want to change the background colour of the event state to match Fault and OK values (red and green obviously :) ). The event state isn't always an OK or Fault and will stay the default white background.

Cheers

Share this post


Link to post
Share on other sites
LarsJ
weirddave

The full code is quite large and requires a number of support files (it's pulling data via a USB to serial port device using FTDI drivers), which is why I posted a small script (post 39) which shows the problem.

I am currently adjusting my code to use WM_NOTIFY instead of the LV_Format_include.1.4.au3, I suspect this will work fine.

However, it would be good to know if LV_Format_include.1.4.au3 isn't compatible with GUICtrlCreateListViewItem()

Cheers

Share this post


Link to post
Share on other sites
LarsJ

You need WM_NOTIFY for LV_Format_include.1.4.au3. See docu in top of file (one page down).

LV_Format_include.1.4.au3 is compatible with GUICtrlCreateListViewItem().

Share this post


Link to post
Share on other sites
weirddave

I'm nearly at the point of admitting defeat and not bothering to change the background colour, All of the code examples I've seen for the various methods have a static listview, mine (and the small script I posted) add a new item to the listview.

Here is a working piece of code which adds a new line to the listview every time the button is pressed, all I want to do is change the background colour of the last column to red, green or white.

#include <GUIConstantsEx.au3>

$MyGUI = GUICreate("listview items", 320, 200, 0,-1, -1)
$idListview = GUICtrlCreateListView("A|B|C", 10, 10, 250, 155)
$idButton = GUICtrlCreateButton("Add", 125, 175, 70, 20)
GUISetState(@SW_SHOW)

while 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $idButton
            GUICtrlCreateListViewItem("a|b|c", $idListview)
    EndSwitch
    sleep(10)
wend

I'm obviously missing something, this shouldn't be so hard :(

Share this post


Link to post
Share on other sites
LarsJ

Share this post


Link to post
Share on other sites
weirddave

I figured it should be easy, I just can't seem to put all the pieces of the puzzle together. I appreciate the help.

Share this post


Link to post
Share on other sites
weirddave

I have managed to get to a point where I have some working code, but there are some things I don't quite understand. Here is my code:
 

#include <GuiListView.au3>
#include <EditConstants.au3>
#include 'LV_Format_include.1.4.au3'

$MyGUI = GUICreate("listview items", 320, 200, 0,-1, -1)
$idListview = GUICtrlCreateListView("A|B|C", 10, 10, 250, 155)
$idButton = GUICtrlCreateButton("Add", 125, 175, 70, 20)
$hListview = GUICtrlGetHandle($idListview)
_GUICtrlListView_Formatting_Startup($MyGUI, $hListview)
GUISetState(@SW_SHOW, $MyGUI)

while 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $idButton
            $LogItemCount = _GUICtrlListView_GetItemCount($idListview)
            GUISetState(@SW_HIDE, $MyGUI)
            ;GUICtrlCreateListViewItem("a|b|"&$LogItemCount, $idListview)
            _GUICtrlListView_AddOrIns_Item($hListview,'a|b|'&$LogItemCount)
            _GUICtrlListView_FormattingCell($hListview, $LogItemCount, 2, 0xff0000, -1, -1, -1, -1)
            GUISetState(@SW_SHOW, $MyGUI)       
    EndSwitch
    sleep(10)
wend

I can't get the standard GUICtrlCreateListViewItem() function to work with it, I had to use _GUICtrlListView_AddOrIns_Item() instead.

The main part to me solving the problem was to @SW_HIDE the gui before adding to the list and formatting it. Is this really required? It does cause some flickering (obviously).

Share this post


Link to post
Share on other sites
mpower

I think this is an easier way to create colour listviews, hope it helps someone!

#include <GUIConstants.au3>
#include <WindowsConstants.au3>
#include <FontConstants.au3>
#include <GuiListView.au3>
#include <WinAPI.au3>

Opt("GUIResizeMode", 802)

$hGui = GUICreate("Custom Colour Listview Draw Example", 690, 300, -1, -1, BitOr($WS_MINIMIZEBOX, $WS_MAXIMIZEBOX, $WS_SIZEBOX))
_WinAPI_SetClassLongEx($hGui, -26, BitAND(_WinAPI_GetClassLongEx($hGui, -26), BitNOT(1), BitNOT(2)))

$aPos = WinGetClientSize($hGui)
$idLV = GUICtrlCreateListView( "", 0, 0, $aPos[0], $aPos[1], -1, 0x200)
_GUICtrlListView_SetExtendedListViewStyle($idLV, BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_DOUBLEBUFFER, $LVS_EX_GRIDLINES, $LVS_EX_SUBITEMIMAGES, $LVS_EX_FLATSB))
$hLV = ControlGetHandle($hGui, "", $idLV)

_GUICtrlListView_AddColumn( $hLV, "Blue text (bold)", 135 )
_GUICtrlListView_AddColumn( $hLV, "Red text (normal)", 135 )
_GUICtrlListView_AddColumn( $hLV, "Black text (bold)", 135 )
_GUICtrlListView_AddColumn( $hLV, "White text on green bg", 135 )
_GUICtrlListView_AddColumn( $hLV, "Black text (normal)", 135 )

$hDC = _WinAPI_GetDC($hLV)
$hFont = _SendMessage($hLV, $WM_GETFONT)
$hObject = _WinAPI_SelectObject($hDC, $hFont)
$lvLOGFONT = DllStructCreate($tagLOGFONT)
$aRet = DllCall('gdi32.dll', 'int', 'GetObjectW', 'ptr', $hFont, 'int', DllStructGetSize($lvLOGFONT), 'ptr', DllStructGetPtr($lvLOGFONT))
_WinAPI_SelectObject($hDC, $hObject)
_WinAPI_ReleaseDC($hLV, $hDC)
$hLVfont = _WinAPI_CreateFontIndirect($lvLOGFONT)
$iWeight = BitOR( DllStructGetData($lvLOGFONT, "Weight"), $FW_BOLD)
DllStructSetData( $lvLOGFONT, "Weight", $iWeight)
$hLVfontBold = _WinAPI_CreateFontIndirect($lvLOGFONT)

GUIRegisterMsg( $WM_NOTIFY, "WM_NOTIFY" )
GUIRegisterMsg( $WM_SIZE, "WM_SIZE" )

AddLVItemsExample()

GUISetState(@SW_SHOW, $hGui)

While 1
    Local $aMsg = GUIGetMsg(1)
    Switch $aMsg[1]
        Case $hGui
            Switch $aMsg[0]
                Case $GUI_EVENT_CLOSE
                    ExitLoop
            EndSwitch
    EndSwitch
WEnd

Func AddLVItemsExample()
    For $i = 0 to 9
        $iCount = _GUICtrlListView_GetItemCount($idLV)
        _GUICtrlListView_AddItem($idLV, 'item ' & $i)
        _GUICtrlListView_AddSubItem($idLV, $iCount, 'subitem ' & $i, 1)
        _GUICtrlListView_AddSubItem($idLV, $iCount, 'subitem ' & $i, 2)
        _GUICtrlListView_AddSubItem($idLV, $iCount, 'subitem ' & $i, 3)
        _GUICtrlListView_AddSubItem($idLV, $iCount, 'subitem ' & $i, 4)
    Next
EndFunc

Func WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam
    Local $tNMHDR, $hWndFrom, $iCode
    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iCode = DllStructGetData($tNMHDR, "Code")

    Switch $hWndFrom
        Case $hLV
            Switch $iCode
                Case $NM_CUSTOMDRAW
                    Local $tNMLVCUSTOMDRAW = DllStructCreate($tagNMLVCUSTOMDRAW, $lParam)
                    Local $dwDrawStage = DllStructGetData($tNMLVCUSTOMDRAW, "dwDrawStage")
                    Switch $dwDrawStage
                        Case $CDDS_PREPAINT
                            Return $CDRF_NOTIFYITEMDRAW
                        Case $CDDS_ITEMPREPAINT
                            Return $CDRF_NOTIFYSUBITEMDRAW
                        Case BitOR( $CDDS_ITEMPREPAINT, $CDDS_SUBITEM )
                            Local $iSubItem = DllStructGetData($tNMLVCUSTOMDRAW, "iSubItem")
                            Local $dwItemSpec = DllStructGetData($tNMLVCUSTOMDRAW, "dwItemSpec")
                            Local $hDC = DllStructGetData($tNMLVCUSTOMDRAW, "HDC")
                            Switch $iSubItem
                                Case 0
                                    _WinAPI_SelectObject($hDC, $hLVfontBold)
                                    DllStructSetData( $tNMLVCUSTOMDRAW, "ClrText", ColorConvert(0x0404B4))
                                    DllStructSetData( $tNMLVCUSTOMDRAW, "ClrTextBk", ColorConvert(0xFFFFFF))
                                Case 1
                                    _WinAPI_SelectObject($hDC, $hLVfont)
                                    DllStructSetData( $tNMLVCUSTOMDRAW, "ClrText", ColorConvert(0xB40404))
                                    DllStructSetData( $tNMLVCUSTOMDRAW, "ClrTextBk", ColorConvert(0xFFFFFF))
                                Case 2
                                    _WinAPI_SelectObject($hDC, $hLVfontBold)
                                    DllStructSetData( $tNMLVCUSTOMDRAW, "ClrText", ColorConvert(0x000000))
                                    DllStructSetData( $tNMLVCUSTOMDRAW, "ClrTextBk", ColorConvert(0xFFFFFF))
                                Case 3
                                    _WinAPI_SelectObject($hDC, $hLVfont)
                                    DllStructSetData( $tNMLVCUSTOMDRAW, "ClrText", ColorConvert(0xFFFFFF))
                                    DllStructSetData( $tNMLVCUSTOMDRAW, "ClrTextBk", ColorConvert(0x088A08))
                                Case Else
                                    _WinAPI_SelectObject($hDC, $hLVfont)
                                    DllStructSetData( $tNMLVCUSTOMDRAW, "ClrText", ColorConvert(0x000000))
                                    DllStructSetData( $tNMLVCUSTOMDRAW, "ClrTextBk", ColorConvert(0xFFFFFF))
                            EndSwitch
                            Return $CDRF_NEWFONT
                    EndSwitch
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc

Func ColorConvert($iColor)
    Return BitOR(BitAND($iColor, 0x00FF00), BitShift(BitAND($iColor, 0x0000FF), -16), BitShift(BitAND($iColor, 0xFF0000), 16))
EndFunc

Func WM_SIZE($hwnd, $uMsg, $wParam, $lParam)
    If $hwnd <> $hGui Then Return 0
    _WinAPI_SetWindowPos(GUICtrlGetHandle($idLV), 0, 0, 0, _WinAPI_LoWord($lParam), _WinAPI_HiWord($lParam), BitOR($SWP_NOMOVE, $SWP_NOZORDER))
    Return 1
EndFunc
Edited by mpower

Share this post


Link to post
Share on other sites
LarsJ

weirddave, You can find code for your example in post 45 here. The code sets the background color in third column to red/green for last/second last row.

Share this post


Link to post
Share on other sites
weirddave

Thankyou both, this is getting closer to my goal, I will post comments in the other thread :)

Share this post


Link to post
Share on other sites
Bolluhhhhh

Hi all,

When I use _GUICtrlListView_FormattingCell($hLV, 1, 1, 0xff0000) to change the background color of a subitem then all subitems to the right get a smaller fontsize. Does anyone have an idea why this is happening? Color change works fine.

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  

×