Skip to content

Commit

Permalink
Improve wallpaper API and exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
t1m0thyj committed Nov 8, 2018
1 parent 328f12d commit 04ba60e
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 58 deletions.
9 changes: 0 additions & 9 deletions src/DesktopHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,5 @@ public override void OpenUpdateLink()
{
System.Diagnostics.Process.Start(updateLink);
}

public override void SetWallpaper(string imageFilename)
{
string imagePath = Path.Combine(Directory.GetCurrentDirectory(), "images",
imageFilename);

WallpaperChanger.EnableTransitions();
WallpaperChanger.SetWallpaper(imagePath);
}
}
}
1 change: 0 additions & 1 deletion src/JsonConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public class AppConfig
public bool changeSystemTheme { get; set; }
public string themeName { get; set; }
public bool useWindowsLocation { get; set; }
//public bool changeLockScreen { get; set; }
}

public class ThemeConfig
Expand Down
14 changes: 13 additions & 1 deletion src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Threading;

namespace WinDynamicDesktop
{
Expand All @@ -16,16 +17,27 @@ static class Program
static void Main()
{
Environment.CurrentDirectory = UwpDesktop.GetHelper().GetCurrentDirectory();
Application.ThreadException += OnThreadException;
AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new AppContext());
}

static void OnThreadException(object sender, ThreadExceptionEventArgs e)
{
LogError(e.Exception);
}

static void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
string errorMessage = ((Exception)e.ExceptionObject).ToString() + "\n";
LogError(e.ExceptionObject as Exception);
}

static void LogError(Exception exc)
{
string errorMessage = exc.ToString() + "\n";
string logFilename = Path.Combine(Directory.GetCurrentDirectory(),
Environment.GetCommandLineArgs()[0] + ".log");

Expand Down
2 changes: 1 addition & 1 deletion src/ThemeDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ private void okButton_Click(object sender, EventArgs e)
}
else
{
UwpDesktop.GetHelper().SetWallpaper(windowsWallpaper);
WallpaperApi.SetWallpaper(windowsWallpaper);
}

okButton.Enabled = true;
Expand Down
2 changes: 0 additions & 2 deletions src/UwpDesktop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ abstract class PlatformHelper
public abstract void ToggleStartOnBoot();

public abstract void OpenUpdateLink();

public abstract void SetWallpaper(string imageFilename);
}

class UwpDesktop
Expand Down
15 changes: 0 additions & 15 deletions src/UwpHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,5 @@ public override async void OpenUpdateLink()
await Windows.System.Launcher.LaunchUriAsync(
new Uri("ms-windows-store://downloadsandupdates"));
}

public override async void SetWallpaper(string imageFilename)
{
var uri = new Uri("ms-appdata:///local/images/" + imageFilename);
var file = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(uri);

var profileSettings =
Windows.System.UserProfile.UserProfilePersonalizationSettings.Current;
await profileSettings.TrySetWallpaperImageAsync(file);

//if (JsonConfig.settings.changeLockScreen)
//{
// await profileSettings.TrySetLockScreenImageAsync(file);
//}
}
}
}
54 changes: 28 additions & 26 deletions src/WallpaperChanger.cs → src/WallpaperApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.Threading;

