Jump to content

I'd like help with LarsJ's GuiListViewEx, please


Mbee
 Share

Recommended Posts

I need to use custom fonts and colors in a list view, and as far as I am aware, the only way of doing that is with LarsJ's GuiListViewEx UDF.
But I can't get it to work. I'm sure I'm doing something wrong, yet after about a week trying, I can't figure out what that is.  Here's the smallest amount of code that's both runnable and demonstrates the issue I'm having...

#Include <APIConstants.au3>
#include <Array.au3>
#include <Constants.au3>
#include <Misc.au3>
#include <WinAPI.au3>
#Include <WinAPIEx.au3>
#include <WinAPIHObj.au3>
#include <WinAPIProc.au3>

#include <ColorConstants.au3>
#include <StaticConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Include <GuiButton.au3>
#include <UpDownConstants.au3>
#include <GuiListView.au3>

#include <FontConstants.au3>
#include <WinAPIGdi.au3>
#include <WinAPIGdiDC.au3>
#include <WinAPIHObj.au3>
#include <WinAPISysWin.au3>


#include "GuiListViewEx.au3"            ; By LarsJ ; https://www.autoitscript.com/forum/topic/181346-colors-and-fonts-in-custom-drawn-listviews-udf-version/
#include "GuiImageListEx.au3"           ; By LarsJ
#include "ListViewColorsFonts.au3"      ; "  "
#include "NamedColors.au3"              ; "  "


Opt( "GUICloseOnESC", 1 )
Opt( "MustDeclareVars", 1 )


Global Const $Gc_Title = "MyVidTool v0.4"

Local $L_Stat, $L_Error
Global $G_LViewHdl

Global $G_VidToolGUI = GUICreate("VidToolGUI",464,266,-1,-1,BitOr($WS_SIZEBOX,$WS_HSCROLL,$WS_VSCROLL,$WS_VISIBLE,$DS_MODALFRAME),BitOr($WS_EX_ACCEPTFILES,$WS_EX_APPWINDOW))
GUISetOnEvent($GUI_EVENT_CLOSE, "Gi_ExitClick", $G_VidToolGUI)
Global $GId_LimitValUpDown = GUICtrlCreateInput("5",10,12,40,20, -1,$WS_EX_STATICEDGE)
GUICtrlSetColor(-1,"0x008080")
Global $GId_LimitValUpDown_Updown = GUICtrlCreateUpdown(-1,BitOr($UDS_ALIGNRIGHT,$ES_NUMBER))
Global $GId_ExitBtn = GUICtrlCreateButton("Exit",85,9,60,28,-1,-1)

Global $G_LViewID = GUICtrlCreateListView( "Name", 9, 50, 500, 500, BitOr($LVS_SHOWSELALWAYS, $LVS_SINGLESEL, $LVS_SHOWSELALWAYS), _
                                            BitOr($LVS_EX_GRIDLINES, $LVS_EX_DOUBLEBUFFER, $LVS_EX_TRACKSELECT) )
If $G_LViewID = 0 Then
    $L_Error = @error
    MsgBox($MB_OK, $Gc_Title, "GUICtrlCreateListView() - @error= " & $L_Error)
    Exit
EndIf

$G_LViewHdl = GUICtrlGetHandle( $G_LViewID )

Local $L_MyLVFontHdl = _WinAPI_CreateFont( 16, 11, 0, 0, $FW_NORMAL, False, False, False, $DEFAULT_CHARSET, $OUT_DEVICE_PRECIS, _
                                            $CLIP_DEFAULT_PRECIS, $PROOF_QUALITY, $FF_DONTCARE, "Anonymous Pro" )

GUICtrlSetData( $G_LViewID, "Example Row" )


$L_Stat = ListViewColorsFonts_Init( $G_LViewHdl, 64, 1, False )
If $L_Stat = -1 Then
    $L_Error = @error
    MsgBox($MB_OK, $Gc_Title, "ListViewColorsFonts_Init() - @error= " & $L_Error)
    Exit
EndIf


