Skip to content

Commit 04ba60e

Browse files
committed
Improve wallpaper API and exception handling
1 parent 328f12d commit 04ba60e

9 files changed

+50
-58
lines changed

src/DesktopHelper.cs

-9
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,5 @@ public override void OpenUpdateLink()
5555
{
5656
System.Diagnostics.Process.Start(updateLink);
5757
}
58-
59-
public override void SetWallpaper(string imageFilename)
60-
{
61-
string imagePath = Path.Combine(Directory.GetCurrentDirectory(), "images",
62-
imageFilename);
63-
64-
WallpaperChanger.EnableTransitions();
65-
WallpaperChanger.SetWallpaper(imagePath);
66-
}
6758
}
6859
}

src/JsonConfig.cs

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ public class AppConfig
2020
public bool changeSystemTheme { get; set; }
2121
public string themeName { get; set; }
2222
public bool useWindowsLocation { get; set; }
23-
//public bool changeLockScreen { get; set; }
2423
}
2524

2625
public class ThemeConfig

src/Program.cs

+13-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Threading.Tasks;
55
using System.Windows.Forms;
66
using System.IO;
7+
using System.Threading;
78

89
namespace WinDynamicDesktop
910
{
@@ -16,16 +17,27 @@ static class Program
1617
static void Main()
1718
{
1819
Environment.CurrentDirectory = UwpDesktop.GetHelper().GetCurrentDirectory();
20+
Application.ThreadException += OnThreadException;
1921
AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
2022

2123
Application.EnableVisualStyles();
2224
Application.SetCompatibleTextRenderingDefault(false);
2325
Application.Run(new AppContext());
2426
}
2527

28+
static void OnThreadException(object sender, ThreadExceptionEventArgs e)
29+
{
30+
LogError(e.Exception);
31+
}
32+
2633
static void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)
2734
{
28-
string errorMessage = ((Exception)e.ExceptionObject).ToString() + "\n";
35+
LogError(e.ExceptionObject as Exception);
36+
}
37+
38+
static void LogError(Exception exc)
39+
{
40+
string errorMessage = exc.ToString() + "\n";
2941
string logFilename = Path.Combine(Directory.GetCurrentDirectory(),
3042
Environment.GetCommandLineArgs()[0] + ".log");
3143

src/ThemeDialog.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ private void okButton_Click(object sender, EventArgs e)
232232
}
233233
else
234234
{
235-
UwpDesktop.GetHelper().SetWallpaper(windowsWallpaper);
235+
WallpaperApi.SetWallpaper(windowsWallpaper);
236236
}
237237

238238
okButton.Enabled = true;

src/UwpDesktop.cs

-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ abstract class PlatformHelper
1616
public abstract void ToggleStartOnBoot();
1717

1818
public abstract void OpenUpdateLink();
19-
20-
public abstract void SetWallpaper(string imageFilename);
2119
}
2220

2321
class UwpDesktop

src/UwpHelper.cs

-15
Original file line numberDiff line numberDiff line change
@@ -70,20 +70,5 @@ public override async void OpenUpdateLink()
7070
await Windows.System.Launcher.LaunchUriAsync(
7171
new Uri("ms-windows-store://downloadsandupdates"));
7272
}
73-
74-
public override async void SetWallpaper(string imageFilename)
75-
{
76-
var uri = new Uri("ms-appdata:///local/images/" + imageFilename);
77-
var file = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(uri);
78-
79-
var profileSettings =
80-
Windows.System.UserProfile.UserProfilePersonalizationSettings.Current;
81-
await profileSettings.TrySetWallpaperImageAsync(file);
82-
83-
//if (JsonConfig.settings.changeLockScreen)
84-
//{
85-
// await profileSettings.TrySetLockScreenImageAsync(file);
86-
//}
87-
}
8873
}
8974
}

src/WallpaperChanger.cs renamed to src/WallpaperApi.cs

+28-26
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Text;
55
using System.Threading.Tasks;
66
using System.Runtime.InteropServices;
7+
using System.Threading;
78

