PageRenderTime 23ms CodeModel.GetById 20ms app.highlight 2ms RepoModel.GetById 0ms app.codeStats 0ms

/GammaJul.LgLcd.Samples.Wpf/SampleControl.xaml.cs

#
C# | 51 lines | 32 code | 6 blank | 13 comment | 6 complexity | 2dbe9a6a7158454b72f07184c58b1ab1 MD5 | raw file
Possible License(s): LGPL-2.1
 1using System;
 2using System.Collections.Generic;
 3using System.IO;
 4using System.Windows.Media;
 5using System.Windows.Media.Imaging;
 6
 7namespace GammaJul.LgLcd.Samples.Wpf {
 8
 9	/// <summary>
10	/// This simple controls loads images from the %windir%\Web\Wallpaper directory
11	/// and provides function to switch between them.
12	/// </summary>
13	public partial class SampleControl {
14		private readonly List<ImageSource> _images = new List<ImageSource>();
15		private int _currentIndex = -1;
16
17		/// <summary>
18		/// Switch to the previous image.
19		/// </summary>
20		public void PreviousImage() {
21			if (_images.Count == 0)
22				return;
23			if (--_currentIndex < 0)
24				_currentIndex = _images.Count - 1;
25			Img.Source = _images[_currentIndex];
26		}
27
28		/// <summary>
29		/// Switch to the next image.
30		/// </summary>
31		public void NextImage() {
32			if (_images.Count == 0)
33				return;
34			if (++_currentIndex >= _images.Count)
35				_currentIndex = 0;
36			Img.Source = _images[_currentIndex];
37		}
38
39		/// <summary>
40		/// Creates a new <see cref="SampleControl"/>.
41		/// </summary>
42		public SampleControl() {
43			InitializeComponent();
44			string wallpaperPath = Path.Combine(Path.GetDirectoryName(Environment.GetFolderPath(Environment.SpecialFolder.System)), "Web\\Wallpaper");
45			foreach (string file in Directory.GetFiles(wallpaperPath, "*.jpg", SearchOption.AllDirectories))
46				_images.Add(new BitmapImage(new Uri(file, UriKind.Absolute)));
47			NextImage();
48		}
49	}
50
51}