Пример #1
0
        public static void NextBackground()
        {
            int    index;
            int    interval;
            string path;

            // Read registry values
            using (var key = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop", true))
            {
                index    = (int)key.GetValue("WWU CurrentIndex", -1);
                interval = (int)key.GetValue("WWU CurrentInterval", 0);
                path     = (string)key.GetValue("WWU CurrentDir", null);
            }

            if (path == null || interval == 0)
            {
                throw new Exception("No slideshow currently in progress");
            }

            // Get the current images in the given directory
            string[] images = GetBackgroundsInFolder(path);
            // Wrap index
            if (++index >= images.Length)
            {
                index = 0;
            }

            // Set blank if no images in folder
            if (images.Length == 0)
            {
                WallpaperInterop.SetWallpaper("", PicturePosition.Center, false);
            }
            // Otherwise set background to the next image
            else
            {
                var pos = WallpaperInterop.GetWallpaperPosition();
                WallpaperInterop.SetWallpaper(images[index], pos, false);
            }
            // 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);
            }
        }
Пример #2
0
        static void Main(string[] args)
        {
            try
            {
                // Output current wallpaper details
                if (args.Length == 0)
                {
                    Console.WriteLine("Current Wallpaper ");
                    Console.WriteLine("\t Filepath: \"{0}\"", WallpaperInterop.GetWallpaperFilepath());
                    Console.WriteLine("\t Position: {0}", WallpaperInterop.GetWallpaperPosition());
                    Console.WriteLine("Current login background ");
                    Console.WriteLine("\t Filepath: \"{0}\"", WallpaperInterop.GetLoginBGFilepath());
                }
                // Output usage info
                else if (args.Contains("/?"))
                {
                    Program.printUsage();
                }
                // Handle just next background
                else if (args[0] == "/next")
                {
                    SlideshowHelper.NextBackground();
                }
                // Handle just endshow
                else if (args[0] == "/endshow")
                {
                    SlideshowHelper.CancelSlideshow();
                }
                else if (args[0] == "/login")
                {
                    if (args.Length == 1)
                    {
                        Program.errorOut("Expected path for option '/login'");
                    }
                    WallpaperInterop.SetLoginBG(args[1]);
                }
                // Perform wallpaper change
                else
                {
                    string path = args[0];
                    var    opts = new ProgramOptions(args);

                    // End show as requested
                    if (opts.EndShow)
                    {
                        SlideshowHelper.CancelSlideshow();
                    }
                    if (opts.Slideshow)
                    {
                        // Begin slideshow
                        SlideshowHelper.BeginSlideshow(path, opts.Position,
                                                       opts.ShowInterval, opts.ShowIndex);
                    }
                    else
                    {
                        // Get random image if selecting from directory
                        if (opts.Directory)
                        {
                            string[] walls = SlideshowHelper.GetBackgroundsInFolder(path);
                            path = walls[new Random().Next(0, walls.Length)];
                        }
                        // Call interop code
                        WallpaperInterop.SetWallpaper(path, opts.Position, opts.Copy);
                    }
                }
            }
            catch (Exception e) {
                errorOut(e.Message);
            }
        }
Пример #3
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);
            }
        }