darklights
2023-01-09 17:11:10 +08:00
程序员嘛, 自己动手, 饿死罢就
$setwallpapersrc = @"
using System.Runtime.InteropServices;
public class Wallpaper
{
public const int SetDesktopWallpaper = 20;
public const int UpdateIniFile = 0x01;
public const int SendWinIniChange = 0x02;
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);
public static void SetWallpaper(string path)
{
SystemParametersInfo(SetDesktopWallpaper, 0, path, UpdateIniFile | SendWinIniChange);
}
}
"@
Add-Type -TypeDefinition $setwallpapersrc
cd $PSScriptRoot
$nr = [uint](Get-Content ".nr" -ErrorAction Ignore)
$ls = Get-ChildItem -r *.jpg,*.jpeg | ? Length -gt 100kb | Select-Object -First ($nr + 10) | % FullName
if ($nr -ge $ls.Length) { $nr = 0 }
[Wallpaper]::SetWallpaper($ls[$nr])
Set-Content '.nr' ($nr + 1)
用计划任务跑 pwsh -noni -nop -w hidden -f "C:\Path\To\Pictures\wallpaper.ps1"
实际上还是会有个小黑窗一闪而过,
受不了的话只能用其他语言改写, 性能还好点
又或者用个 exe 包一下
#include <string>
#include <Windows.h>
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
STARTUPINFO startup_info;
::memset(&startup_info, 0, sizeof(STARTUPINFO));
startup_info.cb = sizeof(STARTUPINFO);
std::wstring cmd = L"pwsh ";
cmd += lpCmdLine;
PROCESS_INFORMATION process_info;
::CreateProcessW(
nullptr,
cmd.data(),
nullptr,
nullptr,
true,
CREATE_NO_WINDOW,
nullptr,
nullptr,
&startup_info,
&process_info
);
return 0;
}
用计划任务跑 RunPwshNoWin -noni -nop -f "C:\Path\To\Pictures\wallpaper.ps1"