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

📄 clipboard.cs

📁 这是一个小型的相片管理器
💻 CS
字号:
using System;
using System.IO;
using System.Windows.Forms;
using VirtualPhotoOrganizer.Dialogs;
using VirtualPhotoOrganizer.Util;
using TXML;

namespace VirtualPhotoOrganizer.Photo
{
	/// <summary>
	/// class used to store photos for copying/moving actions
	/// </summary>
	internal class VPOClipboard
	{
		public enum Action
		{
			Copy,
			Move
		}

		private static Photos _Photos;		// the photos we want to copy/move
		private static Album _SrcAlbum;		// the album they come from
		private static Action _Action;		// the action we'll take

		public delegate void UpdateProgress(string action, int pos, int max);
		public static event UpdateProgress ProgressUpdate;
		public delegate void ChangedClipb(int count);
		public static event ChangedClipb ClipboardChanged;

		private static System.Collections.ArrayList DontDel = new System.Collections.ArrayList();
		private static bool PermUseThisAction = false;
		private static bool LoadedStrings = false;
		private static string LsCopy;
		private static string LsMove;
		private static string LsSameAlbum;

		// public methods
		/// <summary>
		/// sets up the clipboard to copy the specified photos
		/// </summary>
		public static void CopyToClipboard(Photos photos, Album srcAlbum) {
			_Photos = photos;
			_SrcAlbum = srcAlbum;
			_Action = Action.Copy;
			ClipboardChanged(photos.Count);
		}

		/// <summary>
		/// sets up the clipboard to move the specified photos
		/// </summary>
		public static void CutToClipboard(Photos photos, Album srcAlbum) {
			_Photos = photos;
			_SrcAlbum = srcAlbum;
			_Action = Action.Move;
			ClipboardChanged(photos.Count);
		}

		/// <summary>
		/// copies the selected photos to the specified album
		/// </summary>
		public static void CopyToAlbum(Album album, int pos) {
			DontDel.Clear();
			if (LoadedStrings == false)
				LoadStrings();
			Photos photos = (Photos) _Photos.Clone();
			string destPath = AlbumCreator.GetDirectoryFromFilePath(album.AlbumPath) + '\\';
			bool dontAdd = false;
			PhotoExistsDialog pExDlg = null;
			DialogResult drAction = new DialogResult();
			PermUseThisAction = false;
			int max = photos.Count;
			int i = 0;
			string action;
			string pName;
			string thumb;
			FileInfo fi;

			if (_Action == Action.Copy)
				action = LsCopy;
			else
				action = LsMove;

			ProgressUpdate(action, 0, max);

			foreach (Photo p in photos) {
				fi = new FileInfo(p.Path);
				pName = fi.Name;
				thumb = "Thumbnails\\th_" + pName;
				if (!Directory.Exists(destPath + "Thumbnails"))
					Directory.CreateDirectory(destPath + "Thumbnails");
				if (!File.Exists(destPath + pName)) {
					try {
						File.Copy(p.Path, destPath + pName, true);
						File.Copy(p.Thumbnail, destPath + thumb, true);
						dontAdd = false;
					}
					catch (Exception e) {
						System.Windows.Forms.MessageBox.Show(e.Message);
						dontAdd = true;
					}
				} else {
					if (PermUseThisAction == false) {
						if (pExDlg == null)
							pExDlg = new PhotoExistsDialog();
						pExDlg.PhotoPath = destPath + pName;
						pExDlg.CheckedUseThis += new VirtualPhotoOrganizer.Dialogs.PhotoExistsDialog.UseThisChecked(pExDlg_CheckedUseThis);
						drAction = pExDlg.ShowDialog();
						pExDlg.CheckedUseThis -= new VirtualPhotoOrganizer.Dialogs.PhotoExistsDialog.UseThisChecked(pExDlg_CheckedUseThis);
					}

					switch (drAction) {
						case DialogResult.Yes:	// the user wishes to overwrite the file
							int pdelIndex;
							FileInfo fi2;
							foreach (Photo pdel in album.Photos) {
								fi2 = new FileInfo(pdel.Path);
								if (fi2.Name == pName) {
									pdelIndex = album.Photos.IndexOf(pdel);
									album.Photos.RemoveAt(pdelIndex);
									if (pdelIndex < pos)
										pos--;
									break;
								}
							}
							try {
								File.Copy(p.Path, destPath + pName, true);
								File.Copy(p.Thumbnail, destPath + thumb, true);
								dontAdd = false;
							}
							catch (Exception e) {
								MessageBox.Show(e.Message);
								dontAdd = true;
							}
							break;
						case DialogResult.No:	// the user does not wish to overwrite (remove the current photo from the list)
							dontAdd = true;
							break;
						case DialogResult.Retry:	// the user wants the program to automatically rename the new file, so that it can be added  to the album
							int j = 0;
							while (File.Exists(destPath + pName)) {
								j++;
								pName = fi.Name.Substring(0, fi.Name.Length - fi.Extension.Length) + '_' + j.ToString() + fi.Extension;
							}
							try {
								File.Copy(p.Path, destPath + pName, true);
								File.Copy(p.Thumbnail, destPath + "Thumbnails\\th_" + pName, true);
								dontAdd = false;
							}
							catch (Exception e) {
								System.Windows.Forms.MessageBox.Show(e.Message);
								dontAdd = true;
							}
							break;
					}

					ProgressUpdate(action, ++i, max);
				}

				if (dontAdd == false) {
					p.Path = destPath + pName;
					p.Thumbnail = destPath + "Thumbnails\\th_" + pName;
					album.Photos.Insert(pos++, p);
				} else
					DontDel.Add(i);
			}

			album.SaveAlbum();
			ProgressUpdate("", 0, 100);
		}

