LV_SetSelColors() - user-defined selection colors for ListViews

Post your working scripts, libraries and tools for AHK v1.1 and older
m3user
Posts: 235
Joined: 17 Jan 2014, 18:11

Re: LV_SetSelColors() - user-defined selection colors for ListViews

Post by m3user » 04 Mar 2021, 02:59

Thanks, I put Critical after the Local definitions and it seems to work well.
Should I expect any potential issues?
Is Critical only valid for the function thread or I need to turn Critical off too?

think
Posts: 136
Joined: 09 Feb 2014, 05:20

Re: LV_SetSelColors() - user-defined selection colors for ListViews

Post by think » 16 Jan 2022, 09:30

In Windows 11 the ListView checkboxes are now colored blue (accent color) when enabled. Any idea if this could also be changed with the similar function? Thanks!

just me
Posts: 9424
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: LV_SetSelColors() - user-defined selection colors for ListViews

Post by just me » 17 Jan 2022, 09:37

I don't know such a function. You probably need to use own icons for the checkboxes.

think
Posts: 136
Joined: 09 Feb 2014, 05:20

Re: LV_SetSelColors() - user-defined selection colors for ListViews

Post by think » 17 Jan 2022, 17:09

Thanks for replying just me. The checkbox accent color is the same color as used for selected rows so I thought there might be the way.
Is there a working example for using own icons as checkboxes?

User avatar
KruschenZ
Posts: 44
Joined: 20 Jan 2021, 07:05
Location: Germany (Rheinhessen)
Contact:

Re: LV_SetSelColors() - user-defined selection colors for ListViews

Post by KruschenZ » 18 Jan 2022, 13:25

Hi,

maybe something like that:
viewtopic.php?p=311203#p311203

with best regards
KruschenZ

think
Posts: 136
Joined: 09 Feb 2014, 05:20

Re: LV_SetSelColors() - user-defined selection colors for ListViews

Post by think » 19 Jan 2022, 04:26

Thanks, very interesting. However I still don't understand how would be possible to use this functions to change the checkbox color. I see one alternative color, would it be possible to change it?

Alternatively I was thinking about changing the ListView theme or style but whichever style I apply it has no effect on ListView. Any idea?

User avatar
KruschenZ
Posts: 44
Joined: 20 Jan 2021, 07:05
Location: Germany (Rheinhessen)
Contact:

Re: LV_SetSelColors() - user-defined selection colors for ListViews

Post by KruschenZ » 19 Jan 2022, 14:01

It's not for change the color. Because this is not possible I think.
The idea was more like to set your own CheckBox icon with LV_SetImageList() in the post.

twiz
Posts: 11
Joined: 08 Nov 2013, 03:34

Re: LV_SetSelColors() - user-defined selection colors for ListViews

Post by twiz » 13 Jan 2024, 19:15

I've found that using LV_SetSelColors() causes column text(?), and any text in the columns it's maximum expanded size overlaps, to disappear (have not tested if it affects functionality) on resize/update, and on my full script it semi-'locks up' and prevents the window from closing outside of killing the task, but you can still interact with the GUI.

Example script, just click and drag either column back and forth at a moderately fast speed to resize. The resized column will disappear.

Code: Select all

#SingleInstance, Force			; Allow only one running instance, relaunches on new instance.
#NoEnv							; Recommended for performance and compatibility with future AutoHotkey releases.
#Warn 							; Enable warnings to assist with detecting common errors.

Gui, Add, ListView,	x10	y10 w280 r10 hwndHLV, Col1|Col2
LV_ModifyCol(1, 128)
LV_ModifyCol(2, 128)
LV_SetSelColors(HLV, 0xFFFF00)
Loop, 20
	LV_Add("", "Row " A_Index, "Row " A_Index)
Gui, Show, w300
Return

GuiClose:
ExitApp
Commenting out LV_SetSelColors(HLV, 0xFFFF00) will stop the issue from happening.

