|
4 | 4 | using System.Text;
|
5 | 5 | using System.Threading.Tasks;
|
6 | 6 | using System.Runtime.InteropServices;
|
| 7 | +using System.Threading; |
7 | 8 |
|
8 | 9 | namespace WinDynamicDesktop
|
9 | 10 | {
|
@@ -209,51 +210,52 @@ int GetWallpaper([MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pw
|
209 | 210 | int AddUrl(IntPtr hwnd, [MarshalAs(UnmanagedType.LPWStr)] string pszSource, ref COMPONENT pcomp, AddURL dwFlags);
|
210 | 211 | [PreserveSig]
|
211 | 212 | int GetDesktopItemBySource([MarshalAs(UnmanagedType.LPWStr)] string pwszSource, ref COMPONENT pcomp, int dwReserved);
|
212 |
| - |
213 | 213 | }
|
214 | 214 |
|
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 |
221 | 216 | {
|
222 |
| - public static readonly Guid CLSID_ActiveDesktop = |
| 217 | + static readonly Guid CLSID_ActiveDesktop = |
223 | 218 | new Guid("{75048700-EF1F-11D0-9888-006097DEACF9}");
|
224 | 219 |
|
225 | 220 | public static IActiveDesktop GetActiveDesktop()
|
226 | 221 | {
|
227 |
| - Type typeActiveDesktop = Type.GetTypeFromCLSID(WallpaperChanger.CLSID_ActiveDesktop); |
| 222 | + Type typeActiveDesktop = Type.GetTypeFromCLSID(CLSID_ActiveDesktop); |
228 | 223 | return (IActiveDesktop)Activator.CreateInstance(typeActiveDesktop);
|
229 | 224 | }
|
| 225 | + } |
230 | 226 |
|
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 | + { |
242 | 229 | [DllImport("user32.dll", SetLastError = true)]
|
243 |
| - static extern IntPtr FindWindow(string lpClassName, IntPtr ZeroOnly); |
| 230 | + private static extern IntPtr FindWindow(string lpClassName, string lpWindowName); |
244 | 231 |
|
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() |
246 | 237 | {
|
247 | 238 | 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, |
249 | 240 | 0, 500, out result);
|
250 | 241 | }
|
251 | 242 |
|
252 | 243 | public static void SetWallpaper(string imagePath)
|
253 | 244 | {
|
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); |
257 | 259 | }
|
258 | 260 | }
|
259 | 261 | }
|
0 commit comments