GuiControl set color using variable Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
Ahmad-f
Posts: 9
Joined: 17 Nov 2020, 14:46

GuiControl set color using variable

17 Nov 2020, 15:09

Hi
I use these lines to get windows accent color

Code: Select all

RegRead, Color, HKCU\SOFTWARE\Microsoft\Windows\DWM, ColorizationColor
Accent := Format("{:X}", Color)
Accent := SubStr(Accent, 2)
and i want to change progress bar's color

Code: Select all

if Mute
	GuiControl, +cGray, VolSlider
else
	GuiControl, +c, VolSlider, %Accent%
I tried

Code: Select all

GuiControl, +c%Accent%, VolSlider
but it didn't work too

how can i set the color code inside variable?
User avatar
boiler
Posts: 6653
Joined: 21 Dec 2014, 02:44

Re: GuiControl set color using variable

17 Nov 2020, 15:15

Your accent color variable contains one too many characters. If the first byte (first two characters) is the alpha channel (transparency level), then you need to remove the first two characters:

Code: Select all

Accent := SubStr(Accent, 3)
That should work. Here's a demo:

Code: Select all

RegRead, Color, HKCU\SOFTWARE\Microsoft\Windows\DWM, ColorizationColor
Accent := Format("{:X}", Color)
Accent := SubStr(Accent, 3)

Gui, Add, Progress, w200 h20 cBlue vVolSlider, 75
Gui, Show
Sleep, 3000
GuiControl, +c%Accent%, VolSlider
Sleep, 3000
ExitApp
Ahmad-f
Posts: 9
Joined: 17 Nov 2020, 14:46

Re: GuiControl set color using variable

17 Nov 2020, 15:48

thanks

In my case

Code: Select all

RegRead, Accent, HKCU\SOFTWARE\Microsoft\Windows\DWM, ColorizationColor
Accent := Format("{:X}", Accent)
gives Accent C4744DA9 value so i just removed first character and since +c4744DA9 worked I didn't notice that there is an extra

I still cant make it work so here is the full script

Code: Select all

#SingleInstance force
#NoEnv
SetBatchLines, -1
#MaxHotkeysPerInterval 199
#include va.ahk

RegRead, Accent, HKCU\SOFTWARE\Microsoft\Windows\DWM, ColorizationColor
Accent := Format("{:X}", Accent)
Accent := SubStr(Accent, 3)

; Create the slider window
Gui, Font, s13
Gui, Add, Progress, w29 h137 x0 y0 Range0-100 vVolSlider Vertical, 0
Gui, Add, Text, w29 h43 x0 y89 vVolText BackgroundTrans Center, 0
Gui, +AlwaysOnTop -Caption +ToolWindow +Border
Gui, Show, Hide w29 h137 x0 y0, Volume
return

; --- Volume Keybinds ---

*#Volume_Up::
*#Volume_Down::
; Calculate step size
amount := GetKeyState("Ctrl", "P") ? 100 : GetKeyState("Alt", "P") ? 10 : 2
if (A_ThisHotkey == "*#Volume_Down")
	amount *= -1
; Adjust volume of the appropriate target
	WinGet, ProcessName, ProcessName, A
	AppVolume(ProcessName).AdjustVolume(amount)

SetTimer, UpdateSlider, -0 ; Asynchronous function call
return

*#Volume_Mute::
; Mute the appropriate target
WinGet, ProcessName, ProcessName, A
AppVolume(ProcessName).ToggleMute()

SetTimer, UpdateSlider, -0 ; Asynchronous function call
return


; --- Slider Window Functions ---