Larger script that semi-'locks up' (can't close through normal means):

Code: Select all

#SingleInstance, Force			; Allow only one running instance, relaunches on new instance.
#Persistent						; Keep the script 'alive' in the tray.
#NoEnv							; Recommended for performance and compatibility with future AutoHotkey releases.
;#Warn 							; Enable warnings to assist with detecting common errors.
SetWorkingDir, %A_ScriptDir%	; Ensures a consistent starting directory.
iniFile :=	A_ScriptDir "\" A_ScriptName ".ini"


/*		To Do:
	• BUG - Include Path not working
	• "Shortcut" variables, e.x.: ParentFolder -- %p% Done
	• Add Recursive folder functionality
	• Add RegEx Match Case setting functionality
	• Add File -> Open Folder functionality
	• Ctrl+F goes to Find, Ctrl+R goes to Replace?
	• Fix Checkbox alignment
	• Try/Catch errors

		Issues Found:
	• (LV_SetSelColors) Resizing/Updating GUI occasionally breaks everything
	• (LV_SetSelColors) Resizing column removes entries
*/


fSize	:=	9
pxGap	:=	10
SB_H	:=	25
Btn_W	:=	75
Edit_X	:=	Floor(fSize * 1.25 * 4.25) + pxGap * 2
CBox_W	:=	Floor(fSize * 1.25 * 7.25) + pxGap
;			Editbox		row * Font		Checkbox	row * Font		Button		row * Font		n * Gaps
LV_Y	:=	Floor(2.5 * 2 * fSize) +	Floor(1.2 * 2 * fSize) +	Floor(2.6 * 1 * fSize) +	6 * pxGap
LV_HPad	:=	LV_Y + SB_H + 8


Gui, +Resize +MinSize400x290 +hwndGuiID
Gui, Font, s%fSize%
;	Menu bar
Menu, FileMenu,	Add, &Open`tCtrl+O, OpenFolder
Menu, FileMenu,	Add
Menu, FileMenu,	Add, E&xit`tCtrl+X, FileClose
Menu, MenuBar,		Add, &File, :FileMenu
Menu, OptionsMenu,	Add, Save on Exit, ToggleSave
Menu, OptionsMenu,	Add, Remember Position, TogglePos
Menu, OptionsMenu,	Add
Menu, OptionsMenu,	Add, Save Options Now, WriteINI
Menu, OptionsMenu,	Add, Reload Options, ReadINI
Menu, OptionsMenu,	Add, Edit Options, EditINI
Menu, MenuBar,		Add, &Options, :OptionsMenu
Menu, HelpMenu,	Add, About`tF1, About
Menu, MenuBar,		Add, &Help, :HelpMenu
;	Tray menu
Menu, Tray, NoStandard
Menu, Tray, Add, Restore Window, GuiShow
Menu, Tray, Add, E&xit, GuiClose
Menu, Tray, Default, Restore Window
Menu, Tray, Click, 1


;	Gui Window
Gui, Add, Text,		x%pxGap%	y+%pxGap%											, Find:
Gui, Add, Edit,		x%Edit_X%	yp-4					vfText gTyping
Gui, Add, Text,		x%pxGap%	y+%pxGap%											, Replace:
Gui, Add, Edit,		x%Edit_X%	yp-4					vrText gTyping
Gui, Add, Checkbox,	x%pxGap%	y+10		w%CBox_W%	vAddFolders					, Add Folders
Gui, Add, Checkbox, x+%pxGap%	yp			w%CBox_W%	vRecursive					, Recursive
Gui, Add, Checkbox,	x+%pxGap%	yp			w%CBox_W%	vEditPath gEditPath	DISABLED, Incl. Path
Gui, Add, Checkbox, 			yp			w%CBox_W%	vToBatFile					, To BAT file
Gui, Add, Checkbox, x%pxGap%	y+10		w%CBox_W%	vUseRegEx					, Use RegEx
Gui, Add, Checkbox, x+%pxGap%	yp			w%CBox_W%	vMatchCase					, Match cASe
Gui, Add, Checkbox, 			yp			w%CBox_W%	vMakeUndo					, Make Undo
Gui, Add, Button,	x%pxGap%	y+%pxGap%	w%Btn_W%	gClear						, Clear
Gui, Add, Button,	x+%pxGap%	yp			w%Btn_W%	gPreview					, Preview
Gui, Add, Button,				yp			w%Btn_W%	gRename						, Rename

Gui, Font
Gui, Menu, MenuBar
Gui, Add, Statusbar,						h%SB_H%
Gui, Add, ListView,	x%pxGap%	y%LV_Y%		hwndHLV		vMyListView, Path|Original|Modified
LV_SetSelColors(HLV, 0xFFFF00)	; Causes problems on ListView resize/updates.

GoSub, ReadINI
GuiShow:
	Gui, Show, % (GuiX ? " x" GuiX : "") (GuiY ? " y" GuiY : "") (GuiW ? " w" GuiW : "") (GuiH ? " h" GuiH : "")
	If (GuiMaxed = 1)
		WinMaximize, AHK_id %GuiID%
Return


GuiClose:
FileClose:
	If SaveOnExit
		Gosub, WriteINI
ExitApp


GuiSize:
	If (A_EventInfo != 1) {  ; 1 = The window has been minimized. No action needed.
		GuiW := A_GuiWidth
		GuiH := A_GuiHeight
		SetTimer, GuiRedraw, -25
	}
Return

GuiRedraw:
	GuiControl, -Redraw, MyListView
	GuiControl, Move, fText,			% " w" GuiW - pxGap - Edit_X
	GuiControl, Move, rText,			% " w" GuiW - pxGap - Edit_X
	GuiControl, Move, ToBatFile,		% " x" GuiW - CBox_W
	GuiControl, Move, MakeUndo,		% " x" GuiW - CBox_W
	GuiControl, Move, MyListView,	% " w" GuiW - pxGap * 2 " h" GuiH - LV_HPad
	GuiControl, Move, Rename,		% " x" GuiW - (pxGap + Btn_W)
	ColW := Floor((GuiW - (pxGap * 4 +1)) / LV_GetCount("Col"))
	LV_ModifyCol(1, ColW)
	LV_ModifyCol(2, ColW)
	LV_ModifyCol(3, ColW)
	GuiControl, +Redraw, MyListView
	WinSet, Redraw
Return


OpenFolder:
	Gui +OwnDialogs
	FileSelectFolder, OutputDir, ::{20d04fe0-3aea-1069-a2d8-08002b30309d} *%A_Desktop%, 3
	If !ErrorLevel {
		Loop, Files, %OutputDir%\*, R
		{
			LV_Add("", A_LoopFileDir, A_LoopFileName)
		}
	}
Return

GuiDropFiles:
	Gui, Submit, NoHide
	Loop, Parse, A_GuiEvent, `n
	{
		If !AddFolders && InStr(FileExist(A_LoopField), "D") {
			Loop, Files, %A_LoopField%\*, R
			{
				LV_Add("", A_LoopFileDir "\", A_LoopFileName)
			}
		} Else {
			x := InStr(A_LoopField, "\",, 0)
			p := SubStr(A_LoopField, 1, x)
			n := SubStr(A_LoopField, x+1)
			Lv_Add("", p, n)
		}
	}
	Gosub, Preview
Return


Clear:
	LV_Delete()
Return


Typing:
	Gui, Submit, NoHide
	Loop, % LV_GetCount() {
		m := 0
		LV_GetText(fOrig, A_Index, LV_GetCount("Col") - 1)	; Dupe line
		If InStr(fOrig, fText) && fText != "" {
			m := 1
		}
		LV_Modify(A_Index, "Select" m)
	}
	Gosub, Preview
Return


Preview:
	Gui, Submit, NoHide
	GuiControl, -Redraw, MyListView
	Loop, % LV_GetCount() {
		If InStr(rText, "%p%", False) {
			LV_GetText(fPath, A_Index, 1)
			_Parent	:= RegExReplace(fPath, ".*\\(.+?)(\\$|$)", "$1")
			newText	:= StrReplace(rText, "%p%", _Parent)
		} Else {
			newText := rText
		}
		LV_GetText(fOrig, A_Index, LV_GetCount("Col") - 1)
		If UseRegEx {
			newText := RegExReplace(fOrig, fText, newText)
		} Else {
			newText := StrReplace(fOrig, fText, newText)
		}
		newText := RegExReplace(newText, "[\\\/:*?""<>|¦]", "_")
		LV_Modify(A_Index,,,, newText)
	}
	GuiControl, +Redraw, MyListView
Return


EditPath:
	Gui, Submit, NoHide
	WinGetClientPos(,,, GuiW)
	GuiControl, -Redraw, MyListView 
	If EditPath {
		Loop, % LV_GetCount() {
			LV_GetText(fPath, A_Index, 1)
			LV_GetText(fOrig, A_Index, 2)
			LV_Modify(A_Index,, fPath "\" fOrig, fPath "\" fOrig)
		}
		ColW := Floor((GuiW - (pxGap * 4 +1)) / 2)
		LV_ModifyCol(1, Floor(ColW), "Original")
		LV_ModifyCol(2, Floor(ColW), "Modified")
		LV_DeleteCol(3)
	} Else {
		ColW := Floor((GuiW - (pxGap * 4 +1)) / 3)
		LV_ModifyCol(1, Floor(ColW), "Path")
		LV_ModifyCol(2, Floor(ColW), "Original")
		LV_InsertCol(3, Floor(ColW), "Modified")
		Loop, % LV_GetCount() {
			LV_GetText(fOrig, A_Index, 1)
			n		:= InStr(fOrig, "\",, 0)
			fPath	:= SubStr(fOrig, 1, n)
			fOrig	:= SubStr(fOrig, n+1)
			LV_Modify(A_Index,, fPath, fOrig, fOrig)
		}
	}
	GuiControl, +Redraw, MyListView 
Return


Rename:
	bat_Ren := ""
	bat_Undo := ""
	Gosub, Preview
	Loop, % LV_GetCount() {
		LV_GetText(fPath, A_Index, LV_GetCount("Col") - 2)
		LV_GetText(fOrig, A_Index, LV_GetCount("Col") - 1)
		LV_GetText(fNew, A_Index, LV_GetCount("Col"))
		pOrig	:= fPath fOrig
		pNew	:= fPath fNew

		If (pOrig != pNew) {
			If ToBatFile {
				bat_Ren		.= "move """ pOrig """ """ pNew """`n"
			} Else {
				FileGetAttrib, Attrib, %pOrig%
				If InStr(Attrib, "D")
					FileMoveDir, %pOrig%, %pNew%
				Else
					FileMove, %pOrig%, %pNew%
				If ErrorLevel
					MsgBox, % "Error moving """ fOrig """.`nError Code:`t" ErrorLevel "`n`n" pOrig "`n" pNew
				;Else
					;LV_Modify(A_Index,,, fNew)
			}
			If MakeUndo
				bat_Undo	.= "move """ pNew """ """ pOrig """`n"
		}
	}
	If ToBatFile OR MakeUndo {
		Gui +OwnDialogs
		FileSelectFolder, OutputDir, ::{20d04fe0-3aea-1069-a2d8-08002b30309d} *%A_Desktop%, 3
		If (OutputDir != "") {
			If ToBatFile && bat_Ren
				FileAppend, %bat_Ren%, %OutputDir%\Rename_%A_Now%.bat
			If MakeUndo && bat_Undo
				FileAppend, %bat_Undo%, %OutputDir%\Undo_%A_Now%.bat
		}
	}
	Gosub, Typing
Return


ToggleSave:
	Menu, OptionsMenu, ToggleCheck, Save on Exit
	SaveOnExit := IsMenuItemChecked(2,1, GuiID)
	IniWrite, %SaveOnExit%,	%iniFile%, Options, SaveOnExit
Return

TogglePos:
	Menu, OptionsMenu, ToggleCheck, Remember Position
	RememberPos := IsMenuItemChecked(2,2, GuiID)
Return

About:
	Gui +OwnDialogs
	MsgBox,, About %A_ScriptName%, Blah Blah Blah.
Return


ReadINI:
	IniRead, AddFolders	, %iniFile%, Options, AddFolders, 0
	IniRead, Recursive	, %iniFile%, Options, Recursive, 0
	IniRead, ToBatFile	, %iniFile%, Options, ToBatFile, 0
	IniRead, MakeUndo	, %iniFile%, Options, MakeUndo,	0
	IniRead, UseRegEx	, %iniFile%, Options, UseRegEx, 0
	IniRead, MatchCase	, %iniFile%, Options, MatchCase, 0
	IniRead, SaveOnExit	, %iniFile%, Options, SaveOnExit, 0
	IniRead, RememberPos	, %iniFile%, Options, RememberPos, 0
	IniRead, GuiMaxed	, %iniFile%, GUIPos	, GuiMaxed, 0
	IniRead, GuiX		, %iniFile%, GUIPos	, GuiX, %A_Space%
	IniRead, GuiY		, %iniFile%, GUIPos	, GuiY, %A_Space%
	IniRead, GuiW		, %iniFile%, GUIPos	, GuiW, 640
	IniRead, GuiH		, %iniFile%, GUIPos	, GuiH, 360

	GuiControl,, AddFolders,	%AddFolders%
	GuiControl,, Recursive,	%Recursive%
	GuiControl,, ToBatFile,	%ToBatFile%
	GuiControl,, MakeUndo,	%MakeUndo%
	GuiControl,, UseRegEx,	%UseRegEx%
	GuiControl,, MatchCase,	%MatchCase%
	Menu, OptionsMenu, % SaveOnExit ? "Check":"Uncheck", Save on Exit
	Menu, OptionsMenu, % RememberPos ? "Check":"Uncheck", Remember Position
Return

WriteINI:
	Gui, Submit, NoHide
	WinGet, GuiMaxed, MinMax, AHK_id %GuiID%
	IniWrite, %AddFolders%	, %iniFile%, Options, AddFolders
	IniWrite, %Recursive%		, %iniFile%, Options, Recursive
	IniWrite, %ToBatFile%		, %iniFile%, Options, ToBatFile
	IniWrite, %MakeUndo%		, %iniFile%, Options, MakeUndo
	IniWrite, %UseRegEx%		, %iniFile%, Options, UseRegEx
	IniWrite, %MatchCase%		, %iniFile%, Options, MatchCase
	IniWrite, %SaveOnExit%	, %iniFile%, Options, SaveOnExit
	IniWrite, %RememberPos%	, %iniFile%, Options, RememberPos
	IniWrite, %GuiMaxed%		, %iniFile%, GUIPos	, GuiMaxed

	If RememberPos && !GuiMaxed {
		WinGetPos(, GuiPosX, GuiPosY)
		WinGetClientPos(,,, GuiPosW, GuiPosH)
		IniWrite, % GuiPosX	, %iniFile%, GUIPos, GuiX
		IniWrite, % GuiPosY	, %iniFile%, GUIPos, GuiY
		IniWrite, % GuiPosW	, %iniFile%, GUIPos, GuiW
		IniWrite, % GuiPosH	, %iniFile%, GUIPos, GuiH
	}
Return

EditINI:
	Run, %iniFile%
Return


IsMenuItemChecked( MenuPos, SubMenuPos, hWnd, MFS=0x8, state=1 ) {
/*	Original version by By Obi / Lexikos: (dead) https://www.autohotkey.com/forum/viewtopic.php?p=203606#203606
	Altered by SKAN: https://www.autohotkey.com/board/topic/73372-guimenu-getting-togglecheck-state/
	64bit fix from teadrinker: https://www.autohotkey.com/boards/viewtopic.php?t=41307#p188225
	Index 1
*/
	hMenu		:= DllCall("GetMenu", UInt,hWnd)
	hSubMenu	:= DllCall("GetSubMenu", UInt,hMenu, Int,MenuPos-1)
	VarSetCapacity(mii, size := 16 + A_PtrSize*8, 0)
	NumPut(size, mii), NumPut(state, mii, 4, "UInt")
	DllCall("GetMenuItemInfo", UInt,hSubMenu, UInt,SubMenuPos-1, Int,1, UInt,&mii)
	Return (NumGet(mii, 12) & MFS) ? 1 : 0
}

; Based on WinGetClientPos by dd900 and Frosti - https://www.autohotkey.com/boards/viewtopic.php?t=484
WinGetClientPos( HWND:="", ByRef X:="", ByRef Y:="", ByRef W:="", ByRef H:="" ) {
	HWND := HWND ? HWND : WinExist("A")
	Static DPI
	If !DPI {
		RegRead, DPI, HKEY_CURRENT_USER, Control Panel\Desktop\WindowMetrics, AppliedDPI
		; "ErrorLevel = 1" means the Registry key was not found
		; and the system is using the default DPI setting of 96.
		DPI := (ErrorLevel = 1 ? 96 : DPI) / 96
	}

	VarSetCapacity( size, 16, 0 )
	DllCall( "GetClientRect", UInt, HWND, Ptr, &size )
	x := Round(NumGet( size, 0,  "Int" ))
	y := Round(NumGet( size, 4,  "Int" ))
	w := Round(NumGet( size, 8,  "Int" ) // DPI)
	h := Round(NumGet( size, 12, "Int" ) // DPI)
	Return { X: x, Y: y, W: w, H: h }
}

; Based on WinGetClientPos by dd900 and Frosti - https://www.autohotkey.com/boards/viewtopic.php?t=484
WinGetPos( HWND:="", ByRef X:="", ByRef Y:="", ByRef W:="", ByRef H:="" ) {
	HWND := HWND ? HWND : WinExist("A")
	Static DPI
	If !DPI {
		RegRead, DPI, HKEY_CURRENT_USER, Control Panel\Desktop\WindowMetrics, AppliedDPI
		; "ErrorLevel = 1" means the Registry key was not found
		; and the system is using the default DPI setting of 96.
		DPI := (ErrorLevel = 1 ? 96 : DPI) / 96
	}
	;px := (SubStr(A_OSVersion,1,2) = 10 ? 7 : 0)	; Windows 10 'actual' vs shown border size
	VarSetCapacity( size, 16, 0 )
	DllCall( "GetWindowRect", UInt, HWND, Ptr, &size )
	x := Round(NumGet( size, 0,  "Int" )) ;+px
	y := Round(NumGet( size, 4,  "Int" ))	
	w := Round(NumGet( size, 8,  "Int" ) -x // DPI) ;-px
	h := Round(NumGet( size, 12, "Int" ) -y // DPI) ;-px
	Return { X: x, Y: y, W: w, H: h }
}


just me
Posts: 9424
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: LV_SetSelColors() - user-defined selection colors for ListViews

Post by just me » 14 Jan 2024, 05:30

It's a known issue with ListView custom drawing and AHK v1. It's eliminated as far as possible in AHK v2.

twiz
Posts: 11
Joined: 08 Nov 2013, 03:34

Re: LV_SetSelColors() - user-defined selection colors for ListViews

Post by twiz » 14 Jan 2024, 12:15

just me wrote:
14 Jan 2024, 05:30
It's a known issue with ListView custom drawing and AHK v1. It's eliminated as far as possible in AHK v2.
Ah, ok. Thanks... Might have to make the jump then.


Post Reply

Return to “Scripts and Functions (v1)”