$L_Stat = ListViewColorsFonts_SetItemColorsFonts( $G_LViewID, 0, 0, $L_MyLVFontHdl, $iFontStyleNormal, 0, $__g_iDarkGreen )
If $L_Stat = -1 Then
    $L_Error = @error
    MsgBox($MB_OK, $Gc_Title, "ListViewColorsFonts_SetDefaultColorsFonts(Green) - @error= " & $L_Error)
    Exit
EndIf

GUISetState( @SW_ENABLE + @SW_SHOW, $G_VidToolGUI )

Exit
While True
    Sleep(500)
WEnd

Func Gi_ExitClick()
    Exit
EndFunc

Func Gi_LimitClick()
EndFunc

What's going wrong is that the call to ListViewColorsFonts_SetItemColorsFonts() returns an error indicating that it can't "find" the list view ID, even though it's recognized correctly in the UDF function call above it, ListViewColorsFonts_Init().

After looking into @LarsJ'sUDF code, here's the line of code that fails:

If $iIndex = $iListViewColorsFontsInfo Then Return SetError(2, 0, -1) ; Listview not found

Yet that test makes no sense to me, since it actually finds the list view just fine, yet $iListViewColorsFontsInfo doesn't seem to relate to finding the list view at all.  I guess what I'm saying is that the error return doesn't seem to report the actual error, so I can't fix my code to correct for that error.  But the UDF in question is dense and complicated as hell, so I've been unable to understand it nearly well enough to troubleshoot my issue.

LarsJ speaks of a future release that will address some issues, but it's long overdue, so I'm guessing it won't be available anytime soon.

If someone who's successfully used custom fonts and colors with that UDF could take a look and either tell me what I'm doing wrong or offer suggestions, I'd very much appreciate it.

Thank you!

Edited by Mbee
Unsolved
Link to comment
Share on other sites

If you look at the return codes of the function, it tells you that this issue is with ListViewColorsFonts_Init second parameter $fColorsFonts, you also are using GuiCtrlSetData (...) which means you have no listview items, you would need to use GUICtrlCreateListViewItem(...) instead to create the items.

Link to comment
Share on other sites

Thank you, @Subz, for your reply! 

I had forgotten that I had removed that step from the code snippet I posted. Lacking that line does indeed explain the @error = 2 return code.

Unfortunately, when I put it back in, I once again get the List View not found error code of @error = 1. Here's the revised code (everything's the same except for the re-introduction of the GUICtrlCreateListViewItem () call...

 

;~ #include "_Dbug.au3"

#Include <APIConstants.au3>
#include <Array.au3>
#include <Constants.au3>
#include <Misc.au3>
#include <WinAPI.au3>
#Include <WinAPIEx.au3>
#include <WinAPIHObj.au3>
#include <WinAPIProc.au3>

#include <ColorConstants.au3>
#include <StaticConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Include <GuiButton.au3>
#include <UpDownConstants.au3>
#include <GuiListView.au3>

#include <FontConstants.au3>
#include <WinAPIGdi.au3>
#include <WinAPIGdiDC.au3>
#include <WinAPIHObj.au3>
#include <WinAPISysWin.au3>


#include "GuiListViewEx.au3"            ; By LarsJ ; https://www.autoitscript.com/forum/topic/181346-colors-and-fonts-in-custom-drawn-listviews-udf-version/
#include "GuiImageListEx.au3"           ; By LarsJ
#include "ListViewColorsFonts.au3"      ; "  "
#include "NamedColors.au3"              ; "  "


Opt( "GUICloseOnESC", 1 )
Opt( "MustDeclareVars", 1 )


Global Const $Gc_Title = "MyVidTool v0.4"




Local $L_Stat, $L_Error
Global $G_LViewHdl

