11// Copyright (c) 2023 Files Community
22// Licensed under the MIT License. See the LICENSE.
33
4- using System ;
5- using System . Linq ;
6- using System . Threading . Tasks ;
4+ using Microsoft . UI . Xaml . Controls ;
75using Vanara . PInvoke ;
6+ using Windows . Foundation . Metadata ;
87using Windows . Storage ;
98using Windows . System . UserProfile ;
109
@@ -14,12 +13,20 @@ public static class WallpaperHelpers
1413 {
1514 public static async Task SetAsBackgroundAsync ( WallpaperType type , string filePath )
1615 {
16+
1717 if ( type == WallpaperType . Desktop )
1818 {
19- // Set the desktop background
20- var wallpaper = ( Shell32 . IDesktopWallpaper ) new Shell32 . DesktopWallpaper ( ) ;
21- wallpaper . GetMonitorDevicePathAt ( 0 , out var monitorId ) ;
22- wallpaper . SetWallpaper ( monitorId , filePath ) ;
19+ try
20+ {
21+ // Set the desktop background
22+ var wallpaper = ( Shell32 . IDesktopWallpaper ) new Shell32 . DesktopWallpaper ( ) ;
23+ wallpaper . GetMonitorDevicePathAt ( 0 , out var monitorId ) ;
24+ wallpaper . SetWallpaper ( monitorId , filePath ) ;
25+ }
26+ catch ( Exception ex )
27+ {
28+ ShowErrorPrompt ( ex . Message ) ;
29+ }
2330 }
2431 else if ( type == WallpaperType . LockScreen )
2532 {
@@ -34,15 +41,37 @@ public static void SetSlideshow(string[] filePaths)
3441 if ( filePaths is null || ! filePaths . Any ( ) )
3542 return ;
3643
37- var idList = filePaths . Select ( Shell32 . IntILCreateFromPath ) . ToArray ( ) ;
38- Shell32 . SHCreateShellItemArrayFromIDLists ( ( uint ) idList . Length , idList . ToArray ( ) , out var shellItemArray ) ;
44+ try
45+ {
46+ var idList = filePaths . Select ( Shell32 . IntILCreateFromPath ) . ToArray ( ) ;
47+ Shell32 . SHCreateShellItemArrayFromIDLists ( ( uint ) idList . Length , idList . ToArray ( ) , out var shellItemArray ) ;
48+
49+ // Set SlideShow
50+ var wallpaper = ( Shell32 . IDesktopWallpaper ) new Shell32 . DesktopWallpaper ( ) ;
51+ wallpaper . SetSlideshow ( shellItemArray ) ;
52+
53+ // Set wallpaper to fill desktop.
54+ wallpaper . SetPosition ( Shell32 . DESKTOP_WALLPAPER_POSITION . DWPOS_FILL ) ;
55+ }
56+ catch ( Exception ex )
57+ {
58+ ShowErrorPrompt ( ex . Message ) ;
59+ }
60+ }
61+
62+ private static async void ShowErrorPrompt ( string exception )
63+ {
64+ var errorDialog = new ContentDialog ( )
65+ {
66+ Title = "FailedToSetBackground" . GetLocalizedResource ( ) ,
67+ Content = exception ,
68+ PrimaryButtonText = "OK" . GetLocalizedResource ( ) ,
69+ } ;
3970
40- // Set SlideShow
41- var wallpaper = ( Shell32 . IDesktopWallpaper ) new Shell32 . DesktopWallpaper ( ) ;
42- wallpaper . SetSlideshow ( shellItemArray ) ;
71+ if ( ApiInformation . IsApiContractPresent ( "Windows.Foundation.UniversalApiContract" , 8 ) )
72+ errorDialog . XamlRoot = MainWindow . Instance . Content . XamlRoot ;
4373
44- // Set wallpaper to fill desktop.
45- wallpaper . SetPosition ( Shell32 . DESKTOP_WALLPAPER_POSITION . DWPOS_FILL ) ;
74+ await errorDialog . TryShowAsync ( ) ;
4675 }
4776 }
4877}
0 commit comments