Combined CreateTreeView and TreeViewColor

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
ScottMeyer
Posts: 33
Joined: 27 Sep 2017, 12:52
Contact:

Combined CreateTreeView and TreeViewColor

06 Dec 2017, 09:04

Hi,
I combined 2 functions
CreateTreeView by Learning one https://autohotkey.com/board/topic/9286 ... etreeview/
Treeview Custom Item Colors by maestrith https://autohotkey.com/boards/viewtopic ... View+color

Code: Select all

; Combined 2 functions 			by Scott Meyer		https://autohotkey.com/boards/viewtopic.php?f=6&t=40978
; CreateTreeView 				by Learning one 	https://autohotkey.com/board/topic/92863-function-createtreeview/
; Treeview Custom Item Colors 	by maestrith 		https://autohotkey.com/boards/viewtopic.php?f=6&t=2632&hilit=TreeView+color


; TvDefinition Requis : "NameItem"|"Bold Expand"|"Color"
TvDefinition=
(
Menu 1|Bold|Red
Menu 2
Menu 3|Bold Expand|Yellow
	Menu3_C1|Bold Expand|
		Menu3_C1_C1|Expand|Red
			Menu3_C1_C1_C1|Expand|Green
				Menu3_C1_C1_C1_C1||
			Menu3_C1_C1_C2|-Expand|Bleu	
				Menu3_C1_C1_C2_C1|Bold|Yellow
	Menu3_C2
		Menu3_C2_C1|Bold Expand|Green
			Menu3_C2_C1_C1
				Menu3_C2_C1_C1||Red
				Menu3_C2_C1_C2|Bold
Menu 4||Green
	Menu4_C1||Bleu
Last Menu|Bold
)	

Gui, Add , Text		, w350 Center 							, Tree structure
Gui, Add , Button 	, w80		xm	gOuvrir_ARBO Section	, Open tree
Gui, Add , Button 	, w80		ys	gFermer_ARBO			, Close tree
Gui, Add , Button 	, w80		ys	gSelect_Next_Item		, Next Item 
Gui, Add , Button 	, w80		ys	gDisable_Color			, Disable Color 
Gui, Add , Button 	, w80		ys	gEnable_Color			, Enable Color
Gui, Add , TreeView	, w440 h350 xm 	gMyTree vMyTree hwndhTreeview1 BackgroundA9BCF5 c000000 
Gui, Add , Text		, 										, Click for change random color
Gui, Show			, w480 h430 x1200
Gui, TreeView		,% hTreeview1

lv1 := new CLV(hTreeview1) 					; Instantiate a class for each TV
CreateTreeView(TvDefinition)
Block_Once = Yes						 	; Prevent problem first Select item 
WinSet,Redraw,,A
return

Ouvrir_ARBO:
ItemID = 0  ; Causes the loop's first iteration to start the search at the top of the tree.
Loop
{
	ItemID := TV_GetNext(ItemID, "Full")  ; Replace "Full" with "Checked" to find all checkmarked items.
	if not ItemID  ; No more items in tree.
		break
	TV_GetText(ItemText, ItemID)
	TV_Modify(ItemID, "+Expand")
}
return

Fermer_ARBO:
ItemID = 0  ; Causes the loop's first iteration to start the search at the top of the tree.
Loop
{
	ItemID := TV_GetNext(ItemID, "Full")  ; Replace "Full" with "Checked" to find all checkmarked items.
	if not ItemID  ; No more items in tree.
		break
	TV_GetText(ItemText, ItemID)
	TV_Modify(ItemID, "-Expand")
}
return

Select_Next_Item:
ItemID := TV_GetNext(ItemID, "Full")
TV_Modify(ItemID, "Select")
return

Disable_Color:
lv1.UnRegister()							; Disable colouring
WinSet,Redraw,,A
return

Enable_Color:
lv1.Register()								; Re-enable colouring
WinSet,Redraw,,A
return

