Click here to Skip to main content
15,891,375 members
Articles / Desktop Programming / WPF

Another Screensaver with WPF

Rate me:
Please Sign up or sign in to vote.
4.87/5 (17 votes)
27 Jan 2012CDDL8 min read 72.1K   4.3K   63  
Lessons learnt from writing a screensaver with WPF
using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Media;

namespace RZWScreenSaver{
    class TemporaryPictureSource : IPictureSource{
        public TemporaryPictureSource(FolderCollectionSet folderSet, SlideMode slideMode, int delayTime)
            : this(new PictureSource(folderSet, slideMode, delayTime), slideMode, delayTime){
        }
        public TemporaryPictureSource(IPictureSource mainSource, SlideMode slideMode, int delayTime){
            Debug.Assert(mainSource != null);
            this.mainSource = mainSource;
            mode = slideMode;
            this.delayTime = delayTime;
        }
        public bool TempMode{
            get { return tempSource != null; }
        }
        public void SwitchToCurrentFolder(){
            var currentFolder = Path.GetDirectoryName(CurrentPictureFile);
            if (TempMode)
                tempSource.Stop();
            else if (!mainSource.IsPaused)
                    mainSource.Pause();
            tempSource =
                new PictureSource(
                    new FolderCollectionSet{
                                               new FolderCollection{
                                                                       new FolderInclusion(currentFolder, InclusionMode.Recursive)
                                                                   }
                                           },
                                           mode, delayTime);
            tempSource.PictureChanged += pictureChangedDelegates;
            tempSource.Start();
        }
        public void RevertToMainSet(){
            if (tempSource != null){
                tempSource.Stop();
                tempSource = null;
            }
            if (mainSource.IsPaused)
                mainSource.Resume();
        }
        #region Implementation of IPictureSource

        public ImageSource CurrentPicture{
            get { return Source.CurrentPicture; }
        }
        public string CurrentPictureFile{
            get { return Source.CurrentPictureFile; }
        }
        public int PictureIndex{
            get { return mainSource.PictureIndex; }
        }
        public bool IsPaused{
            get { return Source.IsPaused; }
        }
        public void Start(){
            Source.Start();
        }
        public void Pause(){
            Source.Pause();
        }
        public void Resume(){
            Source.Resume();
        }
        public void Stop(){
            Source.Stop();
        }
        public void SwitchToSet(int setIndex){
            Source.SwitchToSet(setIndex);
        }
        public void RestorePicturePosition(int numberToDiscard){
            Source.RestorePicturePosition(numberToDiscard);
        }
        public bool DeleteCurrentPicture(){
            return Source.DeleteCurrentPicture();
        }
        public bool MoveCurrentPictureTo(string targetFolder){
            return Source.MoveCurrentPictureTo(targetFolder);
        }
        public event EventHandler PictureSetChanged{
            add{
                mainSource.PictureSetChanged += value;
                pictureSetChangedDelegates += value;
            }
            remove{
                mainSource.PictureSetChanged -= value;
                pictureSetChangedDelegates -= value;
            }
        }
        public event EventHandler<PictureChangedEventArgs> PictureChanged{
            add{
                mainSource.PictureChanged += value;
                pictureChangedDelegates += value;
            }
            remove{
                mainSource.PictureChanged -= value;
                pictureChangedDelegates -= value;
            }
        }
        EventHandler pictureSetChangedDelegates;
        EventHandler<PictureChangedEventArgs> pictureChangedDelegates;

        #endregion
        IPictureSource Source{
            get { return tempSource ?? mainSource; }
        }

        readonly IPictureSource mainSource;
        IPictureSource tempSource;
        readonly SlideMode mode;
        readonly int delayTime;
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Common Development and Distribution License (CDDL)


Written By
Architect
Thailand Thailand
C/C++ and C# programmer.

Comments and Discussions