Dav1d Posted September 15, 2010 Share Posted September 15, 2010 (edited) Tested to work under Win7. Should work with Vista as well.Usage:WinSetVolume("title", 50) - Set volume (0-100)WinSetVolume("title", "mute") - MuteWinSetVolume("title", "unmute") - UnmuteWinSetVolume("title", "toggle") - Toggles mute stateReturns 1 on success, 0 on failure.Please note:For non-English platforms you'll need to alter the $localized string (e.g. for Dutch: $localized = "Dempen voor ").Setting the volume to 0 is not the same as muting.This function doesn't respect the WinTitleMatchMode setting, it will always match titles as if in mode 1.On how it functions:The keypress after setting the slider's position is needed to trigger Windows to adjust the volume, otherwise it won't become effective. Just FYI, if you wondered.Since there's no easy way to determine whether or not a given window is muted, the volume level is being re-set which results in unmuting (though it possibly already was), so we're certain of it's state. Afterwards it 'presses' the mute button, if applicable.Any questions, bug reports or ideas are welcome. I appreciate if you let me know if this was of use to you.expandcollapse popup#include <GuiConstantsEx.au3> #include <GuiSlider.au3> Func WinSetVolume($targetTitle, $targetVolume = "toggle") Const $localized = "Mute for " $currentActive = WinGetHandle("[active]") $mixerPid = Run(@SystemDir & "\SndVol.exe -r", "", @SW_HIDE) $mixerHandle = WinWaitActive("[CLASS:#32770]") WinActivate($currentActive) $iSlider = 1 $iButton = 2 While 1 $currentButton = ControlGetText("[CLASS:#32770]", "", "[CLASS:ToolbarWindow32; INSTANCE:" & $iButton & "]") If @error Then ProcessClose($mixerPid) Return 0 ElseIf StringInStr($currentButton, $localized & $targetTitle, 1) = 1 Then If NOT ($targetVolume == "toggle") Then $sliderHandle = ControlGetHandle("[CLASS:#32770]", "", "[CLASS:msctls_trackbar32; INSTANCE:" & $iSlider & "]") If IsInt($targetVolume) Then $setVolume = -($targetVolume-100) Else $setVolume = _GUICtrlSlider_GetPos($sliderHandle) EndIf If $setVolume < 100 Then _GUICtrlSlider_SetPos($sliderHandle, $setVolume+1) ControlSend("[CLASS:#32770]", "", "[CLASS:msctls_trackbar32; INSTANCE:" & $iSlider & "]", "{UP}") Else _GUICtrlSlider_SetPos($sliderHandle, $setVolume-1) ControlSend("[CLASS:#32770]", "", "[CLASS:msctls_trackbar32; INSTANCE:" & $iSlider & "]", "{DOWN}") EndIf EndIf If $targetVolume == "toggle" OR $targetVolume == "mute" Then ControlCommand("[CLASS:#32770]", "", "[CLASS:ToolbarWindow32; INSTANCE:" & $iButton & "]", "SendCommandID", 305) WinClose($mixerHandle) Return 1 EndIf $iSlider += 1 $iButton += 2 WEnd EndFunc Edited September 16, 2010 by Dav1d yutijang and tcurran 1 1 Link to comment Share on other sites More sharing options...
ludocus Posted September 15, 2010 Share Posted September 15, 2010 Wow! Looks really useful! I'll use this some time! Link to comment Share on other sites More sharing options...
Ascend4nt Posted September 21, 2010 Share Posted September 21, 2010 Very cool find. I didn't know you could set the volume for apps individually. This actually helps with my Soundblaster X-Fi's screwed up Dolby Digital and DTS encoding (for some reason normal sound is extremely overdriven, and everything needs to be turned way down in volume). My contributions: Performance Counters in Windows - Measure CPU, Disk, Network etc Performance | Network Interface Info, Statistics, and Traffic | CPU Multi-Processor Usage w/o Performance Counters | Disk and Device Read/Write Statistics | Atom Table Functions | Process, Thread, & DLL Functions UDFs |Â Process CPU Usage Trackers | PE File Overlay Extraction | A3X Script Extract | File + Process Imports/Exports Information | Windows Desktop Dimmer Shade | Spotlight + Focus GUI - Highlight and Dim for Eyestrain Relief | CrossHairs (FullScreen) |Â Rubber-Band Boxes using GUI's (_GUIBox) | GUI Fun! | IE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) | Magnifier (Vista+) Functions UDF | _DLLStructDisplay (Debug!) | _EnumChildWindows (controls etc) | _FileFindEx | _ClipGetHTML | _ClipPutHTML + ClipPutHyperlink | _FileGetShortcutEx | _FilePropertiesDialog | I/O Port Functions | File(s) Drag & Drop | _RunWithReducedPrivileges | _ShellExecuteWithReducedPrivileges | _WinAPI_GetSystemInfo | dotNETGetVersions | Drive(s) Power Status | _WinGetDesktopHandle | _StringParseParameters | Screensaver, Sleep, Desktop Lock Disable | Full-Screen Crash Recovery Wrappers/Modifications of others' contributions: _DOSWildcardsToPCRegEx (original code: RobSaunder's) | WinGetAltTabWinList (original: Authenticity) UDF's added support/programming to: _ExplorerWinGetSelectedItems | MIDIEx UDF (original code: eynstyne) (All personal code/wrappers centrally located at Ascend4nt's AutoIT Code) Link to comment Share on other sites More sharing options...
tcurran Posted November 6, 2014 Share Posted November 6, 2014 Very useful... exactly what I needed. I did notice one slight problem. If there's no match for $targetTitle, the function will loop infinitely. There's an easy fix, of course. I just added If $currentButton = "" Then Return 0 below $currentButton = ControlGetText("[CLASS:#32770]", "", "[CLASS:ToolbarWindow32; INSTANCE:" & $iButton & "]") and it's good to go. Thanks for the UDF! Link to comment Share on other sites More sharing options...
tcurran Posted November 7, 2014 Share Posted November 7, 2014 (edited) OK, that wasn't quite right. I forgot to shut down the instance of SndVol.exe Here's the UDF, corrected and complete. Again, thanks! TAC expandcollapse popup#include <GuiConstantsEx.au3> #include <GuiSlider.au3> Func WinSetVolume($targetTitle, $targetVolume = "toggle") Const $localized = "Mute for " $currentActive = WinGetHandle("[active]") $mixerPid = Run(@SystemDir & "\SndVol.exe -r", "", @SW_HIDE) $mixerHandle = WinWaitActive("[CLASS:#32770]") WinActivate($currentActive) $iSlider = 1 $iButton = 2 While 1 $currentButton = ControlGetText("[CLASS:#32770]", "", "[CLASS:ToolbarWindow32; INSTANCE:" & $iButton & "]") If @error Then ProcessClose($mixerPid) Return 0 ElseIf $currentButton = "" Then ;this ElseIf prevents infinite loop if no matching $targetTitle WinClose($mixerHandle) Return 0 ElseIf StringInStr($currentButton, $localized & $targetTitle, 1) = 1 Then If NOT ($targetVolume == "toggle") Then $sliderHandle = ControlGetHandle("[CLASS:#32770]", "", "[CLASS:msctls_trackbar32; INSTANCE:" & $iSlider & "]") If IsInt($targetVolume) Then $setVolume = -($targetVolume-100) Else $setVolume = _GUICtrlSlider_GetPos($sliderHandle) EndIf If $setVolume < 100 Then _GUICtrlSlider_SetPos($sliderHandle, $setVolume+1) ControlSend("[CLASS:#32770]", "", "[CLASS:msctls_trackbar32; INSTANCE:" & $iSlider & "]", "{UP}") Else _GUICtrlSlider_SetPos($sliderHandle, $setVolume-1) ControlSend("[CLASS:#32770]", "", "[CLASS:msctls_trackbar32; INSTANCE:" & $iSlider & "]", "{DOWN}") EndIf EndIf If $targetVolume == "toggle" OR $targetVolume == "mute" Then ControlCommand("[CLASS:#32770]", "", "[CLASS:ToolbarWindow32; INSTANCE:" & $iButton & "]", "SendCommandID", 305) WinClose($mixerHandle) Return 1 EndIf $iSlider += 1 $iButton += 2 WEnd EndFunc Edited November 7, 2014 by tcurran Link to comment Share on other sites More sharing options...
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