LarsJ Posted March 5, 2012 Share Posted March 5, 2012 (edited) I was looking for some examples of custom drawn TreeViews. I didn't find any so here is two. I have made the examples for ListViews too. The first example shows some named colors from the MS HTML documentation. The colors are sorted in different ways in five text files in the zipfile. The forecolor, backcolor, color name and RGB value is shown in the GUI's. Remark that the forecolor in the first column in the ListView is bold. Click on the colors in the TreeView to see some of the effects of custom drawing. The second example enumerates the fonts for the character sets in FontConstants.au3 and shows the fonts in the GUI's. Click on the fonts in the TreeView to see the name of the fonts. The zipfile contains 5 text files with color definitions and 4 au3-files with GUI's. CustDrawTvLv.zip 2016-03-22: For colors and fonts in listviews take a look at Colors and fonts in custom drawn ListViews - UDF version. Edited March 22, 2016 by LarsJ qsek, KaFu, FireFox and 1 other 4 Controls, File Explorer, ROT objects, UI Automation, Windows Message MonitorCompiled code: Accessing AutoIt variables, DotNet.au3 UDF, Using C# and VB codeShell menus: The Context menu, The Favorites menu. Shell related: Control Panel, System Image ListsGraphics related: Rubik's Cube, OpenGL without external libraries, Navigating in an image, Non-rectangular selectionsListView controls: Colors and fonts, Multi-line header, Multi-line items, Checkboxes and icons, Incremental searchListView controls: Virtual ListViews, Editing cells, Data display functions Link to comment Share on other sites More sharing options...
wakillon Posted March 5, 2012 Share Posted March 5, 2012 (edited) Nice examples Thanks ! Edited September 26, 2012 by wakillon AutoIt 3.3.14.2 X86 - SciTE 3.6.0 - WIN 8.1 X64 - Other Example Scripts Link to comment Share on other sites More sharing options...
Belini Posted March 30, 2012 Share Posted March 30, 2012 @LarsJ, good examples will be helpful to me. My Codes: Virtual Key Code UDF: http://www.autoitscript.com/forum/topic/138246-virtual-key-code-udf/ GuiSplashTextOn.au3: http://www.autoitscript.com/forum/topic/143542-guisplashtexton-udf/ Menu versions of Autoit: http://www.autoitscript.com/forum/topic/137435-menu-versions-of-autoit/#entry962011 Selects first folder of letters: ]http://www.autoitscript.com/forum/topic/144780-select-folders-by-letter/#entry1021708/spoiler] List files and folders with long addresses.: http://www.autoitscript.com/forum/topic/144910-list-files-and-folders-with-long-addresses/#entry102 2926 Program JUKEBOX made in Autoit:some functions:http://www.youtube.com/watch?v=WJ2tC2fD5Qs Navigation to search:http://www.youtube.com/watch?v=lblwOFIbgtQ Link to comment Share on other sites More sharing options...
jcpetu Posted September 10, 2012 Share Posted September 10, 2012 Hi LarsJ, nice example and code. is it possible to paint the selected row in a color other than blue? Link to comment Share on other sites More sharing options...
LarsJ Posted September 13, 2012 Author Share Posted September 13, 2012 (edited) jcpetu, Yes it is. The key point is to set the selected state of the selected item to false. If you only use single selection you can use the focused state to keep track of the selected item. If you use multiple selections you have to keep track of the selected items yourself. In the example in the zip (you need the text files from the original zip) the focused state is used to keep track of the selected item which is painted with a cyan backcolor. Regards Lars lvCustDrawColors2.zip 2016-03-22: For a better way to change background color of selected rows take a look at Colors and fonts in custom drawn ListViews - UDF version. Changing state values to be able to set the background color, as is done here, can easily become a mess. Edited March 22, 2016 by LarsJ Controls, File Explorer, ROT objects, UI Automation, Windows Message MonitorCompiled code: Accessing AutoIt variables, DotNet.au3 UDF, Using C# and VB codeShell menus: The Context menu, The Favorites menu. Shell related: Control Panel, System Image ListsGraphics related: Rubik's Cube, OpenGL without external libraries, Navigating in an image, Non-rectangular selectionsListView controls: Colors and fonts, Multi-line header, Multi-line items, Checkboxes and icons, Incremental searchListView controls: Virtual ListViews, Editing cells, Data display functions Link to comment Share on other sites More sharing options...
jcpetu Posted September 13, 2012 Share Posted September 13, 2012 LarsJ, thanks a lot and regards. Link to comment Share on other sites More sharing options...
LarsJ Posted July 4, 2013 Author Share Posted July 4, 2013 How to create a ListView with only a few colored cells. I got a message with that question some days ago.Here is an example:expandcollapse popup#include <GUIConstants.au3> #include <WindowsConstants.au3> #include <FontConstants.au3> #include <GuiListView.au3> #include <WinAPI.au3> Opt( "MustDeclareVars", 1 ) Global $hGui, $hLV, $hLVfont, $hLVfontBold, $aColors[1][1] ; Use an array to store cell colors MainFunc() Func MainFunc() ; Create GUI $hGui = GUICreate( "ListView with a few colored and bold cells", 549, 310, 400, 200 ) ; Create ListView control Local $idLV = GUICtrlCreateListView( "", 10, 10, 529, 290, $GUI_SS_DEFAULT_LISTVIEW, BitOR( $WS_EX_CLIENTEDGE, $LVS_EX_FULLROWSELECT ) ) $hLV = ControlGetHandle( $hGui, "", $idLV ) ; Add 4 columns _GUICtrlListView_AddColumn( $hLV, "Column 0", 125 ) _GUICtrlListView_AddColumn( $hLV, "Column 1", 125 ) _GUICtrlListView_AddColumn( $hLV, "Column 2", 125 ) _GUICtrlListView_AddColumn( $hLV, "Column 3", 125 ) ; Add 50 rows Local $aRows[50][4] For $i = 0 To 9 $aRows[$i][0] = "Row " & $i & ", Column 0" $aRows[$i][1] = "Row " & $i & ", Column 1" $aRows[$i][2] = "Row " & $i & ", Column 2" $aRows[$i][3] = "Row " & $i & ", Column 3" Next For $i = 10 To 49 $aRows[$i][0] = "Row " & $i & ", Column 0" $aRows[$i][1] = "Row " & $i & ", Column 1" $aRows[$i][2] = "Row " & $i & ", Column 2" $aRows[$i][3] = "Row " & $i & ", Column 3" Next _GUICtrlListView_AddArray( $hLV, $aRows ) ; ReDim $aColors to match the ListView ReDim $aColors[50][4] ; Make a few cells colored $aColors[ 0][2] = ColorConvert( 0xFF0000 ) ; Set ( 0, 2 ) to red $aColors[ 5][0] = ColorConvert( 0xFF0000 ) ; Set ( 5, 0 ) to red $aColors[ 9][1] = ColorConvert( 0xFF0000 ) ; Set ( 9, 1 ) to red $aColors[ 9][3] = ColorConvert( 0x0000FF ) ; Set ( 9, 3 ) to blue $aColors[35][1] = ColorConvert( 0x00FF00 ) ; Set ( 35, 1 ) to green $aColors[45][1] = ColorConvert( 0x00FF00 ) ; Set ( 45, 1 ) to green $aColors[45][2] = ColorConvert( 0x0000FF ) ; Set ( 45, 2 ) to blue ; To clear a color set $aColors[row][col] = 0 ; Black ; Set row 15 to BlueViolet = 0x8A2BE2 $aColors[15][0] = ColorConvert( 0x8A2BE2 ) $aColors[15][1] = ColorConvert( 0x8A2BE2 ) $aColors[15][2] = ColorConvert( 0x8A2BE2 ) $aColors[15][3] = ColorConvert( 0x8A2BE2 ) ; Use a bold font in colored cells ; Get the font of the ListView control ; Copied from the _GUICtrlGetFont example by KaFu ; See http://www.autoitscript.com/forum/index.php?showtopic=124526 Local $hDC = _WinAPI_GetDC($hLV) Local $hFont = _SendMessage($hLV, $WM_GETFONT) Local $hObject = _WinAPI_SelectObject($hDC, $hFont) Local $lvLOGFONT = DllStructCreate($tagLOGFONT) Local $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 ) Local $iWeight = BitOR( DllStructGetData( $lvLOGFONT, "Weight" ), $FW_BOLD ) DllStructSetData( $lvLOGFONT, "Weight", $iWeight ) $hLVfontBold = _WinAPI_CreateFontIndirect( $lvLOGFONT ) ; Use WM_NOTIFY events to draw the cells GUIRegisterMsg( $WM_NOTIFY, "WM_NOTIFY" ) ; Show GUI GUISetState( @SW_SHOW, $hGui ) ; Message loop Local $iMsg While 1 $iMsg = GUIGetMsg() Switch $iMsg Case $GUI_EVENT_CLOSE ExitLoop EndSwitch WEnd GUIDelete( $hGui ) Exit 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 ; Holds a value that specifies the drawing stage Case $CDDS_PREPAINT ; Before the paint cycle begins Return $CDRF_NOTIFYITEMDRAW ; Notify the parent window of any item-related drawing operations Case $CDDS_ITEMPREPAINT ; Before painting an item Return $CDRF_NOTIFYSUBITEMDRAW ; Notify the parent window of any subitem-related drawing operations Case BitOR( $CDDS_ITEMPREPAINT, $CDDS_SUBITEM ) ; Before painting a subitem Local $dwItemSpec = DllStructGetData($tNMLVCUSTOMDRAW, "dwItemSpec") ; Item index Local $iSubItem = DllStructGetData($tNMLVCUSTOMDRAW, "iSubItem") ; Subitem index Local $hDC = DllStructGetData($tNMLVCUSTOMDRAW, "HDC") ; Handle to the item's device context If $aColors[$dwItemSpec][$iSubItem] Then _WinAPI_SelectObject($hDC, $hLVfontBold) ; Bold font Else _WinAPI_SelectObject($hDC, $hLVfont) ; Normal font EndIF DllStructSetData( $tNMLVCUSTOMDRAW, "ClrText", $aColors[$dwItemSpec][$iSubItem]) ; Forecolor of item text Return $CDRF_NEWFONT ; $CDRF_NEWFONT must be returned after changing font or colors EndSwitch EndSwitch EndSwitch Return $GUI_RUNDEFMSG EndFunc ;RGB to BGR or BGR to RGB Func ColorConvert($iColor) Return BitOR(BitAND($iColor, 0x00FF00), BitShift(BitAND($iColor, 0x0000FF), -16), BitShift(BitAND($iColor, 0xFF0000), 16)) EndFuncYou don't need anything else to run the example.The colored cells are drawn with a bold font. If you don't need that, it should be easy to remove. Controls, File Explorer, ROT objects, UI Automation, Windows Message MonitorCompiled code: Accessing AutoIt variables, DotNet.au3 UDF, Using C# and VB codeShell menus: The Context menu, The Favorites menu. Shell related: Control Panel, System Image ListsGraphics related: Rubik's Cube, OpenGL without external libraries, Navigating in an image, Non-rectangular selectionsListView controls: Colors and fonts, Multi-line header, Multi-line items, Checkboxes and icons, Incremental searchListView controls: Virtual ListViews, Editing cells, Data display functions Link to comment Share on other sites More sharing options...
DarkAaly Posted August 31, 2013 Share Posted August 31, 2013 (edited) @LarsJ: I tried to adapt the example to a listview but I did not succeed. Could you kindly create a quick example? Really thanks man. ^^ Edited August 31, 2013 by DarkAaly Link to comment Share on other sites More sharing options...
LarsJ Posted August 31, 2013 Author Share Posted August 31, 2013 DarkAaly, Can you provide a little more information about what it is you want? Do you have some code that shows what you need? There are already some examples in the posts above but it's probably not what you need.Regards Lars. Controls, File Explorer, ROT objects, UI Automation, Windows Message MonitorCompiled code: Accessing AutoIt variables, DotNet.au3 UDF, Using C# and VB codeShell menus: The Context menu, The Favorites menu. Shell related: Control Panel, System Image ListsGraphics related: Rubik's Cube, OpenGL without external libraries, Navigating in an image, Non-rectangular selectionsListView controls: Colors and fonts, Multi-line header, Multi-line items, Checkboxes and icons, Incremental searchListView controls: Virtual ListViews, Editing cells, Data display functions Link to comment Share on other sites More sharing options...
DarkAaly Posted September 1, 2013 Share Posted September 1, 2013 (edited) Thanks for the reply. This is a small script that they asked me to do, I would like to change the font or color to the folders that contain characters in the name, such as "[APERTA]", "[CHIUSA]" and "[sOSPESA]," but in 'example above was applied to a list. Thank you. EDIT: The example that I needed stop working when I try to run "vCustDrawColors.au3" and "tvCustDrawFonts.au3" works but the font do not change. Nuova cartella.rar Edited September 1, 2013 by DarkAaly Link to comment Share on other sites More sharing options...
LarsJ Posted September 1, 2013 Author Share Posted September 1, 2013 This code will draw the first level items of the TV with a red forecolor:Case $NM_CUSTOMDRAW Local $tNMTVCUSTOMDRAW = DllStructCreate($tagNMTVCUSTOMDRAW, $lParam) Local $dwDrawStage = DllStructGetData($tNMTVCUSTOMDRAW, "DrawStage") Switch $dwDrawStage ; Holds a value that specifies the drawing stage Case $CDDS_PREPAINT ; Before the paint cycle begins Return BitOr( $CDRF_NOTIFYITEMDRAW, _ ; Notify the parent window of any item-related drawing operations $CDRF_NOTIFYPOSTPAINT ) ; Notify the parent window after item-related drawing operations Case $CDDS_ITEMPREPAINT ; Before painting an item Local $hItemSpec = DllStructGetData($tNMTVCUSTOMDRAW, "ItemSpec") ; $hItemSpec = $hItem If _GUICtrlTreeView_Level( $hTV, $hItemSpec ) = 1 Then DllStructSetData( $tNMTVCUSTOMDRAW, "ClrText", 0x0000FF ) ; Forecolor of item text EndIf Return BitOr( $CDRF_NEWFONT, _ ; $CDRF_NEWFONT must be returned after changing font or colors $CDRF_NOTIFYPOSTPAINT ) ; Notify the parent window after item-related drawing operations EndSwitchInsert the code in function WM_NOTIFY in __Gestore.au3 immediately after these three lines:Switch $hTV Case $hTreeView Switch $IDLars Controls, File Explorer, ROT objects, UI Automation, Windows Message MonitorCompiled code: Accessing AutoIt variables, DotNet.au3 UDF, Using C# and VB codeShell menus: The Context menu, The Favorites menu. Shell related: Control Panel, System Image ListsGraphics related: Rubik's Cube, OpenGL without external libraries, Navigating in an image, Non-rectangular selectionsListView controls: Colors and fonts, Multi-line header, Multi-line items, Checkboxes and icons, Incremental searchListView controls: Virtual ListViews, Editing cells, Data display functions Link to comment Share on other sites More sharing options...
DarkAaly Posted September 2, 2013 Share Posted September 2, 2013 I'll try. Thanks again. Link to comment Share on other sites More sharing options...
DarkAaly Posted September 10, 2013 Share Posted September 10, 2013 (edited) It works, thanks! And if i want to change the elements font? I just need to set bold. Edited September 10, 2013 by DarkAaly Link to comment Share on other sites More sharing options...
LarsJ Posted September 10, 2013 Author Share Posted September 10, 2013 Good to see.BoldYou can use this command:_GUICtrlTreeView_SetState( $hTV, $item, $TVIS_BOLD )when you create the TV. Controls, File Explorer, ROT objects, UI Automation, Windows Message MonitorCompiled code: Accessing AutoIt variables, DotNet.au3 UDF, Using C# and VB codeShell menus: The Context menu, The Favorites menu. Shell related: Control Panel, System Image ListsGraphics related: Rubik's Cube, OpenGL without external libraries, Navigating in an image, Non-rectangular selectionsListView controls: Colors and fonts, Multi-line header, Multi-line items, Checkboxes and icons, Incremental searchListView controls: Virtual ListViews, Editing cells, Data display functions Link to comment Share on other sites More sharing options...
DarkAaly Posted September 10, 2013 Share Posted September 10, 2013 Done and work. Really thanks man, nice job. Link to comment Share on other sites More sharing options...
LarsJ Posted September 10, 2013 Author Share Posted September 10, 2013 You are welcome. Controls, File Explorer, ROT objects, UI Automation, Windows Message MonitorCompiled code: Accessing AutoIt variables, DotNet.au3 UDF, Using C# and VB codeShell menus: The Context menu, The Favorites menu. Shell related: Control Panel, System Image ListsGraphics related: Rubik's Cube, OpenGL without external libraries, Navigating in an image, Non-rectangular selectionsListView controls: Colors and fonts, Multi-line header, Multi-line items, Checkboxes and icons, Incremental searchListView controls: Virtual ListViews, Editing cells, Data display functions Link to comment Share on other sites More sharing options...
LarsJ Posted March 10, 2015 Author Share Posted March 10, 2015 (edited) weirddave, This is code for your example here. The code sets the background color in third column to red/green for last/second last row. expandcollapse popup#include <WindowsConstants.au3> #include <GUIConstantsEx.au3> #include <GuiListView.au3> Global $idLast1 = -1, $idLast2 = -1 ; Last and second last row $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 ) GUISetState(@SW_SHOW) GUIRegisterMsg( $WM_NOTIFY, "WM_NOTIFY" ) while 1 $msg = GUIGetMsg() Switch $msg Case $GUI_EVENT_CLOSE ExitLoop Case $idButton $idLast2 = $idLast1 $idLast1 = GUICtrlCreateListViewItem("a|b|c", $idListview) _GUICtrlListView_RedrawItems( $hListview, 0, _GUICtrlListView_GetItemCount( $hListview ) - 1 ) EndSwitch wend 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 $hListview Switch $iCode Case $NM_CUSTOMDRAW Local $tNMLVCUSTOMDRAW = DllStructCreate( $tagNMLVCUSTOMDRAW, $lParam ) Local $dwDrawStage = DllStructGetData( $tNMLVCUSTOMDRAW, "dwDrawStage" ) Switch $dwDrawStage ; Holds a value that specifies the drawing stage Case $CDDS_PREPAINT ; Before the paint cycle begins Return $CDRF_NOTIFYITEMDRAW ; Notify the parent window of any ITEM-related drawing operations Case $CDDS_ITEMPREPAINT ; Before painting an item Return $CDRF_NOTIFYSUBITEMDRAW ; Notify the parent window of any SUBITEM-related drawing operations Case BitOR( $CDDS_ITEMPREPAINT, $CDDS_SUBITEM ) ; Before painting a subitem Local $iItemParam = DllStructGetData( $tNMLVCUSTOMDRAW, "lItemlParam" ) ; Item param (item control ID) Local $iSubItem = DllStructGetData( $tNMLVCUSTOMDRAW, "iSubItem" ) ; Subitem index Switch $iItemParam Case $idLast1 ; Last row Switch $iSubItem Case 2 ; Third column DllStructSetData( $tNMLVCUSTOMDRAW, "clrTextBk", 0x0000FF ) ; Backcolor red, BGR Case Else ; Other columns DllStructSetData( $tNMLVCUSTOMDRAW, "ClrTextBk", 0xFFFFFF ) ; Backcolor white EndSwitch Case $idLast2 ; Second last row Switch $iSubItem Case 2 ; Third column DllStructSetData( $tNMLVCUSTOMDRAW, "clrTextBk", 0x00FF00 ) ; Backcolor green, BGR Case Else ; Other columns DllStructSetData( $tNMLVCUSTOMDRAW, "ClrTextBk", 0xFFFFFF ) ; Backcolor white EndSwitch Case Else ; Other rows DllStructSetData( $tNMLVCUSTOMDRAW, "ClrTextBk", 0xFFFFFF ) ; Backcolor white EndSwitch Return $CDRF_NEWFONT ; $CDRF_NEWFONT must be returned after changing font or colors EndSwitch EndSwitch EndSwitch Return $GUI_RUNDEFMSG EndFunc Edited March 10, 2015 by LarsJ Controls, File Explorer, ROT objects, UI Automation, Windows Message MonitorCompiled code: Accessing AutoIt variables, DotNet.au3 UDF, Using C# and VB codeShell menus: The Context menu, The Favorites menu. Shell related: Control Panel, System Image ListsGraphics related: Rubik's Cube, OpenGL without external libraries, Navigating in an image, Non-rectangular selectionsListView controls: Colors and fonts, Multi-line header, Multi-line items, Checkboxes and icons, Incremental searchListView controls: Virtual ListViews, Editing cells, Data display functions Link to comment Share on other sites More sharing options...
weirddave Posted March 10, 2015 Share Posted March 10, 2015 This does indeed work. I'm now trying to understand what it's actually doing. The log window I'm trying to create will need to keep the colour/color, so I figured just comment out all but the last row third column case. This didn't work, I assume because all of the items are being redrawn. So, changed it so that only the last row is redrawn. At first I thought this was working, but as the window filled up and items scrolled off the bottom, I realised that they were losing their colour information. Does this mean I need to create an ever expanding list containing the colour for each item? Here's my lightly modified code: expandcollapse popup#include <WindowsConstants.au3> #include <GUIConstantsEx.au3> #include <GuiListView.au3> Global $idLast1 = -1, $idLast2 = -1 ; Last and second last row $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 ) GUISetState(@SW_SHOW) GUIRegisterMsg( $WM_NOTIFY, "WM_NOTIFY" ) while 1 $msg = GUIGetMsg() Switch $msg Case $GUI_EVENT_CLOSE ExitLoop Case $idButton $idLast1 = GUICtrlCreateListViewItem("a|b|c", $idListview) $itemcount = _GUICtrlListView_GetItemCount( $hListview ) - 1 _GUICtrlListView_RedrawItems( $hListview, $itemcount, $itemcount) EndSwitch wend 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 $hListview Switch $iCode Case $NM_CUSTOMDRAW Local $tNMLVCUSTOMDRAW = DllStructCreate( $tagNMLVCUSTOMDRAW, $lParam ) Local $dwDrawStage = DllStructGetData( $tNMLVCUSTOMDRAW, "dwDrawStage" ) Switch $dwDrawStage ; Holds a value that specifies the drawing stage Case $CDDS_PREPAINT ; Before the paint cycle begins Return $CDRF_NOTIFYITEMDRAW ; Notify the parent window of any ITEM-related drawing operations Case $CDDS_ITEMPREPAINT ; Before painting an item Return $CDRF_NOTIFYSUBITEMDRAW ; Notify the parent window of any SUBITEM-related drawing operations Case BitOR( $CDDS_ITEMPREPAINT, $CDDS_SUBITEM ) ; Before painting a subitem Local $iItemParam = DllStructGetData( $tNMLVCUSTOMDRAW, "lItemlParam" ) ; Item param (item control ID) Local $iSubItem = DllStructGetData( $tNMLVCUSTOMDRAW, "iSubItem" ) ; Subitem index Switch $iItemParam Case $idLast1 ; Last row Switch $iSubItem Case 2 ; Third column DllStructSetData( $tNMLVCUSTOMDRAW, "clrTextBk", 0x0000FF ) ; Backcolor red, BGR EndSwitch EndSwitch Return $CDRF_NEWFONT ; $CDRF_NEWFONT must be returned after changing font or colors EndSwitch EndSwitch EndSwitch Return $GUI_RUNDEFMSG EndFunc If I do have to create a list of colours, it will save the problem of how to pass the newest one to WM_NOTIFY Link to comment Share on other sites More sharing options...
mpower Posted March 10, 2015 Share Posted March 10, 2015 (edited) This does indeed work. I'm now trying to understand what it's actually doing. The log window I'm trying to create will need to keep the colour/color, so I figured just comment out all but the last row third column case. This didn't work, I assume because all of the items are being redrawn. So, changed it so that only the last row is redrawn. At first I thought this was working, but as the window filled up and items scrolled off the bottom, I realised that they were losing their colour information. Does this mean I need to create an ever expanding list containing the colour for each item? Here's my lightly modified code: Here's some code to get you started: expandcollapse popup#include <GUIConstants.au3> #include <WindowsConstants.au3> #include <FontConstants.au3> #include <GuiListView.au3> #include <WinAPI.au3> Global $idLast1 = -1, $idLast2 = -1 ; Last and second last row $MyGUI = GUICreate("listview items", 320, 200, 0,-1, -1) _WinAPI_SetClassLongEx($MyGUI, -26, BitAND(_WinAPI_GetClassLongEx($MyGUI, -26), BitNOT(1), BitNOT(2))) $idListview = GUICtrlCreateListView("A|B|C", 10, 10, 250, 155, 0x200) _GUICtrlListView_SetExtendedListViewStyle($idListview, $LVS_EX_DOUBLEBUFFER) $hLV = ControlGetHandle($MyGUI, "", $idListview) $idButton = GUICtrlCreateButton("Add", 125, 175, 70, 20) $hListview = GUICtrlGetHandle( $idListview ) $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" ) GUISetState(@SW_SHOW) while 1 $msg = GUIGetMsg() Switch $msg Case $GUI_EVENT_CLOSE ExitLoop Case $idButton $idLast1 = GUICtrlCreateListViewItem("a|b|c", $idListview) $itemcount = _GUICtrlListView_GetItemCount( $hListview ) - 1 _GUICtrlListView_RedrawItems( $hListview, $itemcount, $itemcount) EndSwitch wend 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 2 _WinAPI_SelectObject($hDC, $hLVfont) DllStructSetData( $tNMLVCUSTOMDRAW, "ClrText", ColorConvert(0x000000)) DllStructSetData( $tNMLVCUSTOMDRAW, "ClrTextBk", ColorConvert(0xFF0000)) 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 As you can see we custom draw the red bg in column 2 but we also custom draw all other colums as normal (white bg/black text) I've set a couple of styles to the GUI and the ListView to remove any flicker when scrolling etc Edited March 10, 2015 by mpower Link to comment Share on other sites More sharing options...
weirddave Posted March 10, 2015 Share Posted March 10, 2015 I need the colour in column 2 to be dependent on some other variable, not always red, but once it's set to red, green or white, I don't want to change it, it's so I can quickly locate problems which are being logged. A bit of googling suggests there may be a limit on the number of items I can add to the listview, am I going down completely the wrong path for a log window? Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now