Beispiel #1
0
        /// <summary>
        /// Begins a new slideshow, cancelling any active ones and setting the initial
        /// background.
        /// </summary>
        /// <param name="path">The path to the background directory.</param>
        /// <param name="position">The position with which the background will be displayed.</param>
        /// <param name="interval">The interval before which the next background will be set.</param>
        /// <param name="index">The file index within the directory at which to begin.</param>
        public static void BeginSlideshow(string path, PicturePosition position,
                                          int interval, int index)
        {
            if (interval < 0)
            {
                throw new ArgumentOutOfRangeException("showInterval");
            }
            if (index < 0)
            {
                throw new ArgumentOutOfRangeException("showIndex");
            }

            // Get the absolute path to the directory
            path = Path.GetFullPath(path);

            // Get the current images in the given directory
            string[] images = GetBackgroundsInFolder(path);

            // Wrap index
            if (index >= images.Length)
            {
                index = 0;
            }

            // Clamp interval
            interval = Math.Min(interval, 44640);

            // Save registry values
            using (var key = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop", true))
            {
                key.SetValue("WWU CurrentIndex", index);
                key.SetValue("WWU CurrentInterval", interval);
                key.SetValue("WWU CurrentDir", path);
            }

            // Remove any current slideshows
            CancelSlideshow();

            // Schedule slideshow
            using (TaskService service = new TaskService())
            {
                // Create new task
                TaskDefinition task = service.NewTask();

                // Create time trigger
                var trigger = new TimeTrigger();
                trigger.SetRepetition(TimeSpan.FromMinutes(interval), TimeSpan.Zero);

                task.Triggers.Add(trigger);
                task.Settings.StartWhenAvailable = true;
                task.Settings.WakeToRun          = false;

                // Add re-execution to set next background
                task.Actions.Add(new ExecAction(GetWallpaperwExe() + " /next"));

                // Register task
                service.RootFolder.RegisterTaskDefinition("wwu_slideshow", task);
            }

            // Now actually set the first wallpaper

            // Set blank if no images in folder
            if (images.Length == 0)
            {
                WallpaperInterop.SetWallpaper("", PicturePosition.Center, false);
            }
            // Otherwise set background to the next image
            else
            {
                WallpaperInterop.SetWallpaper(images[index], position, false);
            }
        }