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

📄 albumspane.cs

📁 这是一个小型的相片管理器
💻 CS
📖 第 1 页 / 共 3 页
字号:

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

namespace VirtualPhotoOrganizer.Controls
{
	/// <summary>
	/// Zusammenfassung f黵 AlbumsPane.
	/// </summary>
	internal class AlbumsPane : System.Windows.Forms.UserControl
	{
		// event albumOpen
		public delegate void AlbumOpen(Album a);
		public event AlbumOpen OpenAlbum;
		public delegate void AddPhotos(Album a);
		public event AddPhotos AddedPhotos;
		public delegate void AlbumNameChange(Album a);
		public event AlbumNameChange AlbumNameChanged;
		public delegate void AlbumDelete(Album a);
		public event AlbumDelete AlbumDeleted;
		public delegate void EnterPhotos();
		public event EnterPhotos PhotosEnter;
		public delegate void DropPhotos(Album a);
		public event DropPhotos PhotosDropped;

		//the RootAlbum that we import our albums from
		RootAlbum RAlbum;

		// global language strings
		private string LsWarning;
		private string LsConfirmDel;
		private string LsPhotos;

		// the currently selected album
		private Album SelAlbum;

		// indicated if we're currently editing an album's title
		private bool Editing = false;

		// temp buffer for an album's title during editing
		private string TitleBuffer;

		// the current mouse pos
		private Point CurrMPos;

		// indicates if lv_AfterSelect event is supposed to update SelAlbum
		private bool DontUpdateSelAlbum = false;
		private bool ForceSelAlbumUpdate = false;

		// import the method necessary for scrolling during dd
		[System.Runtime.InteropServices.DllImport("User32.dll")]
		public extern static int SendMessage(IntPtr hwnd, uint message, int wparam,
			int lparam);

		private System.Windows.Forms.TreeView tvAlbums;
		private VirtualPhotoOrganizer.Controls.PaneTitle paneTitle;
		private System.Windows.Forms.ColumnHeader chAlbumN;
		private System.Windows.Forms.ColumnHeader chPhotos;
		private System.Windows.Forms.ContextMenu contMenu;
		private System.Windows.Forms.MenuItem miAddAlbum;
		private System.Windows.Forms.MenuItem miAddPhotos;
		private System.Windows.Forms.MenuItem miFiles;
		private System.Windows.Forms.MenuItem miFolder;
		private System.Windows.Forms.MenuItem menuItem5;
		private System.Windows.Forms.MenuItem miDel;
		private System.Windows.Forms.MenuItem miRen;
		private System.Windows.Forms.MenuItem menuItem1;
		private System.Windows.Forms.MenuItem miProp;
		private System.Windows.Forms.ToolTip ttAlbInfo;
		private System.Windows.Forms.Timer timerToolTip;
		private System.Windows.Forms.MenuItem miExpAll;
		private System.Windows.Forms.MenuItem miColAll;
		private System.Windows.Forms.MenuItem menuItem4;
		private System.Windows.Forms.Button bUp;
		private System.Windows.Forms.ImageList ilButtons;
		private System.Windows.Forms.Button bDown;
		private System.Windows.Forms.MenuItem miUp;
		private System.Windows.Forms.MenuItem miDown;
		private System.Windows.Forms.MenuItem menuItem6;
		private System.ComponentModel.IContainer components;

		public AlbumsPane() {
			// Dieser Aufruf ist f黵 den Windows Form-Designer erforderlich.
			InitializeComponent();

			CurrMPos = new Point(0, 0);
			ReloadRootAlbum();
		}

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

		#region accessors

		internal RootAlbum RootAlbum {
			get { return RAlbum; }
		}
		internal Album SelectedAlbum {
			get { return SelAlbum; }
		}
		internal void FireOpenAlbum(Album a) {
			OpenAlbum(a);
		}

		#endregion

		/// <summary>
		/// Refreshes the AlbumView
		/// </summary>
		public void ReloadRootAlbum() {
			RAlbum = new RootAlbum();
			ParseRootAlbum();
		}

