Jump to content

Using GUICtrlSetFont() with a Listview Control


Recommended Posts

I have a Listview Control and I would like to change the font color and weight for certain rows. I have successfully chenged the font color using the GUICtrlSetColor() statement below. However, I am not able to change the font weight with the GUICtrlSetFont() statement below: Is GUICtrlSetFont() not supported in a listview?

GUICtrlSetColor($LV_AR[$i], $COLOR_MAROON) ;Maroon

GUICtrlSetFont($LV_AR[$i], 8.5, 800) ;Bold

HeidiRFind free applications, code examples and more on my site at:http://heidisdownloads.com/
Link to comment
Share on other sites

I think it's a limitation in the way Microsoft implemented ListView.

You can't qualify the fields by row via ListView, only column.

If I recall, you also have to recreate the listview for the changes to take effect?

The event handler butts-in in on ListView via the API and gives you a lots more flexibility.

Anyway, I said the same thing as you when I saw that code: "Holy crap!"

I decided I wasn't going to try and research how it worked, and from there it was cake...

I just stuck the one-liner to define the event handler at the top of my program:

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

Then cut-and-pasted the main function to the bottom of my program:

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg, $iwParam
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndListView, $tInfo
;~  Local $tBuffer
    $hWndListView = $hListView
    If Not IsHWnd($hListView) Then $hWndListView = GUICtrlGetHandle($hListView)

    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)

    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
    
    Switch $hWndFrom
        Case $hWndListView
            Switch $iCode
                Case $NM_CLICK, $NM_DBLCLK;         ******** Monitor mouse clicks and double-clicks ********
                    $tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam)
                    $x = DllStructGetData($tInfo, "Index")
; My click/double-click code starts here ----------------
;                   MsgBox(1,"","-->Index: " & $x & @LF & _
;                           "-->SubItem: " & DllStructGetData($tInfo, "SubItem"))
                    Switch  _GUICtrlListView_GetItemImage($hListView, $x)
                        Case 0; blank
                        Case 1; unchecked
                            _GUICtrlListView_SetItemImage($hListView, $x, 2); checked
                        Case 2; checked
                            _GUICtrlListView_SetItemImage($hListView, $x, 1); unchecked
                    EndSwitch
                    _GUICtrlListView_SetItemSelected($hListView, $x, False, False)   
;My click/double-click code ends here ----------------
                
                Case $NM_CUSTOMDRAW;            ******** Monitor drawing of ListView ********
                    Local $tCustDraw = DllStructCreate($tagNMLVCUSTOMDRAW, $ilParam)
                    Local $iDrawStage = DllStructGetData($tCustDraw, "dwDrawStage")
                    
                    If $iDrawStage = $CDDS_PREPAINT Then Return $CDRF_NOTIFYITEMDRAW
                    If $iDrawStage = $CDDS_ITEMPREPAINT Then Return $CDRF_NOTIFYSUBITEMDRAW
                    
                    Local $iSubItem = DllStructGetData($tCustDraw, "iSubItem")
                    Local $iItem = DllStructGetData($tCustDraw, "dwItemSpec")
                    Local $iColor, $hDC
;My customdraw code starts here ----------------
                    If $MWTRN_DATE[$iItem + 1] = $Date_Today Then
                        $hDC = DllStructGetData($tCustDraw, "hdc")
                        If $iSubItem = 1 Then
                            $iColor = 0xDD0000; blue
                        EndIf
                        DllStructSetData($tCustDraw, "clrText", $iColor)
                    EndIf
                    If $MWTRN_TYPE[$iItem + 1] = "A" Then
                        $hDC = DllStructGetData($tCustDraw, "hdc")
                        If $iSubItem = 2 Then
                            $iColor = 0x777777; gray
                        EndIf
                        DllStructSetData($tCustDraw, "clrText", $iColor)
                    EndIf
;My customdraw code ends here ----------------
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc

