⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 slideshow.cs

📁 这是一个小型的相片管理器
💻 CS
字号:


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using VirtualPhotoOrganizer.Photo;
using VirtualPhotoOrganizer.Util;

namespace VirtualPhotoOrganizer
{
	/// <summary>
	/// Zusammenfassung f黵 SlideShow.
	/// </summary>
	internal class SlideShow : System.Windows.Forms.Form
	{
		// the photos we're supposed to iterate through
		private Photos Photos;
		private int CurrIndex;

		// screen width/height
		private int SWidth;
		private int SHeight;

		// our graphics object
		private Graphics Gr;
		private SolidBrush BlackBrush;
		private Rectangle R;

		private System.Windows.Forms.Button bLeft;
		private System.Windows.Forms.ImageList ilButtons;
		private System.Windows.Forms.Panel panelButtons;
		private System.Windows.Forms.Button bPause;
		private System.Windows.Forms.Button bPlay;
		private System.Windows.Forms.Button bStop;
		private System.Windows.Forms.Button bRight;
		private System.Windows.Forms.Timer timer;
		private System.ComponentModel.IContainer components;

		public SlideShow(Photos photos, int index) {
			//
			// Erforderlich f黵 die Windows Form-Designerunterst黷zung
			//
			InitializeComponent();

			Photos = photos;
			CurrIndex = index;

			Settings settings = new Settings();
			this.timer.Interval = settings.SlideShowInterval * 1000;
		}

		/// <summary>
		/// Die verwendeten Ressourcen bereinigen.
		/// </summary>
		protected override void Dispose(bool disposing) {
			if (disposing) {
				if (components != null) {
					components.Dispose();
				}
			}
			base.Dispose(disposing);
		}

		private void DrawPhoto() {
			// initialize the photo
			Photo.Photo p = Photos[CurrIndex];

			// initialize the gdi+ objects
			Gr = Graphics.FromHwnd(this.Handle);
			BlackBrush = new SolidBrush(Color.Black);
			R = new Rectangle(0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height);

			// load the photo into a bitmap
			Image img = Bitmap.FromFile(p.Path);
			Bitmap photo = new Bitmap(img);

			// clear the screen area
			Gr.FillRectangle(BlackBrush, R);

			if (photo.Height > this.ClientRectangle.Height || photo.Width > this.ClientRectangle.Height) {	// if the photo is bigger than the screen
				// calculate the size of the destRectangle
				double ratio = photo.Height / photo.Width;
				if (ratio < (this.ClientRectangle.Height / this.ClientRectangle.Width)) {
					// width is constant
					R.Width = this.ClientRectangle.Width;
					R.Height = (photo.Height * this.ClientRectangle.Width) / photo.Width;
					R.Offset(0, (this.ClientRectangle.Height - R.Height) / 2);
				} else {
					// height is constant
					R.Height = this.ClientRectangle.Height;
					R.Width = (photo.Width * this.ClientRectangle.Height) / photo.Height;
					R.Offset((this.ClientRectangle.Width - R.Width) / 2, 0);
				}
			} else {
				// just calculate the offset
				R.Width = photo.Width;
				R.Height = photo.Height;
				R.Offset((this.ClientRectangle.Width - R.Width) / 2, (this.ClientRectangle.Height - R.Height) / 2);
			}

			// draw the image to the form
			Gr.DrawImage(photo, R);

			// dispose the gdi+ objects
			img.Dispose();
			photo.Dispose();
			Gr.Dispose();
			BlackBrush.Dispose();
		}

		private void NextPhoto() {
			if (CurrIndex < Photos.Count - 1)
				CurrIndex++;
			else
				CurrIndex = 0;

			DrawPhoto();
		}

		private void PreviousPhoto() {
			if (CurrIndex != 0)
				CurrIndex--;
			else
				CurrIndex = Photos.Count - 1;

			DrawPhoto();
		}