		#region AddAlbum

		/// <summary>
		/// Shows the dialog used for adding an album to the RootAlbum
		/// </summary>
		public void AddAlbum() {
			Dialogs.AddAlbumDialog aad = new VirtualPhotoOrganizer.Dialogs.AddAlbumDialog();
			if (aad.ShowDialog() == DialogResult.OK) {
				// load the two important strings into vars
				string aPath = aad.AlbumPath;
				string aName = aad.AlbumName;

				// check if the dir exists
				if (!Directory.Exists(aPath))
					Directory.CreateDirectory(aPath);

				try {
					// create the new album and save it
					Album a = new Album(aName, aPath + '\\' + aName + ".vpoa");
					a.SaveAlbum();

					TreeNode tn = new TreeNode(a.AlbumName);
					tn.Tag = a;
					if (SelAlbum != null) {
						// add the album to the selected parent album and save it
						Album parent = (Album) tvAlbums.SelectedNode.Tag;
						parent.SubAlbums.Add(a);
						parent.SaveAlbum();

						// add the album to the tv and expand the selected node
						tvAlbums.SelectedNode.Nodes.Add(tn);
						tn.EnsureVisible();
					} else {
						// add the album to the root file and save that one
						RAlbum.Albums.Add(a);
						RAlbum.SaveRootAlbum();

						// add the album to the tv
						tvAlbums.Nodes.Add(tn);
					}
					tvAlbums.SelectedNode = tn;

				}
				catch (Exception e) {
					MessageBox.Show(e.Message);
				}
			}
		}

		#endregion

		#region OpenAlbum from file

		/// <summary>
		/// adds the selected album to the rootAlbum if necessary and opens it in the photosPane
		/// </summary>
		public void OpenAlbumFromFile(Album a) {
			// add the album to the rootAlbum (if necessarry)
			RootAlbum ra = new RootAlbum();
			bool addToRA = true;
			TreeNode destNode;
			Album album;
			foreach (TreeNode tn in tvAlbums.Nodes) {
				album = (Album) tn.Tag;
				if (album.AlbumPath == a.AlbumPath) {
					addToRA = false;
					destNode = tn;
				} else {
					destNode = ParseForAlbum(tn.Nodes, a);
					if (destNode != null)
						addToRA = false;
				}

				if (addToRA == false) {
					// open the new album in the photosPane
					tvAlbums.SelectedNode = destNode;
					destNode.EnsureVisible();
					this.OpenAlbum(SelAlbum);
					return;
				}
			}

			if (addToRA == true) {
				ra.Albums.Add(a);
				ra.SaveRootAlbum();
				destNode = new TreeNode(a.AlbumName);
				destNode.Tag = a;
				ParseSubAlbums(a, destNode);
				tvAlbums.Nodes.Add(destNode);

				// open the new album in the photosPane
				tvAlbums.SelectedNode = destNode;
				destNode.EnsureVisible();
				this.OpenAlbum(SelAlbum);

			}
		}

		private TreeNode ParseForAlbum(TreeNodeCollection nodes, Album a) {
			Album album;
			TreeNode returnValue;
			foreach (TreeNode tn in nodes) {
				// check if the current node contains the album
				album = (Album) tn.Tag;
				if (album.AlbumPath == a.AlbumPath)
					return tn;

				// parse its subnodes
				returnValue = ParseForAlbum(tn.Nodes, a);
				if (returnValue != null)
					return returnValue;
			}

			// if we didn't find anything return null
			return null;
		}

		#endregion

		#region DeleteAlbum

