|
| 1 | +// Copyright (c) 2024 Files Community |
| 2 | +// Licensed under the MIT License. See the LICENSE. |
| 3 | + |
| 4 | +using Windows.Storage; |
| 5 | +using Windows.System.UserProfile; |
| 6 | +using Windows.Win32; |
| 7 | +using Windows.Win32.Foundation; |
| 8 | +using Windows.Win32.System.Com; |
| 9 | +using Windows.Win32.UI.Shell; |
| 10 | +using Windows.Win32.UI.Shell.Common; |
| 11 | + |
| 12 | +namespace Files.App.Services |
| 13 | +{ |
| 14 | + /// <inheritdoc cref="IWindowsWallpaperService"/> |
| 15 | + public sealed class WindowsWallpaperService : IWindowsWallpaperService |
| 16 | + { |
| 17 | + /// <inheritdoc/> |
| 18 | + public unsafe void SetDesktopWallpaper(string szPath) |
| 19 | + { |
| 20 | + PInvoke.CoCreateInstance( |
| 21 | + new Guid("{C2CF3110-460E-4fc1-B9D0-8A1C0C9CC4BD}"), |
| 22 | + null, |
| 23 | + CLSCTX.CLSCTX_LOCAL_SERVER, |
| 24 | + out IDesktopWallpaper desktopWallpaper) |
| 25 | + .ThrowOnFailure(); |
| 26 | + |
| 27 | + desktopWallpaper.GetMonitorDevicePathCount(out var dwMonitorCount); |
| 28 | + |
| 29 | + fixed (char* pszPath = szPath) |
| 30 | + { |
| 31 | + var pwszPath = new PWSTR(pszPath); |
| 32 | + |
| 33 | + for (uint dwIndex = 0; dwIndex < dwMonitorCount; dwIndex++) |
| 34 | + { |
| 35 | + desktopWallpaper.GetMonitorDevicePathAt(dwIndex, out var pMonitorID); |
| 36 | + desktopWallpaper.SetWallpaper(pMonitorID, pwszPath); |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + /// <inheritdoc/> |
| 42 | + public unsafe void SetDesktopSlideshow(string[] aszPaths) |
| 43 | + { |
| 44 | + PInvoke.CoCreateInstance( |
| 45 | + new Guid("{C2CF3110-460E-4fc1-B9D0-8A1C0C9CC4BD}"), |
| 46 | + null, |
| 47 | + CLSCTX.CLSCTX_LOCAL_SERVER, |
| 48 | + out IDesktopWallpaper desktopWallpaper) |
| 49 | + .ThrowOnFailure(); |
| 50 | + |
| 51 | + var dwCount = (uint)aszPaths.Length; |
| 52 | + |
| 53 | + fixed (ITEMIDLIST** idList = new ITEMIDLIST*[dwCount]) |
| 54 | + { |
| 55 | + for (uint dwIndex = 0u; dwIndex < dwCount; dwIndex++) |
| 56 | + { |
| 57 | + var id = PInvoke.ILCreateFromPath(aszPaths[dwIndex]); |
| 58 | + idList[dwIndex] = id; |
| 59 | + } |
| 60 | + |
| 61 | + // Get shell item array from images to use for slideshow |
| 62 | + PInvoke.SHCreateShellItemArrayFromIDLists(dwCount, idList, out var shellItemArray); |
| 63 | + |
| 64 | + // Set slideshow |
| 65 | + desktopWallpaper.SetSlideshow(shellItemArray); |
| 66 | + } |
| 67 | + |
| 68 | + // Set wallpaper to fill desktop. |
| 69 | + desktopWallpaper.SetPosition(DESKTOP_WALLPAPER_POSITION.DWPOS_FILL); |
| 70 | + } |
| 71 | + |
| 72 | + /// <inheritdoc/> |
| 73 | + public async Task SetLockScreenWallpaper(string szPath) |
| 74 | + { |
| 75 | + IStorageFile sourceFile = await StorageFile.GetFileFromPathAsync(szPath); |
| 76 | + await LockScreen.SetImageFileAsync(sourceFile); |
| 77 | + } |
| 78 | + } |
| 79 | +} |
0 commit comments