Global $G_VidToolGUI = GUICreate("VidToolGUI",464,266,-1,-1,BitOr($WS_SIZEBOX,$WS_HSCROLL,$WS_VSCROLL,$WS_VISIBLE,$DS_MODALFRAME),BitOr($WS_EX_ACCEPTFILES,$WS_EX_APPWINDOW))
GUISetOnEvent($GUI_EVENT_CLOSE, "Gi_ExitClick", $G_VidToolGUI)
Global $GId_LimitValUpDown = GUICtrlCreateInput("5",10,12,40,20, -1,$WS_EX_STATICEDGE)
GUICtrlSetColor(-1,"0x008080")
Global $GId_LimitValUpDown_Updown = GUICtrlCreateUpdown(-1,BitOr($UDS_ALIGNRIGHT,$ES_NUMBER))
Global $GId_ExitBtn = GUICtrlCreateButton("Exit",85,9,60,28,-1,-1)

Global $G_LViewID = GUICtrlCreateListView( "Name", 9, 50, 500, 500, BitOr($LVS_SHOWSELALWAYS, $LVS_SINGLESEL, $LVS_SHOWSELALWAYS), _
                                            BitOr($LVS_EX_GRIDLINES, $LVS_EX_DOUBLEBUFFER, $LVS_EX_TRACKSELECT) )
If $G_LViewID = 0 Then
    $L_Error = @error
    MsgBox($MB_OK, $Gc_Title, "GUICtrlCreateListView() - @error= " & $L_Error)
    Exit
EndIf

$G_LViewHdl = GUICtrlGetHandle( $G_LViewID )

Local $L_MyLVFontHdl = _WinAPI_CreateFont( 16, 11, 0, 0, $FW_NORMAL, False, False, False, $DEFAULT_CHARSET, $OUT_DEVICE_PRECIS, _
                                            $CLIP_DEFAULT_PRECIS, $PROOF_QUALITY, $FF_DONTCARE, "Anonymous Pro" )

Local $L_LVitemID = GUICtrlCreateListViewItem( "Name", $G_LViewID )

$L_Stat = ListViewColorsFonts_Init( $G_LViewHdl, 64, 1, False )
If $L_Stat = -1 Then
    $L_Error = @error
    MsgBox($MB_OK, $Gc_Title, "ListViewColorsFonts_Init() - @error= " & $L_Error)
    Exit
EndIf


$L_Stat = ListViewColorsFonts_SetItemColorsFonts( $G_LViewID, 0, 0, $L_MyLVFontHdl, $iFontStyleNormal, 0, $__g_iDarkGreen )
If $L_Stat = -1 Then
    $L_Error = @error
    MsgBox($MB_OK, $Gc_Title, "ListViewColorsFonts_SetDefaultColorsFonts(Green) - @error= " & $L_Error)
    Exit
EndIf

GUISetState( @SW_ENABLE + @SW_SHOW, $G_VidToolGUI )

Exit
While True
    Sleep(500)
WEnd

Func Gi_ExitClick()
    Exit
EndFunc

Func Gi_LimitClick()
EndFunc

 

Do you now get the @error = 1 return as well?

I should add that if I use the regular graphical debugger when debugging the actual code (rather than the snippet above), that debugger usually crashes for some unknown reason within the UDF code.  Therefore, I have to use DBug, which I've come to like rather a lot anyway (though it desperately needs an actual help file/documentation, rather than the worthless little help file that comes with it). You may want to keep that in mind if you choose to run it in a debugger...

 

ETA: Just in case it matters, I have asked LarsJ to elaborate more on the meaning of the fourth parameter in the ListViewColorsFonts_Init() call. In the fuller code I'm developing, if I set it to True, I run into problems. But in the snippet above, it doesn't matter; I still get @error = 1 from the ListViewColorsFonts_SetItemColorsFonts() call.

 

Edited by Mbee
Link to comment
Share on other sites

I'm not sure where you're getting @error equals 1 means listview doesn't exist, as I mentioned if you look at ListViewColorsFonts_SetItemColorsFonts return values (see below), this function requires that $fColorsFonts includes 1/2/4?   The $fColorsFonts is set in "$L_Stat = ListViewColorsFonts_Init( $G_LViewHdl, 64, 1, False )", you are using 64 but haven't added 1, 2 or 4 or a combination?  You also haven't added the ListViewColorsFonts(_Redraw(...)  which changes the color afterwards, I recommend you look at Lars examples specifically "...\Examples\0) UDF examples\3) ListViewColorsFonts_SetItemColorsFonts" and work backwards from that.