		/// <summary>
		/// Deletes the currently selected album and all its subalbums
		/// </summary>
		public void DeleteAlbum() {
			if (SelAlbum != null) {
				if (MessageBox.Show(LsConfirmDel, LsWarning, MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes) {
					ForceSelAlbumUpdate = true;
					// remove the album from the root album if necessary
					if (tvAlbums.SelectedNode.Parent == null) {
						RAlbum.Albums.Remove(SelAlbum);
						RAlbum.SaveRootAlbum();
					} else {
						try {
							Album a = (Album) tvAlbums.SelectedNode.Parent.Tag;
							a.SaveAlbum();
							a.SubAlbums.Remove(SelAlbum);
							a.SaveAlbum();
						}
						catch (Exception e) {
							MessageBox.Show(e.Message);
							return;
						}
					}

					// delete the album
					DeleteAlbum(SelAlbum);
					tvAlbums.SelectedNode.Remove();
					AdjustButtons();
				}
			}
		}

		/// <summary>
		/// deletes the specified album
		/// </summary>
		private void DeleteAlbum(Album a) {
			// necessary for deleting the empty dirs
			DirectoryInfo di;
			FileInfo[] files;

			// delete the album's subalbums
			foreach (Album suba in a.SubAlbums)
				DeleteAlbum(suba);

			// delete the specified album and all files in it
			// delete each photo and its thumbnail
			foreach (Photo.Photo p in a.Photos) {
				try {
					File.Delete(p.Path);
					File.Delete(p.Thumbnail);
				}
				catch (Exception e) {
					MessageBox.Show(e.Message);
				}
			}

			try {
				// check if the thumbs dir is empty and delete it, if it is
				di = new DirectoryInfo(GetDirectoryFromFilePath(a.AlbumPath) + "\\Thumbnails");
				files = di.GetFiles();
				if (files.Length == 0)
					di.Delete();
			}
			catch { }

			// delete the album file itself, check if the album dir is empty and delete it, if it is
			di = new DirectoryInfo(GetDirectoryFromFilePath(a.AlbumPath));
			File.Delete(a.AlbumPath);
			files = di.GetFiles();
			string s = di.FullName;
			if (files.Length == 0)
				di.Delete();

			AlbumDeleted(a);
		}

		#endregion

		#region RenameAlbum

		/// <summary>
		/// Begins the renaming of the selected album
		/// </summary>
		public void RenameAlbum() {
			tvAlbums.SelectedNode.BeginEdit();
		}

		#endregion

		#region AddPhotos

		/// <summary>
		/// Shows an openFileDialog and adds the selected photos to the selected album
		/// </summary>
		public void AddPhotosFromFiles() {
			PhotosAdder pa = new PhotosAdder();
			if (pa.AddPhotosFromFiles(SelAlbum) == true)
				AddedPhotos(SelAlbum);
		}

		/// <summary>
		/// Shows a FolderBrowserDialog and adds all photos from the selected folder to the selected album
		/// </summary>
		public void AddPhotosFromFolder() {
			PhotosAdder pa = new PhotosAdder();
			if (pa.AddPhotosFromDirectory(SelAlbum) == true)
				AddedPhotos(SelAlbum);
		}

		#endregion

		#region ParseNode

		/// <summary>
		/// parses the given treeNode for sub album changes and then saves the album
		/// </summary>
		private void ParseNode(TreeNode node) {
			try {
				Album parent = (Album) node.Tag;
				parent.SubAlbums.Clear();	// clear the parent's subalbums
				foreach (TreeNode tn in node.Nodes)	// parse the treeNode and readd the subalbums in correct order
					parent.SubAlbums.Add((Album) tn.Tag);
				parent.SaveAlbum();
			}
			catch (Exception e) {
				MessageBox.Show(e.Message);
			}
		}

		/// <summary>
		/// parses the given TreeNodeCollection for album changes and then saves the rootalbum
		/// </summary>
		/// <param name="node"></param>
		private void ParseNode(TreeNodeCollection nodes) {
			try {
				RAlbum.Albums.Clear();	// clear the rootAlbum's albums
				foreach (TreeNode tn in nodes)	// parse the treeNode and readd the albums in correct order
					RAlbum.Albums.Add((Album) tn.Tag);
				RAlbum.SaveRootAlbum();
			}
			catch (Exception e) {
				MessageBox.Show(e.Message);
			}
		}

		#endregion

⌨️ 快捷键说明

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