89
namespace WinDynamicDesktop
910
{
@@ -209,51 +210,52 @@ int GetWallpaper([MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pw
209210
int AddUrl(IntPtr hwnd, [MarshalAs(UnmanagedType.LPWStr)] string pszSource, ref COMPONENT pcomp, AddURL dwFlags);
210211
[PreserveSig]
211212
int GetDesktopItemBySource([MarshalAs(UnmanagedType.LPWStr)] string pwszSource, ref COMPONENT pcomp, int dwReserved);
212-
213213
}
214214

215-
/// <summary>
216-
/// Summary description for shlobj.
217-
/// Written by: Eber Irigoyen
218-
/// on: 11/23/2005
219-
/// </summary>
220-
public class WallpaperChanger
215+
public class ActiveDesktopWrapper
221216
{
222-
public static readonly Guid CLSID_ActiveDesktop =
217+
static readonly Guid CLSID_ActiveDesktop =
223218
new Guid("{75048700-EF1F-11D0-9888-006097DEACF9}");
224219

225220
public static IActiveDesktop GetActiveDesktop()
226221
{
227-
Type typeActiveDesktop = Type.GetTypeFromCLSID(WallpaperChanger.CLSID_ActiveDesktop);
222+
Type typeActiveDesktop = Type.GetTypeFromCLSID(CLSID_ActiveDesktop);
228223
return (IActiveDesktop)Activator.CreateInstance(typeActiveDesktop);
229224
}
225+
}
230226

231-
[DllImport("user32.dll", CharSet = CharSet.Auto)]
232-
public static extern int SendMessageTimeout(
233-
IntPtr hWnd, // handle to destination window
234-
uint Msg, // message
235-
IntPtr wParam, // first message parameter
236-
IntPtr lParam, // second message parameter
237-
uint fuFlags,
238-
uint uTimeout,
239-
out IntPtr result
240-
);
241-
227+
public class WallpaperApi
228+
{
242229
[DllImport("user32.dll", SetLastError = true)]
243-
static extern IntPtr FindWindow(string lpClassName, IntPtr ZeroOnly);
230+
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
244231

245-
public static void EnableTransitions()
232+
[DllImport("user32.dll", CharSet = CharSet.Auto)]
233+
private static extern int SendMessageTimeout(IntPtr hWnd, uint Msg, IntPtr wParam,
234+
IntPtr lParam, uint fuFlags, uint uTimeout, out IntPtr result);
235+
236+
private static void EnableTransitions()
246237
{
247238
IntPtr result = IntPtr.Zero;
248-
SendMessageTimeout(FindWindow("Progman", IntPtr.Zero), 0x52c, IntPtr.Zero, IntPtr.Zero,
239+
SendMessageTimeout(FindWindow("Progman", null), 0x52c, IntPtr.Zero, IntPtr.Zero,
249240
0, 500, out result);
250241
}
251242

252243
public static void SetWallpaper(string imagePath)
253244
{
254-
IActiveDesktop iad = GetActiveDesktop();
255-
iad.SetWallpaper(imagePath, 0);
256-
iad.ApplyChanges(AD_Apply.ALL | AD_Apply.FORCE | AD_Apply.BUFFERED_REFRESH);
245+
EnableTransitions();
246+
247+
ThreadStart threadStarter = () =>
248+
{
249+
IActiveDesktop _activeDesktop = ActiveDesktopWrapper.GetActiveDesktop();
250+
_activeDesktop.SetWallpaper(imagePath, 0);
251+
_activeDesktop.ApplyChanges(AD_Apply.ALL | AD_Apply.FORCE);
252+
253+
Marshal.ReleaseComObject(_activeDesktop);
254+
};
255+
Thread thread = new Thread(threadStarter);
256+
thread.SetApartmentState(ApartmentState.STA); // Set the thread to STA (required!)
257+
thread.Start();
258+
thread.Join(2000);
257259
}
258260
}
259261
}

src/WallpaperChangeScheduler.cs

+7-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Globalization;
77
using System.Windows.Forms;
88
using Microsoft.Win32;
9+
using System.IO;
910

1011
namespace WinDynamicDesktop
1112
{
@@ -91,8 +92,12 @@ private void StartTimer(long intervalTicks, TimeSpan maxInterval)
9192

9293
private void SetWallpaper(int imageId)
9394
{
94-
string imageFilename = ThemeManager.currentTheme.imageFilename.Replace("*", imageId.ToString());
95-
UwpDesktop.GetHelper().SetWallpaper(imageFilename);
95+
string imageFilename = ThemeManager.currentTheme.imageFilename.Replace("*",
96+
imageId.ToString());
97+
string imagePath = Path.Combine(Directory.GetCurrentDirectory(), "images",
98+
imageFilename);
99+
100+
WallpaperApi.SetWallpaper(imagePath);
96101

97102
lastImageId = imageId;
98103
}

src/WinDynamicDesktop.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@
118118
<Compile Include="UwpDesktop.cs" />
119119
<Compile Include="UwpHelper.cs" />
120120
<Compile Include="UwpLocation.cs" />
121-
<Compile Include="WallpaperChanger.cs" />
121+
<Compile Include="WallpaperApi.cs" />
122122
<Compile Include="WallpaperChangeScheduler.cs" />
123123
<EmbeddedResource Include="AboutDialog.resx">
124124
<DependentUpon>AboutDialog.cs</DependentUpon>

0 commit comments

Comments
 (0)