		/// <summary>
		/// moves the selected photos to the specified album
		/// </summary>
		public static void MoveToAlbum(Album album, int pos) {
			if (album.AlbumPath == _SrcAlbum.AlbumPath) {
				MessageBox.Show(LsSameAlbum);
				return;
			}
			CopyToAlbum(album, pos);
			bool dontDel;
			for (int i = 0; i < _Photos.Count; i++) {
				dontDel = false;
				foreach (int j in DontDel) {
					if (j == i) {
						dontDel = true;
						DontDel.Remove(j);
						break;
					}
				}
				if (dontDel == false) {
					try {
						int index = _SrcAlbum.Photos.IndexOf(_Photos[i]);
						File.Delete(_SrcAlbum.Photos[index].Path);
						File.Delete(_SrcAlbum.Photos[index].Thumbnail);
						_SrcAlbum.Photos.Remove(_Photos[i]);
					}
					catch { }
				}
			}
			_Photos.Clear();
			_SrcAlbum.SaveAlbum();
			ClipboardChanged(0);
			ProgressUpdate("", 0, 100);
		}

		public static void LoadStrings() {
			try {
				TXmlReader reader = XmlHandler.OpenLangFile();

				LsCopy = reader.GetString("Clipboard", "Copy", "Copying...");
				LsMove = reader.GetString("Clipboard", "Move", "Moving...");
				LsSameAlbum = reader.GetString("Clipboard", "SameAlbum", "The photos cannot be moved, because the destination and the source album are identical!");

				reader.Close();
			}
			catch {
				LsCopy = "Copying...";
				LsMove = "Moving...";
				LsSameAlbum = "The photos cannot be moved, because the destination and the source album are identical!";
			}

			LoadedStrings = true;
		}

		// accessors
		public static Photos Photos {
			get { return _Photos; }
		}
		public static Album SrcAlbum {
			get { return _SrcAlbum; }
		}
		public static Action ClipboardAction {
			get { return _Action; }
		}

		private static void pExDlg_CheckedUseThis(bool isChecked) {
			PermUseThisAction = isChecked;
		}
	}
}

⌨️ 快捷键说明

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