; Return values .: Failure - Returns -1 and sets @error:
;                            1 - This function requires that $fColorsFonts includes 1/2/4
;                            2 - Listview not found
;                            3 - Invalid listview item
;                            4 - Invalid listview subitem
;                            5 - Invalid font or style
;                            6 - Too many color parameters
;                            7 - Invalid color value

 

Link to comment
Share on other sites

Geez, I need a mental health break!

Here's why I screwed up: I've tried a minimum of 50-60 different values for various parameters in order to get it to work. I just couldn't keep all the different attempts straight in my tiny little mind.

So to cut to the chase, I modified the snippet code to loop through various values for the $fColorsFonts parameter, but without including all the various permutations. But although 7 worked, that was emphatically NOT what I needed or wanted -- I needed 64! Every single value failed except 7.

But after all those failures, in order to avoid looking like a ninny yet again to your eyes, I decided I better try a lot of permutations (combinations of values). And thus I was finally rewarded: 64 + 7 did not fail!

I've yet to try it with the larger, real application I'm developing, but now that I'm past that hurdle I think I can manage on my own.  See what shame can accomplish?

Please accept my strongest thanks for your patience and replies! 🤩

Link to comment
Share on other sites

By the way, when I specified 7+64 in my real code, it went right back to the dreaded @error = 1 again! So I carefully examined the differences between the snippet code and the real deal, and I discovered the difference, and it's seems pretty strange to me...

It turns out that you can cause the ListViewColorsFonts_Init() to fail if you set certain values in the GUICtrlCreateListView() call. The code I posted was this:

Global $G_LViewID = GUICtrlCreateListView( "Name", 9, 50, 500, 500, BitOr($LVS_SHOWSELALWAYS, $LVS_SINGLESEL, $LVS_SHOWSELALWAYS), _
                                            BitOr($LVS_EX_GRIDLINES, $LVS_EX_DOUBLEBUFFER, $LVS_EX_TRACKSELECT) )

But my real code looked like this:

Global $G_LViewID = GUICtrlCreateListView( "Name", $L_PosAra[0], $L_PosAra[1], $L_PosAra[2], $L_PosAra[3], _
                    BitOr($LVS_ALIGNLEFT, $LVS_LIST, $LVS_NOCOLUMNHEADER, $LVS_NOLABELWRAP, $LVS_SHOWSELALWAYS, $LVS_SINGLESEL), _
                    BitOR($LVS_EX_DOUBLEBUFFER, $LVS_EX_GRIDLINES, $LVS_EX_FLATSB, $LVS_EX_TRACKSELECT) )

Which caused the ListViewColorsFonts_Init() function return @error = 1.

But the following worked fine:

Global $G_LViewID = GUICtrlCreateListView( "Name", $L_PosAra[0], $L_PosAra[1], $L_PosAra[2], $L_PosAra[3], _
                    BitOr($LVS_ALIGNLEFT, $LVS_SHOWSELALWAYS, $LVS_SINGLESEL), _
                    BitOR($LVS_EX_DOUBLEBUFFER, $LVS_EX_GRIDLINES, $LVS_EX_FLATSB, $LVS_EX_TRACKSELECT) )

Just FYI.

 

But the UDF doesn't appear to work as advertised. The most important capability I need is to use a larger font than the default, which is why I created my own logical font, size 16.  But the resulting size on the screen is much smaller. So I tried size 32, but it didn't make any difference.  Another problem I have is that there's a moveable vertical bar in the List View very close to the left edge which you have to push aside to the right to see more than 4-5 characters.  Do you know any way to avoid that and to increase the font size?

 

Edited by Mbee
Link to comment
Share on other sites

Are you sure, friend @Subz ? That's the UDF I started with, but I couldn't get the function that set a specific color for a given row to work. Everything came out black, which is why I switched to LarsJ's UDF.  But Melba's is a lot easier to use and comprehend, and of course the problem is certainly an error in my part (though the color function always returns success).

