Click here to Skip to main content
12,395,645 members (67,166 online)
Click here to Skip to main content

Stats

42.4K views
3.7K downloads
62 bookmarked
Posted

Another Screensaver with WPF

, 27 Jan 2012 CDDL
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)

Share

About the Author

Ruxo Zheng
Technical Lead
Thailand Thailand
C/C++ and C# programmer.

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160721.1 | Last Updated 27 Jan 2012
Article Copyright 2009 by Ruxo Zheng
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid