AndyS01 Posted January 13, 2011 Posted January 13, 2011 I want to set an item's color and attribute (bold, italics, etc.) in a listview. (I am forced to create the listview and it's individual items using _GUICtrlListView_... functions due to a sort issue.)My test code looks like this:expandcollapse popup#include <GUIConstantsEx.au3> #include <GuiListView.au3> #include <ColorConstants.au3> #include <FontConstants.au3> Opt("GUICloseOnESC", 1) ; ESC closes GUI Opt("GUIOnEventMode", 1) ; Change to OnEvent mode Opt('MustDeclareVars', 1) _Main() exit (1) Func _Main() Local $hMainWin, $hListview, $IDListview, $aList[10] For $x = 0 To UBound($aList) - 1 For $y = 0 To 3 $aList[$x] &= Chr(0x41 + $x) & $y & "|" ; Load the list with values Next Next $hMainWin = GUICreate("Test2", 320, 220) $hListview = createListView($hMainWin) loadItemsListView($hListview, $aList) GUISetOnEvent($GUI_EVENT_CLOSE, 'Event_GUIClose') ; CLOSE events setListViewRowColor($hListview, 1, $COLOR_RED, $aList) setListViewRowAttr($hListview, 3, $FW_BOLD, $aList) GUISetState() Do Until GUIGetMsg() = $GUI_EVENT_CLOSE GUIDelete() EndFunc ;==>_Main Func createListView($hWndMain) Local $flags, $xflags, $hWnd $flags = BitOR($LVS_REPORT, $flags, $LVS_SHOWSELALWAYS) $xflags = BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT, $LVS_EX_DOUBLEBUFFER) $hWnd = _GUICtrlListView_Create($hWndMain, "Col1|col2|col3|col4", 10, 10, 300, 200, $flags) _GUICtrlListView_SetExtendedListViewStyle($hWnd, $xflags) return ($hWnd) EndFunc ;==>createListView Func loadItemsListView($hWnd, ByRef $ar) Local $row, $item, $aItem, $iIndex _GUICtrlListView_DeleteAllItems($hWnd) For $row = 0 To UBound($ar) - 1 $item = $ar[$row] $aItem = StringSplit($item, "|") ; Break the item into parts $iIndex = _GUICtrlListView_AddItem($hWnd, $aItem[1], -1, $row) For $ndx = 2 To UBound($aItem) - 1 _GUICtrlListView_AddSubItem($hWnd, $iIndex, $aItem[$ndx], $ndx - 1) Next Next EndFunc ;==>loadItemsListView Func Event_GUIClose() exit (0) EndFunc ;==>Event_GUIClose Func setListViewRowColor($hWnd, $iItemNum, $iColor, ByRef $ar) EndFunc ;==>setListViewRowColor Func setListViewRowAttr($hWnd, $iItemNum, $iAttr, ByRef $ar) EndFunc ;==>setListViewRowAttrI need to fill in code in the setListViewRowColor() and setListViewRowAttr() functions.
enaiman Posted January 13, 2011 Posted January 13, 2011 Have you tried to search "Example Scripts" section? I remember that long ago I've seen someting similar made by Yashied or Rasim ... Have a look, I'm pretty sure you'll find something you can use. SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script wannabe "Unbeatable" Tic-Tac-Toe Paper-Scissor-Rock ... try to beat it anyway :)
guinness Posted January 13, 2011 Posted January 13, 2011 Or why not use small icons/bitmaps, search _GUICtrlListView_CreateSolidBitMap() in the Help File. This is what I have done in the past to show if (for example) a file has copied or not (green/red) UDF List: _AdapterConnections() • _AlwaysRun() • _AppMon() • _AppMonEx() • _ArrayFilter/_ArrayReduce • _BinaryBin() • _CheckMsgBox() • _CmdLineRaw() • _ContextMenu() • _ConvertLHWebColor()/_ConvertSHWebColor() • _DesktopDimensions() • _DisplayPassword() • _DotNet_Load()/_DotNet_Unload() • _Fibonacci() • _FileCompare() • _FileCompareContents() • _FileNameByHandle() • _FilePrefix/SRE() • _FindInFile() • _GetBackgroundColor()/_SetBackgroundColor() • _GetConrolID() • _GetCtrlClass() • _GetDirectoryFormat() • _GetDriveMediaType() • _GetFilename()/_GetFilenameExt() • _GetHardwareID() • _GetIP() • _GetIP_Country() • _GetOSLanguage() • _GetSavedSource() • _GetStringSize() • _GetSystemPaths() • _GetURLImage() • _GIFImage() • _GoogleWeather() • _GUICtrlCreateGroup() • _GUICtrlListBox_CreateArray() • _GUICtrlListView_CreateArray() • _GUICtrlListView_SaveCSV() • _GUICtrlListView_SaveHTML() • _GUICtrlListView_SaveTxt() • _GUICtrlListView_SaveXML() • _GUICtrlMenu_Recent() • _GUICtrlMenu_SetItemImage() • _GUICtrlTreeView_CreateArray() • _GUIDisable() • _GUIImageList_SetIconFromHandle() • _GUIRegisterMsg() • _GUISetIcon() • _Icon_Clear()/_Icon_Set() • _IdleTime() • _InetGet() • _InetGetGUI() • _InetGetProgress() • _IPDetails() • _IsFileOlder() • _IsGUID() • _IsHex() • _IsPalindrome() • _IsRegKey() • _IsStringRegExp() • _IsSystemDrive() • _IsUPX() • _IsValidType() • _IsWebColor() • _Language() • _Log() • _MicrosoftInternetConnectivity() • _MSDNDataType() • _PathFull/GetRelative/Split() • _PathSplitEx() • _PrintFromArray() • _ProgressSetMarquee() • _ReDim() • _RockPaperScissors()/_RockPaperScissorsLizardSpock() • _ScrollingCredits • _SelfDelete() • _SelfRename() • _SelfUpdate() • _SendTo() • _ShellAll() • _ShellFile() • _ShellFolder() • _SingletonHWID() • _SingletonPID() • _Startup() • _StringCompact() • _StringIsValid() • _StringRegExpMetaCharacters() • _StringReplaceWholeWord() • _StringStripChars() • _Temperature() • _TrialPeriod() • _UKToUSDate()/_USToUKDate() • _WinAPI_Create_CTL_CODE() • _WinAPI_CreateGUID() • _WMIDateStringToDate()/_DateToWMIDateString() • Au3 script parsing • AutoIt Search • AutoIt3 Portable • AutoIt3WrapperToPragma • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • DirToHTML5 • FileInstallr • FileReadLastChars() • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIEvents • GUIGetBkColor() • Int_Parse() & Int_TryParse() • IsISBN() • LockFile() • Mapping CtrlIDs • OOP in AutoIt • ParseHeadersToSciTE() • PasswordValid • PasteBin • Posts Per Day • PreExpand • Protect Globals • Queue() • Resource Update • ResourcesEx • SciTE Jump • Settings INI • SHELLHOOK • Shunting-Yard • Signature Creator • Stack() • Stopwatch() • StringAddLF()/StringStripLF() • StringEOLToCRLF() • VSCROLL • WM_COPYDATA • More Examples... Updated: 22/04/2018
AndyS01 Posted January 24, 2011 Author Posted January 24, 2011 OK, I solved it using code I found by Googling the problem. What I had to do was hook into the $NM_CUSTOMDRAW event for the listbox. Actually, it's pretty slick. All I had to do was to set each item's param with a color number when I added them to the listview, and in the hook code, read the color number and convert it to a RGB color, then set the clrText element to that value. It was important that I surround the listbox item creation with a _GUICtrlListView_BeginUpdate() and _GUICtrlListView_EndUpdate() to keep from firing off $NM_CUSTOMDRAW events. When the _GUICtrlListView_EndUpdate() is called, there will be an $NM_CUSTOMDRAW event fired off for each item. That's where I added the color. This technique could be used to set text to bold, italics, etc. as well. Here is a test script that demonstrates what I'm talking about: expandcollapse popup#include <GuiConstants.au3> #include <GuiListView.au3> #include <String.au3> #include <WindowsConstants.au3> #include <ColorConstants.au3> Opt("GUICloseOnESC", 0) ; ESC does not close GUI Opt("GUIOnEventMode", 1) ; Change to OnEvent mode Opt('MustDeclareVars', 1) OnAutoItExitRegister("Event_GUIClose") Opt("GUIEventOptions", 1) ;0=default, 1=just notification, 2=GuiCtrlRead tab index Global $mainWin_hWnd, $listView_hWnd, $listView_ID, $colorButton_hWnd, $colorButton_ID Global $aMasterList[6], $aColors[4] _Main() ExitStageLeft(0) ;;;;;;;;;;;;;;;;;;;;;;;;;; ; _Main() - Main function ;;;;;;;;;;;;;;;;;;;;;;;;;; Func _Main() createMainWin() createListView() createColorButton() createMasterList() setupColorsArray() GUISetOnEvent($GUI_EVENT_CLOSE, "Event_GUIClose") GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY") GUICtrlSetOnEvent($colorButton_ID, "handle_Color_btn") GUISetState() loadListView() ; importand to do this after registering WM_NOTIFY events While 1 Sleep(500) WEnd EndFunc ;==>_Main Func ExitStageLeft($ret) Exit ($ret) EndFunc ;==>ExitStageLeft Func Event_GUIClose() ExitStageLeft(1) EndFunc ;==>Event_GUIClose Func createMainWin() $mainWin_hWnd = GUICreate("Test2", 250, 200, -1, -1, BitOR($WS_CAPTION, $WS_SIZEBOX)) ConsoleWrite("+++: $mainWin_hWnd = " & $mainWin_hWnd & @CRLF) if ($mainWin_hWnd == 0) Then exit (1) EndFunc ;==>createMainWin Func createListView() $listView_ID = GUICtrlCreateListView("", 10, 10, 230, 160, $LVS_REPORT) $listView_hWnd = GUICtrlGetHandle($listView_ID) _GUICtrlListView_InsertColumn($listView_hWnd, 0, "Color#+Name", 120) _GUICtrlListView_InsertColumn($listView_hWnd, 1, "Ax", 50) _GUICtrlListView_InsertColumn($listView_hWnd, 2, "Bx", 50) EndFunc ;==>createListView Func createColorButton() $colorButton_ID = GUICtrlCreateButton("Bump row 3's color", 10, 170, 100, 30) $colorButton_hWnd = GUICtrlGetHandle($colorButton_ID) EndFunc ;==>createColorButton Func createMasterList() $aMasterList[0] = "Name1|A1|B1~0" $aMasterList[1] = "Name2|A2|B2~1" $aMasterList[2] = "Name3|A3|B3~2" $aMasterList[3] = "Name4|A4|B4~0" $aMasterList[4] = "Name5|A5|B5~1" $aMasterList[5] = "Name6|A6|B6~2" EndFunc ;==>createMasterList Func loadListView() Local $item, $parts, $colornum, $str, $iIndex, $hWnd $hWnd = $listView_hWnd _GUICtrlListView_DeleteAllItems($hWnd) _GUICtrlListView_BeginUpdate($hWnd) For $item In $aMasterList $parts = StringSplit($item, "~") ; split the item from the colornum $item = $parts[1] $colornum = $parts[2] $parts = StringSplit($item, "|") ; break up the item into parts $str = StringFormat("(%d) ", $colornum) & $parts[1] ; prefix with the colornum $iIndex = _GUICtrlListView_AddItem($hWnd, $str, -1, $colornum) _GUICtrlListView_AddSubItem($hWnd, $iIndex, $parts[2], 1) _GUICtrlListView_AddSubItem($hWnd, $iIndex, $parts[3], 2) Next _GUICtrlListView_EndUpdate($hWnd) EndFunc ;==>loadListView Func setupColorsArray() $aColors[0] = RGB2BGR($COLOR_BLACK) ; 0x000000 - Normal $aColors[1] = RGB2BGR($COLOR_RED) ; 0xFF0000 - Secure $aColors[2] = RGB2BGR($COLOR_BLUE) ; 0x0000FF - CNS $aColors[3] = RGB2BGR($COLOR_YELLOW) ; 0xFFFF00 - Other EndFunc ;==>setupColorsArray Func RGB2BGR($iColor) Local $sH = Hex($iColor, 6) Return '0x' & StringRight($sH, 2) & StringMid($sH, 3, 2) & StringLeft($sH, 2) EndFunc ;==>RGB2BGR ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; WM_NOTIFY(0 - Handle the WM_NOTIFY event ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam) #forceref $hWnd, $iMsg, $iwParam, $ilParam Local $hWndFrom, $iCode, $tNMHDR $tNMHDR = DllStructCreate($tagNMHDR, $ilParam) $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom")) $iCode = DllStructGetData($tNMHDR, "Code") Switch $hWndFrom Case $listView_hWnd Switch $iCode Case $NM_CUSTOMDRAW Local $iDrawStage, $tCustDraw $tCustDraw = DllStructCreate($tagNMLVCUSTOMDRAW, $ilParam) $iDrawStage = DllStructGetData($tCustDraw, 'dwDrawStage') Switch $iDrawStage Case $CDDS_ITEMPREPAINT ; 0x00010001 setItemColor($tCustDraw, $hWndFrom) Return $CDRF_NOTIFYSUBITEMDRAW EndSwitch EndSwitch EndSwitch Return $GUI_RUNDEFMSG EndFunc ;==>WM_NOTIFY Func setItemColor($tCustDraw, $hWnd) Local $iIndex, $color, $colornum $iIndex = DllStructGetData($tCustDraw, 'dwItemSpec') $colornum = _GUICtrlListView_GetItemParam($hWnd, $iIndex) $color = $aColors[$colornum] DllStructSetData($tCustDraw, 'clrText', $color) EndFunc ;==>setItemColor Func handle_Color_btn() Local $item, $parts $item = $aMasterList[2] $parts = StringSplit($item, "~") ; split the item from the colornum $parts[2] += 1 $parts[2] = Mod($parts[2], 4) $item = $parts[1] & "~" & $parts[2] $aMasterList[2] = $item loadListView() EndFunc ;==>handle_Color_btn
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