MyTree:
if A_GuiEvent = S
{
	If Block_Once = 
	{
	TV_GetText(SelectedItemText, A_EventInfo)
	NewColorHTML =
	Loop, 6								; Generate random color HTML
	{
		Random, Letter_or_Number, 1, 2
		if Letter_or_Number = 1
		{
			Random, CHARACTER, 65, 70
			NewColorHTML .= Chr(CHARACTER)
		}
		else
		{
			Random, Number, 0, 9
			NewColorHTML .= Number
		}
	}
	NewColorHTML = 0x%NewColorHTML%
	lv1.ModifyNode(A_EventInfo,NewColorHTML)
	WinSet,Redraw,,A
	}
}

if A_GuiEvent = DoubleClick
{
	TV_GetText(SelectedItemText, A_EventInfo)
	Tooltip, Double click on " %SelectedItemText% "
}

Block_Once =
return

ApplyColor:					; Bleu Green Red
if SortirCouleur = Red
	lv1.ModifyNode(NumElement,0x0000FF)	
if SortirCouleur = Yellow
	lv1.ModifyNode(NumElement,0x00FFFF)	
if SortirCouleur = Bleu
	lv1.ModifyNode(NumElement,0xFF0000)	
if SortirCouleur = Green
	lv1.ModifyNode(NumElement,0x00FF00)	
WinSet,Redraw,,A
return

CreateTreeView(TreeViewDefinitionString) {	; by Learning one
	IDs := {}
	Loop, parse, TreeViewDefinitionString, `n, `r
	{
		global NumElement	; ID de l'element a colorier	(sorti de donnee de la fonction)
		global SortirCouleur ;								(sorti de donnee de la fonction)
		if A_LoopField is space
			continue
		Item := RTrim(A_LoopField, A_Space A_Tab), Item := LTrim(Item, A_Space), Level := 0
		While (SubStr(Item,1,1) = A_Tab)
			Level += 1,	Item := SubStr(Item, 2)
		StringSplit, match, Item, |,			;	match1 = ItemName, match2 = Options, match3 = COLOR
		if (Level=0)
		{
			IDs["Level0"] := TV_Add(match1, 0, match2 " ")	; <- argument possible " Sort"
			NumElement := IDs["Level0"]
			SortirCouleur := match3
			Gosub, ApplyColor
			match2 = 
			match3 = 
		}
		else
		{
			IDs["Level" Level] := TV_Add(match1, IDs["Level" Level-1], match2 " ")	; <- argument possible " Sort"
			NumElement := IDs["Level" Level]
			SortirCouleur := match3
			Gosub, ApplyColor
			match2 =
			match3 =
		}
	}
} 	; http://www.autohotkey.com/board/topic/92863-function-createtreeview/

Class CLV {
	Nodes := {}
	
	__New(hwnd){
		this.hwnd := hwnd
		this.MsgFn := this.WM_NOTIFY.Bind(this)
		this.Register()
	}
	
	Register(){
		OnMessage(0x4e,this.MsgFn)
	}
	
	UnRegister(){
		OnMessage(0x4e,this.MsgFn, 0)
	}
	
	ModifyNode(hNode, col){
		this.Nodes[hNode] := col
	}
	
	WM_NOTIFY(Param*){
		static o_hwndFrom := 0, o_code := 2*A_PtrSize, o_dwDrawStage := 3*A_PtrSize, o_dwItemSpec := 16+(5*A_PtrSize), o_fg := 16+(8*A_PtrSize), o_bg := o_fg+4
		if (NumGet(Param.2, o_hwndFrom) != this.hwnd)	; filter messages not for this treeview
			return
		stage := NumGet(Param.2, o_dwDrawStage, "uint")
		if (stage == 1 && NumGet(Param.2, o_code, "int") == -12)
			return 0x20 ;sets CDRF_NOTIFYITEMDRAW
		node := numget(Param.2, o_dwItemSpec, "uint")
		if (stage == 0x10001 && ObjHasKey(this.Nodes, node)){
			NumPut(this.Nodes[node],Param.2, o_bg,"int") ;sets the background
			;NumPut(0x0,Param.2, o_fg,"int") ;sets the foreground
		}
	}
}
JEE (jeeswg) "Better WinGetPos" : LIEN Forum AHK
AHK Vision Go : LIEN Forum AHK
Simply Backup Script : LIEN Forum AHK

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 68 guests