namespace WinDynamicDesktop
{
Expand Down Expand Up @@ -209,51 +210,52 @@ int GetWallpaper([MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pw
int AddUrl(IntPtr hwnd, [MarshalAs(UnmanagedType.LPWStr)] string pszSource, ref COMPONENT pcomp, AddURL dwFlags);
[PreserveSig]
int GetDesktopItemBySource([MarshalAs(UnmanagedType.LPWStr)] string pwszSource, ref COMPONENT pcomp, int dwReserved);

}

/// <summary>
/// Summary description for shlobj.
/// Written by: Eber Irigoyen
/// on: 11/23/2005
/// </summary>
public class WallpaperChanger
public class ActiveDesktopWrapper
{
public static readonly Guid CLSID_ActiveDesktop =
static readonly Guid CLSID_ActiveDesktop =
new Guid("{75048700-EF1F-11D0-9888-006097DEACF9}");

public static IActiveDesktop GetActiveDesktop()
{
Type typeActiveDesktop = Type.GetTypeFromCLSID(WallpaperChanger.CLSID_ActiveDesktop);
Type typeActiveDesktop = Type.GetTypeFromCLSID(CLSID_ActiveDesktop);
return (IActiveDesktop)Activator.CreateInstance(typeActiveDesktop);
}
}

[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int SendMessageTimeout(
IntPtr hWnd, // handle to destination window
uint Msg, // message
IntPtr wParam, // first message parameter
IntPtr lParam, // second message parameter
uint fuFlags,
uint uTimeout,
out IntPtr result
);

public class WallpaperApi
{
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, IntPtr ZeroOnly);
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

public static void EnableTransitions()
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern int SendMessageTimeout(IntPtr hWnd, uint Msg, IntPtr wParam,
IntPtr lParam, uint fuFlags, uint uTimeout, out IntPtr result);

private static void EnableTransitions()
{
IntPtr result = IntPtr.Zero;
SendMessageTimeout(FindWindow("Progman", IntPtr.Zero), 0x52c, IntPtr.Zero, IntPtr.Zero,
SendMessageTimeout(FindWindow("Progman", null), 0x52c, IntPtr.Zero, IntPtr.Zero,
0, 500, out result);
}

public static void SetWallpaper(string imagePath)
{
IActiveDesktop iad = GetActiveDesktop();
iad.SetWallpaper(imagePath, 0);
iad.ApplyChanges(AD_Apply.ALL | AD_Apply.FORCE | AD_Apply.BUFFERED_REFRESH);
EnableTransitions();

ThreadStart threadStarter = () =>
{
IActiveDesktop _activeDesktop = ActiveDesktopWrapper.GetActiveDesktop();
_activeDesktop.SetWallpaper(imagePath, 0);
_activeDesktop.ApplyChanges(AD_Apply.ALL | AD_Apply.FORCE);
Marshal.ReleaseComObject(_activeDesktop);
};
Thread thread = new Thread(threadStarter);
thread.SetApartmentState(ApartmentState.STA); // Set the thread to STA (required!)
thread.Start();
thread.Join(2000);
}
}
}
9 changes: 7 additions & 2 deletions src/WallpaperChangeScheduler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Globalization;
using System.Windows.Forms;
using Microsoft.Win32;
using System.IO;

namespace WinDynamicDesktop
{
Expand Down Expand Up @@ -91,8 +92,12 @@ private void StartTimer(long intervalTicks, TimeSpan maxInterval)

private void SetWallpaper(int imageId)
{
string imageFilename = ThemeManager.currentTheme.imageFilename.Replace("*", imageId.ToString());
UwpDesktop.GetHelper().SetWallpaper(imageFilename);
string imageFilename = ThemeManager.currentTheme.imageFilename.Replace("*",
imageId.ToString());
string imagePath = Path.Combine(Directory.GetCurrentDirectory(), "images",
imageFilename);

WallpaperApi.SetWallpaper(imagePath);

lastImageId = imageId;
}
Expand Down
2 changes: 1 addition & 1 deletion src/WinDynamicDesktop.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
<Compile Include="UwpDesktop.cs" />
<Compile Include="UwpHelper.cs" />
<Compile Include="UwpLocation.cs" />
<Compile Include="WallpaperChanger.cs" />
<Compile Include="WallpaperApi.cs" />
<Compile Include="WallpaperChangeScheduler.cs" />
<EmbeddedResource Include="AboutDialog.resx">
<DependentUpon>AboutDialog.cs</DependentUpon>
Expand Down

0 comments on commit 04ba60e

Please sign in to comment.