A subroutine to switch to the next background (Windows 10)

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
Maestr0
Posts: 136
Joined: 05 Dec 2013, 17:43

A subroutine to switch to the next background (Windows 10)

13 Jul 2017, 14:32

I found myself wanting to use a hotkey to change to the next wallpaper in my cycle.
I found this: https://superuser.com/questions/71860/i ... background

but it was doing some mouse clicking I didn't like, so I edited it a bit, it turned into this, works pretty well so far.

Code: Select all

	hotkey, #n, sh_send_nextbackground
return

sh_send_nextbackground:
	; https://superuser.com/questions/71860/is-there-a-windows-7-keyboard-shortcut-to-change-the-desktop-background
	WinActivate, ahk_class Progman   ; activate the Desktop
	Send #D							; hide all windows
	Click							; make sure the desktop has focus
	Send +{F10}                      ; send Shift+F10, the shortcut for right-click
	Send n                           ; send "n", the key for "next desktop background"
	Send #D							; show all windows that were showing before
return                           ; done!
qwerty12
Posts: 468
Joined: 04 Mar 2016, 04:33
Contact:

Re: A subroutine to switch to the next background (Windows 10)

16 Jul 2017, 12:45

For what it's worth, since Windows 8, you can advance to the next wallpaper without any window interaction at all:

Code: Select all

try if ((pDesktopWallpaper := ComObjCreate("{C2CF3110-460E-4fc1-B9D0-8A1C0C9CC4BD}", "{B92B56A9-8B55-4E14-9A89-0199BBB6F93B}"))) {
	DllCall(NumGet(NumGet(pDesktopWallpaper+0)+16*A_PtrSize), "Ptr", pDesktopWallpaper, "Ptr", 0, "UInt", 0) ; IDesktopWallpaper::AdvanceSlideshow - https://msdn.microsoft.com/en-us/library/windows/desktop/hh706947(v=vs.85).aspx
	ObjRelease(pDesktopWallpaper)
}
MrMxNemesis

Re: A subroutine to switch to the next background (Windows 10)

03 Oct 2018, 11:29

qwerty12 wrote:For what it's worth, since Windows 8, you can advance to the next wallpaper without any window interaction at all:

Code: Select all

try if ((pDesktopWallpaper := ComObjCreate("{C2CF3110-460E-4fc1-B9D0-8A1C0C9CC4BD}", "{B92B56A9-8B55-4E14-9A89-0199BBB6F93B}"))) {
	DllCall(NumGet(NumGet(pDesktopWallpaper+0)+16*A_PtrSize), "Ptr", pDesktopWallpaper, "Ptr", 0, "UInt", 0) ; IDesktopWallpaper::AdvanceSlideshow - https://msdn.microsoft.com/en-us/library/windows/desktop/hh706947(v=vs.85).aspx
	ObjRelease(pDesktopWallpaper)
}
Man, I know this post has been here for a while but I just found it and works like a charm! Just what I needed it. I'm still trying to understand every piece of your code tho. Thanks a lot.
User avatar
Maestr0
Posts: 136
Joined: 05 Dec 2013, 17:43

Re: A subroutine to switch to the next background (Windows 10)

07 Oct 2018, 07:44

MrMxNemesis wrote:
qwerty12 wrote:For what it's worth, since Windows 8, you can advance to the next wallpaper without any window interaction at all:

Code: Select all

try if ((pDesktopWallpaper := ComObjCreate("{C2CF3110-460E-4fc1-B9D0-8A1C0C9CC4BD}", "{B92B56A9-8B55-4E14-9A89-0199BBB6F93B}"))) {
	DllCall(NumGet(NumGet(pDesktopWallpaper+0)+16*A_PtrSize), "Ptr", pDesktopWallpaper, "Ptr", 0, "UInt", 0) ; IDesktopWallpaper::AdvanceSlideshow - https://msdn.microsoft.com/en-us/library/windows/desktop/hh706947(v=vs.85).aspx
	ObjRelease(pDesktopWallpaper)
}
Man, I know this post has been here for a while but I just found it and works like a charm! Just what I needed it. I'm still trying to understand every piece of your code tho. Thanks a lot.
I'll second that!

As for how to understand it, it's COM so a bit hard to read.
First he creates a COM object named pDesktopWallpaper with CLSID {C2CF3110-460E-4fc1-B9D0-8A1C0C9CC4BD} and IID {B92B56A9-8B55-4E14-9A89-0199BBB6F93B}".
He did not come up with those ID's, they're in a list from Microsoft ;)
After that, he sends a DllCall which - if I understand it correctly - sends the message to the object to go to the next background.
And lastly, he releases the pDesktopWallpaper object from memory.
User avatar
Thoughtfu1Tux
Posts: 125
Joined: 31 May 2018, 23:26

Re: A subroutine to switch to the next background (Windows 10)

08 Oct 2018, 07:02

Maestr0 wrote:
MrMxNemesis wrote:
qwerty12 wrote:For what it's worth, since Windows 8, you can advance to the next wallpaper without any window interaction at all:

Code: Select all

try if ((pDesktopWallpaper := ComObjCreate("{C2CF3110-460E-4fc1-B9D0-8A1C0C9CC4BD}", "{B92B56A9-8B55-4E14-9A89-0199BBB6F93B}"))) {
	DllCall(NumGet(NumGet(pDesktopWallpaper+0)+16*A_PtrSize), "Ptr", pDesktopWallpaper, "Ptr", 0, "UInt", 0) ; IDesktopWallpaper::AdvanceSlideshow - https://msdn.microsoft.com/en-us/library/windows/desktop/hh706947(v=vs.85).aspx
	ObjRelease(pDesktopWallpaper)
}
Man, I know this post has been here for a while but I just found it and works like a charm! Just what I needed it. I'm still trying to understand every piece of your code tho. Thanks a lot.
I'll second that!

