Send WM_SETTINGCHANGE message to refresh desktop

2019-08-05 16:24发布

问题:

I managed to programmatically change my desktop background via this one-liner:

DllCall("user32.dll", "int", "SystemParametersInfo", "int", 20, "int", 0, "str", $CmdLine[1], "int", 0)

… by drag and drop of the desired wallpaper onto the compiled script.

I have another program that changes the desktop but for some reason fails to send the WM_SETTINGCHANGE message (I can see the key HKCU\Control Panel\Desktop\Wallpaper getting updated). How can I send this message to trigger a wallpaper refresh? Is WM_SETTINGCHANGE the correct message? Or is the first script the only way?

I tried this but it doesn't work:

DllCall("user32.dll", "lresult", "SendMessage", _
     "int", 0xffff, _
     "int", 0x001a, _
     "int", 0x0014, _
     "str", "HKCU\Control Panel")

0xffff is for broadcasting the message to every window, 0x001a stands for WM_SETTINGCHANGE, 0x0014 is the code for spi_setdeskwallpaper. The taskbar in Windows 7 flickers but the wallpaper doesn't change.

If you solve my problem in C, C++ or Visual Basic I'll convert it to AutoIt myself (I just need the numerical codes of the called functions).

回答1:

#include <SendMessage.au3>
#include <WindowsConstants.au3>

Dim $hWnd = WinGetHandle('[CLASS:Progman]')
_SendMessage($hWnd, $WM_COMMAND, 0x0001A220)

Should work. Let me know if it doesn't, so I can update this answer.