I'll re-investiage.  Thanks again!

Edited by Mbee
Typo
Link to comment
Share on other sites

Actually you should be able to set the font size just using GuiCtrlSetFont() you can also change a rows color using GuiCtrlSetBgColor() (needs to be assigned to the listview item).  If you only want a specific row colored, you can use _GUICtrlListView_GetItemParam(..) along with GuiCtrlSetBgColor(...), you would need to know the index though.

Link to comment
Share on other sites

Thank you so very much for sticking with me, @Subz !

I tried that, but I got an error from the built-in color functions as well as from Melba's. The parameters all look fine to me.

Perhaps using Melba's UDF along with certain built-in functions causes interaction issues? I've posted in Melba's UDF thread asking why I'm getting a puzzling error return when I call the colour (I guess Melba's European, not that those spellings aren't pleasing to the eye) functions. It returns a value of 4, which mean that there's an error in the row or column numbers, even though both are zero in the first call.

On another matter I mentioned above relating to the vertical column separation bar being so short, the solution is to use _GUICtrlListView_SetColumnWidth(). But although that call actually does what I want, it always returns an error.  Maybe I'll just ignore it...

Here's the new snippet...

#Include <GuiButton.au3>
#include <ColorConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiListView.au3>
#include <Misc.au3>
#include <StaticConstants.au3>
#include <UpDownConstants.au3>
#include <WindowsConstants.au3>

#include "GUIListViewEx.au3"    ; By Melba23 ; See: https://www.autoitscript.com/forum/topic/182492-guilistviewex-bugfix-version-26-apr-19/

Opt( "GUICloseOnESC", 1 )
Opt( "MustDeclareVars", 1 )
Opt( "GUIOnEventMode", 1 )


Global Const $Gc_Title = "Test"
Global $G_CurrentVidCount = 2
Global $G_CurrentVidList[2][4] = [ ["One", 0, 0, 0], ["Two", 0, 0, 0] ]

Local $L_Stat, $L_Error, $L_LVExID, $L_ItemID

Global $G_VidToolGUI = GUICreate("VidToolGUI",600,300,-1,-1,BitOr($WS_SYSMENU,$WS_HSCROLL,$WS_VSCROLL,$WS_VISIBLE,$DS_MODALFRAME), _
                                    BitOr($WS_EX_ACCEPTFILES,$WS_EX_APPWINDOW))

GUISetOnEvent($GUI_EVENT_CLOSE, "Gi_ExitClick", $G_VidToolGUI)

Local $L_LVStyle = BitOr($LVS_LIST, $LVS_ALIGNLEFT, $LVS_SHOWSELALWAYS, $LVS_SINGLESEL)
Local $L_LVExStyle = BitOR($LVS_EX_DOUBLEBUFFER, $LVS_EX_GRIDLINES, $LVS_EX_FLATSB, $LVS_EX_TRACKSELECT)

Global $G_LViewID = GUICtrlCreateListView( "Name", 600,266,-1,-1, $L_LVStyle, $L_LVExStyle )
If $G_LViewID = 0 Then
    $L_Error = @error
    MsgBox($MB_OK, $Gc_Title, "GUICtrlCreateListView() Failed - @error= " & $L_Error)
    Exit
EndIf

_GUICtrlListView_SetExtendedListViewStyle($G_LViewID, _
                        BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_DOUBLEBUFFER, $LVS_EX_GRIDLINES, $LVS_EX_FLATSB, $LVS_EX_TRACKSELECT) )

GUICtrlSetFont( $G_LViewID, 16, $FW_NORMAL, $GUI_FONTNORMAL, "Anonymous Pro", $PROOF_QUALITY )

$L_LVExID = _GUIListViewEx_Init( $G_LViewID, "", 0, Default, False, 32+64+256 )
$L_Stat = _GUIListViewEx_SetEditStatus( $L_LVExID, "*", 0 )             ; No editing