		/// <summary>
		/// analyzes the key that was pressed and takes the apropriate action
		/// </summary>
		/// <param name="e"></param>
		private void AnalyzeKeys(System.Windows.Forms.KeyEventArgs e) {
			switch (e.KeyCode) {
				case Keys.Escape: this.Close();
					break;
				case Keys.Space:
					// check whether the timer is active or not and take the apropriate action
					if (timer.Enabled == false)
						timer.Enabled = true;
					else
						timer.Enabled = false;
					break;
				case Keys.Left: PreviousPhoto();
					break;
				case Keys.Down: NextPhoto();
					break;
				case Keys.Right: NextPhoto();
					break;
				case Keys.Up: PreviousPhoto();
					break;
				default:
					break;
			}
		}


		#region Vom Windows Form-Designer generierter Code
		/// <summary>
		/// Erforderliche Methode f黵 die Designerunterst黷zung. 
		/// Der Inhalt der Methode darf nicht mit dem Code-Editor ge鋘dert werden.
		/// </summary>
		private void InitializeComponent() {
			this.components = new System.ComponentModel.Container();
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(SlideShow));
			this.timer = new System.Windows.Forms.Timer(this.components);
			this.panelButtons = new System.Windows.Forms.Panel();
			this.bRight = new System.Windows.Forms.Button();
			this.ilButtons = new System.Windows.Forms.ImageList(this.components);
			this.bStop = new System.Windows.Forms.Button();
			this.bPlay = new System.Windows.Forms.Button();
			this.bPause = new System.Windows.Forms.Button();
			this.bLeft = new System.Windows.Forms.Button();
			this.panelButtons.SuspendLayout();
			this.SuspendLayout();
			// 
			// timer
			// 
			this.timer.Interval = 1000;
			this.timer.Tick += new System.EventHandler(this.timer_Tick);
			// 
			// panelButtons
			// 
			this.panelButtons.BackColor = System.Drawing.Color.White;
			this.panelButtons.Controls.Add(this.bRight);
			this.panelButtons.Controls.Add(this.bStop);
			this.panelButtons.Controls.Add(this.bPlay);
			this.panelButtons.Controls.Add(this.bPause);
			this.panelButtons.Controls.Add(this.bLeft);
			this.panelButtons.Location = new System.Drawing.Point(0, 0);
			this.panelButtons.Name = "panelButtons";
			this.panelButtons.Size = new System.Drawing.Size(202, 34);
			this.panelButtons.TabIndex = 0;
			// 
			// bRight
			// 
			this.bRight.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
			this.bRight.ImageIndex = 4;
			this.bRight.ImageList = this.ilButtons;
			this.bRight.Location = new System.Drawing.Point(163, 9);
			this.bRight.Name = "bRight";
			this.bRight.Size = new System.Drawing.Size(29, 17);
			this.bRight.TabIndex = 4;
			this.bRight.TabStop = false;
			this.bRight.Click += new System.EventHandler(this.bRight_Click);
			this.bRight.KeyDown += new System.Windows.Forms.KeyEventHandler(this.bRight_KeyDown);
			// 
			// ilButtons
			// 
			this.ilButtons.ImageSize = new System.Drawing.Size(15, 15);
			this.ilButtons.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("ilButtons.ImageStream")));
			this.ilButtons.TransparentColor = System.Drawing.Color.White;
			// 
			// bStop
			// 
			this.bStop.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
			this.bStop.ImageIndex = 3;
			this.bStop.ImageList = this.ilButtons;
			this.bStop.Location = new System.Drawing.Point(125, 9);
			this.bStop.Name = "bStop";
			this.bStop.Size = new System.Drawing.Size(29, 17);
			this.bStop.TabIndex = 3;
			this.bStop.TabStop = false;
			this.bStop.Click += new System.EventHandler(this.bStop_Click);
			this.bStop.KeyDown += new System.Windows.Forms.KeyEventHandler(this.bStop_KeyDown);
			// 
			// bPlay
			// 
			this.bPlay.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
			this.bPlay.ImageIndex = 2;
			this.bPlay.ImageList = this.ilButtons;
			this.bPlay.Location = new System.Drawing.Point(86, 9);
			this.bPlay.Name = "bPlay";
			this.bPlay.Size = new System.Drawing.Size(29, 17);
			this.bPlay.TabIndex = 2;
			this.bPlay.TabStop = false;
			this.bPlay.Click += new System.EventHandler(this.bPlay_Click);
			this.bPlay.KeyDown += new System.Windows.Forms.KeyEventHandler(this.bPlay_KeyDown);
			// 
			// bPause
			// 
			this.bPause.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
			this.bPause.ImageIndex = 1;
			this.bPause.ImageList = this.ilButtons;
			this.bPause.Location = new System.Drawing.Point(48, 9);
			this.bPause.Name = "bPause";
			this.bPause.Size = new System.Drawing.Size(29, 17);
			this.bPause.TabIndex = 1;
			this.bPause.TabStop = false;
			this.bPause.Click += new System.EventHandler(this.bPause_Click);
			this.bPause.KeyDown += new System.Windows.Forms.KeyEventHandler(this.bPause_KeyDown);
			// 
			// bLeft
			// 
			this.bLeft.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
			this.bLeft.ImageIndex = 0;
			this.bLeft.ImageList = this.ilButtons;
			this.bLeft.Location = new System.Drawing.Point(10, 9);
			this.bLeft.Name = "bLeft";
			this.bLeft.Size = new System.Drawing.Size(28, 17);
			this.bLeft.TabIndex = 0;
			this.bLeft.TabStop = false;
			this.bLeft.Click += new System.EventHandler(this.bLeft_Click);
			this.bLeft.KeyDown += new System.Windows.Forms.KeyEventHandler(this.bLeft_KeyDown);
			// 
			// SlideShow
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.BackColor = System.Drawing.Color.Black;
			this.ClientSize = new System.Drawing.Size(350, 286);
			this.Controls.Add(this.panelButtons);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.Name = "SlideShow";
			this.Text = "SlideShow";
			this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
			this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.SlideShow_KeyDown);
			this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.SlideShow_MouseDown);
			this.Load += new System.EventHandler(this.SlideShow_Load);
			this.MouseHover += new System.EventHandler(this.SlideShow_MouseHover);
			this.Closed += new System.EventHandler(this.SlideShow_Closed);
			this.Paint += new System.Windows.Forms.PaintEventHandler(this.SlideShow_Paint);
			this.Leave += new System.EventHandler(this.SlideShow_Leave);
			this.Enter += new System.EventHandler(this.SlideShow_Enter);
			this.panelButtons.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		private void SlideShow_Paint(object sender, System.Windows.Forms.PaintEventArgs e) {
			SWidth = this.Width;
			SHeight = this.Height;
			panelButtons.Top = 0;
			panelButtons.Left = 0;
			DrawPhoto();
		}

		private void SlideShow_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) {
			AnalyzeKeys(e);
		}

		private void bStop_Click(object sender, System.EventArgs e) {
			this.Close();
		}

		private void bLeft_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) {
			AnalyzeKeys(e);
		}

		private void bPause_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) {
			AnalyzeKeys(e);
		}

		private void bPlay_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) {
			AnalyzeKeys(e);
		}

		private void bStop_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) {
			AnalyzeKeys(e);
		}

		private void bRight_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) {
			AnalyzeKeys(e);
		}

		private void SlideShow_Load(object sender, System.EventArgs e) {
			timer.Enabled = true;
		}

		private void SlideShow_MouseHover(object sender, System.EventArgs e) {
			Cursor.Hide();
			panelButtons.Hide();
		}

		private void SlideShow_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) {
			Cursor.Show();
			panelButtons.Show();
		}

		private void SlideShow_Leave(object sender, System.EventArgs e) {
			Cursor.Show();
			if (timer.Enabled == true)
				timer.Stop();
		}

		private void SlideShow_Enter(object sender, System.EventArgs e) {
			if (timer.Enabled == true)
				timer.Start();
		}

		private void SlideShow_Closed(object sender, System.EventArgs e) {
			Cursor.Show();
		}

		private void timer_Tick(object sender, System.EventArgs e) {
			NextPhoto();
		}

		private void bPlay_Click(object sender, System.EventArgs e) {
			timer.Enabled = true;
		}

		private void bPause_Click(object sender, System.EventArgs e) {
			timer.Enabled = false;
		}

		private void bLeft_Click(object sender, System.EventArgs e) {
			PreviousPhoto();
		}

		private void bRight_Click(object sender, System.EventArgs e) {
			NextPhoto();
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -