1
1
// Copyright (c) 2023 Files Community
2
2
// Licensed under the MIT License. See the LICENSE.
3
3
4
- using System ;
5
- using System . Linq ;
6
- using System . Threading . Tasks ;
4
+ using Microsoft . UI . Xaml . Controls ;
7
5
using Vanara . PInvoke ;
6
+ using Windows . Foundation . Metadata ;
8
7
using Windows . Storage ;
9
8
using Windows . System . UserProfile ;
10
9
@@ -14,12 +13,20 @@ public static class WallpaperHelpers
14
13
{
15
14
public static async Task SetAsBackgroundAsync ( WallpaperType type , string filePath )
16
15
{
16
+
17
17
if ( type == WallpaperType . Desktop )
18
18
{
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
+ }
23
30
}
24
31
else if ( type == WallpaperType . LockScreen )
25
32
{
@@ -34,15 +41,37 @@ public static void SetSlideshow(string[] filePaths)
34
41
if ( filePaths is null || ! filePaths . Any ( ) )
35
42
return ;
36
43
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
+ } ;
39
70
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 ;
43
73
44
- // Set wallpaper to fill desktop.
45
- wallpaper . SetPosition ( Shell32 . DESKTOP_WALLPAPER_POSITION . DWPOS_FILL ) ;
74
+ await errorDialog . TryShowAsync ( ) ;
46
75
}
47
76
}
48
77
}
0 commit comments