For $i = 0 To $G_CurrentVidCount -1

    $L_ItemID = _GUICtrlListView_AddItem( $G_LViewID, $G_CurrentVidList[$i][0], Default, 1000 )
    If $L_ItemID = -1 Then
        MsgBox($MB_OK, $Gc_Title, "_GUICtrlListView_AddItem(" & $i & ") Failed -- Aborting")
        Exit
    EndIf

    $L_Stat = _GUICtrlListView_SetBkColor($L_ItemID, $CLR_WHITE)
    If Not $L_Stat Then
        MsgBox($MB_OK, $Gc_Title, "_GUICtrlListView_SetBkColor(" & $i & ") Failed - Exiting")
        Exit
    EndIf

    _GUICtrlListView_SetTextColor($L_ItemID, $CLR_RED)
    If Not $L_Stat Then
        MsgBox($MB_OK, $Gc_Title, "_GUICtrlListView_SetTextColor(" & $i & ") (red) Failed - Exiting")
        Exit
    EndIf

Next

MsgBox($MB_OK, $Gc_Title, "Success")

Exit



Func Gi_ExitClick()
    Exit
EndFunc

 

Edited by Mbee
Link to comment
Share on other sites

Here is a cut down version of the code above, the color functions you're using are for the entire Listview not Listview Items, also the $L_ItemID is returning the index of the Listview not the handle.

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

#include "GUIListViewEx.au3"

Opt( "GUIOnEventMode", 1 )

Global Const $Gc_Title = "Test"
Global $L_Stat, $L_Error, $L_LVExID, $L_ItemID
Global $L_LVStyle = BitOr($LVS_LIST, $LVS_ALIGNLEFT, $LVS_SHOWSELALWAYS, $LVS_SINGLESEL)
Global $L_LVExStyle = BitOR($LVS_EX_DOUBLEBUFFER, $LVS_EX_GRIDLINES, $LVS_EX_FLATSB, $LVS_EX_TRACKSELECT)

Global $G_VidToolGUI = GUICreate("VidToolGUI", 600, 300)
    GUISetOnEvent($GUI_EVENT_CLOSE, "Gi_ExitClick", $G_VidToolGUI)
Global $G_LViewID = GUICtrlCreateListView( "Name", 0, 0, 600, 300, $L_LVStyle, $L_LVExStyle )
    GUICtrlSetFont( $G_LViewID, 16, $FW_NORMAL, $GUI_FONTNORMAL, "Anonymous Pro", $PROOF_QUALITY )
    $L_LVExID = _GUIListViewEx_Init( $G_LViewID, "", 0, Default, False, 32+64+256 )
    $L_Stat = _GUIListViewEx_SetEditStatus( $L_LVExID, "*", 0 )             ; No editing

    ;~ Add Listview Items
    ;~ Odd Numbered Row Items Aqua BackGround
    ;~ Even Numbered Row Items Red Text
    For $i = 0 To 9
        GUICtrlCreateListViewItem("Listview Item Text " & $i, $G_LViewID)
        If Mod($i, 2) = False Then
            GUICtrlSetBkColor(-1, $COLOR_AQUA)
        Else
            GUICtrlSetColor(-1, $COLOR_RED)
        EndIf
    Next

GUISetState(@SW_SHOW)

While 1
    Sleep(100)
WEnd

Func Gi_ExitClick()
    Exit
EndFunc

 

Link to comment
Share on other sites

Thanks enormously for all your tremendous help, @Subz !  I'm very pleased to learn this is and will continue to be easier than expected, in that many elements of Melba23's UDF aren't necessary to do what I want.  Hooray for Subz! 🤗

As an aside, when I read your post earlier, I immediately updated my full code to use your elegant solution. However, everything still ended up monochrome! And I practically bled my eyes trying to see what was different from your code above, and I just couldn't see it (too old -- or stupid-- I guess...) So instead of updating my code to use your methods, I moved the rest of my code into your snippet above, and it worked! Yay! Obviously I somehow messed up going the other direction, but who cares now that it's working correctly?

You have my total gratitude, good @Subz

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...