As for how to understand it, it's COM so a bit hard to read.
First he creates a COM object named pDesktopWallpaper with CLSID {C2CF3110-460E-4fc1-B9D0-8A1C0C9CC4BD} and IID {B92B56A9-8B55-4E14-9A89-0199BBB6F93B}".
He did not come up with those ID's, they're in a list from Microsoft ;)
After that, he sends a DllCall which - if I understand it correctly - sends the message to the object to go to the next background.
And lastly, he releases the pDesktopWallpaper object from memory.
Any idea where I can learn more about using desktop COM with AutoHotKey? I know how to use it with Excel and Internet Explorer, but I'd love to find some tutorials where I can make system changes like this using COM.
User avatar
Maestr0
Posts: 136
Joined: 05 Dec 2013, 17:43

Re: A subroutine to switch to the next background (Windows 10)

08 Oct 2018, 08:18

Thoughtfu1Tux wrote: Any idea where I can learn more about using desktop COM with AutoHotKey? I know how to use it with Excel and Internet Explorer, but I'd love to find some tutorials where I can make system changes like this using COM.
Sorry, no, but if you do find a good place, let me know, because I'm also interested in learning :D
CivilizationAce
Posts: 4
Joined: 24 Feb 2022, 21:44

Re: A subroutine to switch to the next background (Windows 10)

24 Feb 2022, 21:57

This code looks like a fragment to me. Would we just embed this code in code to make it fire off when we (for instance) use Win+n ?
User avatar
Maestr0
Posts: 136
Joined: 05 Dec 2013, 17:43

Re: A subroutine to switch to the next background (Windows 10)

24 Feb 2022, 22:11

CivilizationAce wrote:
24 Feb 2022, 21:57
This code looks like a fragment to me. Would we just embed this code in code to make it fire off when we (for instance) use Win+n ?
My code is complete.
Here's an example of how you might bind the dllcall method to win-n (untested)

Code: Select all

#n::next_bg()

next_bg() {
	try if ((pDesktopWallpaper := ComObjCreate("{C2CF3110-460E-4fc1-B9D0-8A1C0C9CC4BD}", "{B92B56A9-8B55-4E14-9A89-0199BBB6F93B}"))) {
		DllCall(NumGet(NumGet(pDesktopWallpaper+0)+16*A_PtrSize), "Ptr", pDesktopWallpaper, "Ptr", 0, "UInt", 0) ; IDesktopWallpaper::AdvanceSlideshow - https://msdn.microsoft.com/en-us/library/windows/desktop/hh706947(v=vs.85).aspx
		ObjRelease(pDesktopWallpaper)
	}
}
User avatar
Maestr0
Posts: 136
Joined: 05 Dec 2013, 17:43

Re: A subroutine to switch to the next background (Windows 10)

25 Feb 2022, 16:17

iseahound wrote:
25 Feb 2022, 15:58
👍 Thanks
np, we're all in this together. Just report here or in Discord if you run into issues :)
CivilizationAce
Posts: 4
Joined: 24 Feb 2022, 21:44

Re: A subroutine to switch to the next background (Windows 10)

26 Feb 2022, 16:58

Works like a charm. Thanks to all involved. :D
Maestr0 wrote:
24 Feb 2022, 22:11
CivilizationAce wrote:
24 Feb 2022, 21:57
This code looks like a fragment to me. Would we just embed this code in code to make it fire off when we (for instance) use Win+n ?
My code is complete.
Here's an example of how you might bind the dllcall method to win-n (untested)

Code: Select all

#n::next_bg()

next_bg() {
	try if ((pDesktopWallpaper := ComObjCreate("{C2CF3110-460E-4fc1-B9D0-8A1C0C9CC4BD}", "{B92B56A9-8B55-4E14-9A89-0199BBB6F93B}"))) {
		DllCall(NumGet(NumGet(pDesktopWallpaper+0)+16*A_PtrSize), "Ptr", pDesktopWallpaper, "Ptr", 0, "UInt", 0) ; IDesktopWallpaper::AdvanceSlideshow - https://msdn.microsoft.com/en-us/library/windows/desktop/hh706947(v=vs.85).aspx
		ObjRelease(pDesktopWallpaper)
	}
}
sum1yadonkno
Posts: 1
Joined: 23 Jun 2023, 13:28

Re: A subroutine to switch to the next background (Windows 10)

23 Jun 2023, 13:31

CivilizationAce wrote:
26 Feb 2022, 16:58
Works like a charm. Thanks to all involved. :D
Maestr0 wrote:
24 Feb 2022, 22:11
CivilizationAce wrote:
24 Feb 2022, 21:57
This code looks like a fragment to me. Would we just embed this code in code to make it fire off when we (for instance) use Win+n ?
My code is complete.
Here's an example of how you might bind the dllcall method to win-n (untested)

Code: Select all

#n::next_bg()

next_bg() {
	try if ((pDesktopWallpaper := ComObjCreate("{C2CF3110-460E-4fc1-B9D0-8A1C0C9CC4BD}", "{B92B56A9-8B55-4E14-9A89-0199BBB6F93B}"))) {
		DllCall(NumGet(NumGet(pDesktopWallpaper+0)+16*A_PtrSize), "Ptr", pDesktopWallpaper, "Ptr", 0, "UInt", 0) ; IDesktopWallpaper::AdvanceSlideshow - https://msdn.microsoft.com/en-us/library/windows/desktop/hh706947(v=vs.85).aspx
		ObjRelease(pDesktopWallpaper)
	}
}
can you tell me how you got it working i am new to ahk and i dont know how to change this code according to my pc

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 127 guests