Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

ListView Custom Colors [Ahk_1.1]


  • Please log in to reply
2 replies to this topic
maestrith
  • Members
  • 786 posts
  • Last active: Apr 10 2019 01:28 PM
  • Joined: 17 Sep 2005

Hopefully someone can use this.  A while back I was trying to figure out how to change the colors of individual listview items.

 

I took information from Here which borrowed information from SKAN's dropbox and came up with this.

#SingleInstance,Force
Gui, +LastFound
Gui, Add, ListView, x5 y5 w200 h200 vLV_Sample hwndlv,index|day
lv:=new ListView(lv)
LV.Add("",1,"Monday")
LV.Add("",2,"Tuesday")
LV.Add("",3,"Wednesday")
LV.Add("",4,"Thursday")
LV.Add("",5,"Friday")
LV.Add("",6,"Saturday")
LV.Add("",7,"Sunday")
Gui,Show,x50 y50 w210 h210
Loop,2
	LV_ModifyCol(A_Index,"AutoHdr")
Loop,5
{
	Loop,7
	{
		lv.Color(A_Index,0xff00ff,0)
		WinSet,Redraw,,A
		sleep,100
		lv.clear()
	}
	Loop,5
	{
		lv.Color(7-A_Index,0xff00ff,0)
		WinSet,Redraw,,A
		sleep,100
		lv.clear()
	}
}
WinSet,Redraw,,A
Return 
GuiClose:
GuiEscape:
Exitapp
class ListView{
	static list:=[]
	__New(hwnd){
		this.list[hwnd]:=this
		OnMessage(0x4e,"WM_NOTIFY")
		this.hwnd:=hwnd
		this.control:=[]
	}
	add(options,items*){
		Gui,ListView,% this.hwnd
		for a,b in items{
			if A_Index=1
				item:=LV_Add(options,b)
			Else
				LV_Modify(item,"col" A_Index,b)
		}
	}
	clear(){
		this.control:=[]
	}
	Color(item,fore="",back=""){
		LV_GetText(text,item)
		if fore!=""
			this.Control[text,"fore"]:=fore
		if Back!=""
			this.Control[text,"back"]:=back
	}
}
WM_NOTIFY(Param*){
	Critical
	control:=
	if (this:=ListView.list[NumGet(Param.2)])&&(NumGet(Param.2,2*A_PtrSize,"int")=-12){
		stage:=NumGet(Param.2,3*A_PtrSize,"uint")
		if (stage=1)
			return 0x20 ;sets CDRF_NOTIFYITEMDRAW
		if (stage=0x10001){ ;NM_CUSTOMDRAW && Control is in the list
			index:=numget(Param.2,A_PtrSize=4?9*A_PtrSize:7*A_PtrSize,"uint")
			LV_GetText(text,index+1)
			info:=this.Control[text]
			if info.fore!=""
				NumPut(info.fore,Param.2,A_PtrSize=4?12*A_PtrSize:10*A_PtrSize,"int") ;sets the foreground
			if info.back!=""
				NumPut(info.back,Param.2,A_PtrSize=4?13*A_PtrSize:10.5*A_PtrSize,"int") ;sets the background
		}
	}
}

this will highlight by the second column.

 

Let me know if this is helpful to you.

The colors are based off of the first column of the listviews text value. to change that you can just change the two LV_GetText() values

LV_GetText(text,item,2)

and

LV_GetText(text,item+1,2)




szujeq
  • Members
  • 304 posts
  • Last active: Jan 12 2017 09:11 PM
  • Joined: 28 Mar 2011

you should put Critical into WM_NOTIFY() to reduce disappearing lv content on column resize. If it happen then only you can do is reload script ;p
However I didn't saw yet any method to prevent that effect in ahk, just lovely WinApi...



maestrith
  • Members
  • 786 posts
  • Last active: Apr 10 2019 01:28 PM
  • Joined: 17 Sep 2005

you should put Critical into WM_NOTIFY() to reduce disappearing lv content on column resize. If it happen then only you can do is reload script ;p
However I didn't saw yet any method to prevent that effect in ahk, just lovely WinApi...

Thanks for the tip :)