There are a long list of events that can be trapped, I chose to use the click/double-click to toggle 2 of 4 different images I display at the beginning of each row (indicating the status of each transaction), and I use customdraw to vary background color between the default, gray, and blue. The couple lines of code in that other example would work for tweaking text settings.

Edited by Spiff59
Link to comment
Share on other sites

I think it's a limitation in the way Microsoft implemented ListView.

You can't qualify the fields by row via ListView, only column.

If I recall, you also have to recreate the listview for the changes to take effect?

The event handler butts-in in on ListView via the API and gives you a lots more flexibility.

Anyway, I said the same thing as you when I saw that code: "Holy crap!"

I decided I wasn't going to try and research how it worked, and from there it was cake...

I just stuck the one-liner to define the event handler at the top of my program:

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

Then cut-and-pasted the main function to the bottom of my program:

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg, $iwParam
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndListView, $tInfo
;~  Local $tBuffer
    $hWndListView = $hListView
    If Not IsHWnd($hListView) Then $hWndListView = GUICtrlGetHandle($hListView)

    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)

    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
    
    Switch $hWndFrom
        Case $hWndListView
            Switch $iCode
                Case $NM_CLICK, $NM_DBLCLK;         ******** Monitor mouse clicks and double-clicks ********
                    $tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam)
                    $x = DllStructGetData($tInfo, "Index")
; My click/double-click code starts here ----------------
;                   MsgBox(1,"","-->Index: " & $x & @LF & _
;                           "-->SubItem: " & DllStructGetData($tInfo, "SubItem"))
                    Switch  _GUICtrlListView_GetItemImage($hListView, $x)
                        Case 0; blank
                        Case 1; unchecked
                            _GUICtrlListView_SetItemImage($hListView, $x, 2); checked
                        Case 2; checked
                            _GUICtrlListView_SetItemImage($hListView, $x, 1); unchecked
                    EndSwitch
                    _GUICtrlListView_SetItemSelected($hListView, $x, False, False)   
;My click/double-click code ends here ----------------
                
                Case $NM_CUSTOMDRAW;            ******** Monitor drawing of ListView ********
                    Local $tCustDraw = DllStructCreate($tagNMLVCUSTOMDRAW, $ilParam)
                    Local $iDrawStage = DllStructGetData($tCustDraw, "dwDrawStage")
                    
                    If $iDrawStage = $CDDS_PREPAINT Then Return $CDRF_NOTIFYITEMDRAW
                    If $iDrawStage = $CDDS_ITEMPREPAINT Then Return $CDRF_NOTIFYSUBITEMDRAW
                    
                    Local $iSubItem = DllStructGetData($tCustDraw, "iSubItem")
                    Local $iItem = DllStructGetData($tCustDraw, "dwItemSpec")
                    Local $iColor, $hDC
;My customdraw code starts here ----------------
                    If $MWTRN_DATE[$iItem + 1] = $Date_Today Then
                        $hDC = DllStructGetData($tCustDraw, "hdc")
                        If $iSubItem = 1 Then
                            $iColor = 0xDD0000; blue
                        EndIf
                        DllStructSetData($tCustDraw, "clrText", $iColor)
                    EndIf
                    If $MWTRN_TYPE[$iItem + 1] = "A" Then
                        $hDC = DllStructGetData($tCustDraw, "hdc")
                        If $iSubItem = 2 Then
                            $iColor = 0x777777; gray
                        EndIf
                        DllStructSetData($tCustDraw, "clrText", $iColor)
                    EndIf
;My customdraw code ends here ----------------
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc

There are a long list of events that can be trapped, I chose to use the click/double-click to toggle 2 of 4 different images I display at the beginning of each row (indicating the status of each transaction), and I use customdraw to vary background color between the default, gray, and blue. The couple lines of code in that other example would work for tweaking text settings.

Thanks for the info and the code. I'll give it a try.
HeidiRFind free applications, code examples and more on my site at:http://heidisdownloads.com/
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...