UpdateSlider()
{
	; Application volume level
	WinGet, ProcessName, ProcessName, A
	AV := AppVolume(ProcessName)
	Volume := Round(AV.GetVolume())
	Mute := AV.GetMute() ? "x" : ""
	GetWindowRect(WinExist("A"), x1, y1, x2, y2)
	x := x1 + 67, y := y1 + 61

	; Update and show the window
	if Mute
		GuiControl, +cGray, VolSlider
	else
		GuiControl, +c%Accent%, VolSlider
	GuiControl,, VolSlider, %Volume%
	GuiControl,, VolText, %Mute%`n%Volume%
	Gui, Show, NoActivate x%x% y%y%

	; Dismiss after a while
	SetTimer, ClearTip, -1031
}

ClearTip()
{
	Gui, Show, Hide
}

; W10 compatible function to find a window's visible boundaries
GetWindowRect(hWnd, ByRef x1, ByRef y1, ByRef x2, ByRef y2)
{
	size := VarSetCapacity(rect, 16, 0)
	error := DllCall("dwmapi\DwmGetWindowAttribute"
	, "UPtr", hWnd  ; HWND  hwnd
	, "UInt", 9     ; DWORD dwAttribute (DWMWA_EXTENDED_FRAME_BOUNDS)
	, "UPtr", &rect ; PVOID pvAttribute
	, "UInt", size  ; DWORD cbAttribute
	, "UInt")       ; HRESULT
	if (error)
		DllCall("GetWindowRect", "UPtr", WinExist("A"), "UPtr", &rect, "UInt")

	x1 := NumGet(rect, 0, "Int"), y1 := NumGet(rect, 4, "Int")
	x2 := NumGet(rect, 8, "Int"), y2 := NumGet(rect, 12, "Int")
}


; --- AppVolume Library ---

AppVolume(app:="", device:="")
{
	return new AppVolume(app, device)
}

class AppVolume
{
	ISAVs := []

	__New(app:="", device:="")
	{
		static IID_IASM2 := "{77AA99A0-1BD6-484F-8BC7-2C654C9A9B6F}"
		, IID_IASC2 := "{BFB7FF88-7239-4FC9-8FA2-07C950BE9C6D}"
		, IID_ISAV := "{87CE5498-68D6-44E5-9215-6DA47EF883D8}"

		; Activate the session manager of the given device
		pIMMD := VA_GetDevice(device)
		VA_IMMDevice_Activate(pIMMD, IID_IASM2, 0, 0, pIASM2)
		ObjRelease(pIMMD)

		; Enumerate sessions for on this device
		VA_IAudioSessionManager2_GetSessionEnumerator(pIASM2, pIASE)
		ObjRelease(pIASM2)

		; Search for audio sessions with a matching process ID or Name
		VA_IAudioSessionEnumerator_GetCount(pIASE, Count)
		Loop, % Count
		{
			; Get this session's IAudioSessionControl2 via its IAudioSessionControl
			VA_IAudioSessionEnumerator_GetSession(pIASE, A_Index-1, pIASC)
			pIASC2 := ComObjQuery(pIASC, IID_IASC2)
			ObjRelease(pIASC)

			; If its PID matches save its ISimpleAudioVolume pointer
			VA_IAudioSessionControl2_GetProcessID(pIASC2, PID)
			if (PID == app || this.GetProcessName(PID) == app)
				this.ISAVs.Push(ComObjQuery(pIASC2, IID_ISAV))

			ObjRelease(pIASC2)
		}

		; Release the IAudioSessionEnumerator
		ObjRelease(pIASE)
	}

	__Delete()
	{
		for k, pISAV in this.ISAVs
			ObjRelease(pISAV)
	}

	AdjustVolume(Amount)
	{
		return this.SetVolume(this.GetVolume() + Amount)
	}

	GetVolume()
	{
		for k, pISAV in this.ISAVs
		{
			VA_ISimpleAudioVolume_GetMasterVolume(pISAV, fLevel)
			return fLevel * 100
		}
	}

	SetVolume(level)
	{
		level := level>100 ? 100 : level<0 ? 0 : level ; Limit to range 0-100
		for k, pISAV in this.ISAVs
			VA_ISimpleAudioVolume_SetMasterVolume(pISAV, level / 100)
		return level
	}

	GetMute()
	{
		for k, pISAV in this.ISAVs
		{
			VA_ISimpleAudioVolume_GetMute(pISAV, bMute)
			return bMute
		}
	}

	SetMute(bMute)
	{
		for k, pISAV in this.ISAVs
			VA_ISimpleAudioVolume_SetMute(pISAV, bMute)
		return bMute
	}

	ToggleMute()
	{
		return this.SetMute(!this.GetMute())
	}

	GetProcessName(PID) {
		hProcess := DllCall("OpenProcess"
		, "UInt", 0x1000 ; DWORD dwDesiredAccess (PROCESS_QUERY_LIMITED_INFORMATION)
		, "UInt", False  ; BOOL  bInheritHandle
		, "UInt", PID    ; DWORD dwProcessId
		, "UPtr")
		dwSize := VarSetCapacity(strExeName, 512 * A_IsUnicode, 0) // A_IsUnicode
		DllCall("QueryFullProcessImageName"
		, "UPtr", hProcess  ; HANDLE hProcess
		, "UInt", 0         ; DWORD  dwFlags
		, "Str", strExeName ; LPSTR  lpExeName
		, "UInt*", dwSize   ; PDWORD lpdwSize
		, "UInt")
		DllCall("CloseHandle", "UPtr", hProcess, "UInt")
		SplitPath, strExeName, strExeName
		return strExeName
	}
}


; --- Vista Audio Additions ---

;
; ISimpleAudioVolume : {87CE5498-68D6-44E5-9215-6DA47EF883D8}
;
VA_ISimpleAudioVolume_SetMasterVolume(this, ByRef fLevel, GuidEventContext="") {
	return DllCall(NumGet(NumGet(this+0)+3*A_PtrSize), "ptr", this, "float", fLevel, "ptr", VA_GUID(GuidEventContext))
}
VA_ISimpleAudioVolume_GetMasterVolume(this, ByRef fLevel) {
	return DllCall(NumGet(NumGet(this+0)+4*A_PtrSize), "ptr", this, "float*", fLevel)
}
VA_ISimpleAudioVolume_SetMute(this, ByRef Muted, GuidEventContext="") {
	return DllCall(NumGet(NumGet(this+0)+5*A_PtrSize), "ptr", this, "int", Muted, "ptr", VA_GUID(GuidEventContext))
}
VA_ISimpleAudioVolume_GetMute(this, ByRef Muted) {
	return DllCall(NumGet(NumGet(this+0)+6*A_PtrSize), "ptr", this, "int*", Muted)
}
Source: https://gist.github.com/G33kDude/5b7ba418e685e52c3e6507e5c6972959
User avatar
boiler
Posts: 6653
Joined: 21 Dec 2014, 02:44

Re: GuiControl set color using variable  Topic is solved

17 Nov 2020, 16:04

You are trying to access the variable Accent outside of its scope (inside a function). You can put global Accent at the top of that function so it can access it. To demonstrate, try the following with and without the global Accent line:

Code: Select all

RegRead, Accent, HKCU\SOFTWARE\Microsoft\Windows\DWM, ColorizationColor
Accent := Format("{:X}", Accent)
Accent := SubStr(Accent, 3)

; Create the slider window
Gui, Font, s13
Gui, Add, Progress, w29 h137 x0 y0 Range0-100 vVolSlider Vertical, 100
Gui, Add, Text, w29 h43 x0 y89 vVolText BackgroundTrans Center, 0
Gui, +AlwaysOnTop -Caption +ToolWindow +Border
Gui, Show, w29 h137 x0 y0, Volume

Sleep, 3000
UpdateSlider()
return

UpdateSlider()
{
	global Accent
	GuiControl, +c%Accent%, VolSlider
}

Return to “Ask For Help”

Who is online

Users browsing this forum: AHKStudent, Bing [Bot], brunohimalaya, JackbobEvolved, proactivematter, TAC109 and 46 guests