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

📄 photospane.cs

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


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

namespace VirtualPhotoOrganizer.Controls
{
	/// <summary>
	/// main component used for displaying an album's thumbnails
	/// </summary>
	internal class PhotosPane : System.Windows.Forms.UserControl
	{
		// the album, we're currently browsing
		private Album _Album;

		// our events
		public delegate void ProgressUpdate(int progress, int max, string desc);
		public event ProgressUpdate OnProgressUpdate;
		public delegate void SelectionChanged(Photos selPhotos);
		public event SelectionChanged SelectedPhotosChanged;
		public delegate void AlbumLoad(int photos);
		public event AlbumLoad AlbumLoaded;
		public delegate void PhotoDelete(int photosCount);
		public event PhotoDelete PhotosDeleted;

		// selected photos
		private Photos SelPhotos;
		private bool Deleting = false;

		// language strings
		private string LsNoPhoto;
		private string LsLThumb;
		private string LsDeleteQ;
		private string LsDel;
		private string LsRot;
		private string LsNoAlbumSave;
		private string LsPixels;
		private string LsArrChroErr;

		// settings
		private int PSaverQ = 100;
		private int ThumbWidth = 150;
		private int ThumbHeight = 113;
		private bool ShowImgRes = true;

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

		// temp storage for a photo's title during editing
		private string TitleBuffer;

		// the graphics object needed for previewing the drag&drop destination
		private Graphics GlvPhotos;

		// the distance between two items - needed for locating the destIndex for a drag&drop operation
		private int ItemOffset;

		// the current mouse pos
		private Point MousePos;

		// the index of the item the mouse is currently hovering over
		private int CurrItemIndex;

		// the pen needed for previewing drag&drop
		private Pen PPrev;

		// indicates whether Ctrl has been pressed or not
		private bool CtrlDown = false;

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

		// import the methods necessary for setting the scroll bar to the correct position after dd
		[DllImport("User32.dll")]
		public extern static bool GetScrollInfo(IntPtr hwnd, int fnBar, ref ScrollInfo lpsi);

		[DllImport("User32.dll")]
		public extern static int SetScrollInfo(IntPtr hwnd, int fnBar, ref ScrollInfo lpsi, bool fRedraw);

		public struct ScrollInfo
		{
			public uint cbSize;
			public uint fMask;
			public int nMin;
			public int nMax;
			public uint nPage;
			public int nPos;
			public int nTrackPos;
		}

		private System.Windows.Forms.ListView lvPhotos;
		private VirtualPhotoOrganizer.Controls.PaneTitle paneTitle;
		private System.Windows.Forms.ImageList ilThumbs;
		private System.Windows.Forms.ContextMenu contMenu;
		private System.Windows.Forms.MenuItem miOpen;
		private System.Windows.Forms.MenuItem miSlideSh;
		private System.Windows.Forms.MenuItem menuItem3;
		private System.Windows.Forms.MenuItem miRLeft;
		private System.Windows.Forms.MenuItem miRRight;
		private System.Windows.Forms.MenuItem menuItem6;
		private System.Windows.Forms.MenuItem miDel;
		private System.Windows.Forms.MenuItem miRen;
		private System.Windows.Forms.MenuItem menuItem9;
		private System.Windows.Forms.MenuItem miPInfo;
		private System.Windows.Forms.MenuItem miSelAll;
		private System.Windows.Forms.MenuItem miHTMLExp;
		private System.Windows.Forms.ToolTip ttPhotoInfo;
		private System.Windows.Forms.Timer timerToolTip;
		private System.Windows.Forms.MenuItem miPrint;
		private System.Windows.Forms.MenuItem menuItem1;
		private System.Windows.Forms.MenuItem miRenTime;
		private System.Windows.Forms.MenuItem miReoAZ;
		private System.Windows.Forms.MenuItem miReoZA;
		private System.Windows.Forms.MenuItem miReoChro;
		private System.Windows.Forms.MenuItem miRenReo;
		private System.Windows.Forms.MenuItem menuItem2;
		private System.Windows.Forms.MenuItem miCopy;
		private System.Windows.Forms.MenuItem miCut;
		private System.Windows.Forms.MenuItem miPaste;
		private System.ComponentModel.IContainer components;

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

//			LoadLanugageStrings();
//			LoadSettings();
			SelPhotos = new Photos();
			MousePos = new Point(0, 0);
			CurrItemIndex = -1;
			VPOClipboard.ClipboardChanged += new VirtualPhotoOrganizer.Photo.VPOClipboard.ChangedClipb(VPOClipboard_ClipboardChanged);
		}

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

		#region ParseAlbum

		/// <summary>
		/// parses the _Album and adds each thumb to the lv
		/// </summary>
		private void ParseAlbum() {
			if (_Album != null) {
				// prevent lvPhotos from redrawing itself
				lvPhotos.BeginUpdate();

				paneTitle.Text = _Album.AlbumName;	// set the pane title to the album name
				ilThumbs.Images.Clear();	// clear the image list
				lvPhotos.Items.Clear();		// clear the photos lv
				int i = 0;
				int max = _Album.Photos.Count;
				Photos notFound = new Photos();	// the photos that cannot be found
				bool saveAlbum = false;			// do we have to save our album after?

				// add each photo to the il and to the lv
				foreach (Photo.Photo p in _Album.Photos) {
					try		// to add the thumbnail to the il and the lv
					{
						Image img = Bitmap.FromFile(p.Thumbnail);
						ilThumbs.Images.Add(img);
						lvPhotos.Items.Add(p.Title, i);
						p.Tag = ilThumbs.Images[i];
						img.Dispose();

						// if the timeTaken has not yet been saved in the album, get the time and set the saveAlbum var to true
						if (p.TimeTaken == "") {
							FileInfo fi = new FileInfo(p.Path);
							p.TimeTaken = fi.LastWriteTime.ToString();
							saveAlbum = true;
						}
					}
					catch {
						try {
							// create a new thumbnail and add that one
							Resizer r = new Resizer();
							r.CreateThumbnail(p.Path, p.Thumbnail);
							Image img = Bitmap.FromFile(p.Thumbnail);
							ilThumbs.Images.Add(img);
							img.Dispose();
							lvPhotos.Items.Add(p.Title, i);
							p.Tag = ilThumbs.Images[i];

							// if the timeTaken has not yet been saved in the album, get the time and set the saveAlbum var to true
							if (p.TimeTaken == "") {
								FileInfo fi = new FileInfo(p.Path);
								p.TimeTaken = fi.LastWriteTime.ToString();
								saveAlbum = true;
							}
						}
						catch {
							// inform the user that the photo doesn't exist and remove it from the album
							MessageBox.Show(LsNoPhoto, p.Path, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
							notFound.Add(p);
							max--;
							i--;
							saveAlbum = true;
						}
					}
					finally {
						OnProgressUpdate(++i, max, LsLThumb);
					}
				}

				// reset the status bar to its initial state
				OnProgressUpdate(0, 100, "");

				// fire the albumLoaded event
				AlbumLoaded(max);

				// remove each photo that was not found from the album
				foreach (Photo.Photo p in notFound)
					_Album.Photos.Remove(p);

				// save the album if necessarry
				if (saveAlbum == true) {
					try {
						_Album.SaveAlbum();
					}
					catch { }
				}

				// redraw lvPhotos and update the scrollInfo
				lvPhotos.EndUpdate();
				ScrollInfo si = new ScrollInfo();
				si.cbSize = (uint) Marshal.SizeOf(si);
				si.fMask = 0x0004;
				GetScrollInfo(lvPhotos.Handle, 0x1, ref si);
				while (si.nPos > si.nMin) {
					SendMessage(lvPhotos.Handle, 0x0115, 0, 0);
					GetScrollInfo(lvPhotos.Handle, 0x1, ref si);
				}

				// get the ItemOffset
				try { ItemOffset = (int) Math.Round((double) (lvPhotos.Items[1].Bounds.Left - lvPhotos.Items[0].Bounds.Right), 0); }
				catch { ItemOffset = 10; }
			}
		}

		/// <summary>
		/// reloads the photos (excl. the thumbs) after a drag&drop operation in rearranged order
		/// </summary>
		private void ReloadAlbum() {
			lvPhotos.BeginUpdate();
			lvPhotos.Items.Clear();
			int i = 0;
			foreach (Photo.Photo p in this.Album.Photos) {
				lvPhotos.Items.Add(p.Title, i);
				p.Tag = ilThumbs.Images[i];
				i++;
			}
			lvPhotos.EndUpdate();
		}

		public void ReloadAlbumName() {
			paneTitle.Text = _Album.AlbumName;
		}

		#endregion

		#region renaming & rearranging

		/// <summary>
		/// renames the photos according to their timeTaken
		/// </summary>
		private void RenamePhotosAccToTime() {
			DialogResult dlgRes = SaveAlbum(MessageBoxButtons.AbortRetryIgnore);
			switch (dlgRes) {
				case DialogResult.Abort: return;
				case DialogResult.Retry:
					RenamePhotosAccToTime();
					return;
					break;
				default:
					break;
			}

			if (SelPhotos.Count == 0) {
				foreach (Photo.Photo p in _Album.Photos) {
					p.Title = p.TimeTaken;
					lvPhotos.Items[_Album.Photos.IndexOf(p)].Text = p.Title;
				}
			} else {
				foreach (Photo.Photo p in SelPhotos) {
					p.Title = p.TimeTaken;
					lvPhotos.Items[_Album.Photos.IndexOf(p)].Text = p.Title;
				}
			}

			_Album.SaveAlbum();
		}

		/// <summary>
		/// rearranges the photos alphabetically (a->z)
		/// </summary>
		private void RearrangePhotosAZ() {
			// check if the album can be saved
			DialogResult dlgRes = SaveAlbum(MessageBoxButtons.AbortRetryIgnore);
			switch (dlgRes) {
				case DialogResult.Abort: return;
				case DialogResult.Retry:
					RearrangePhotosAZ();
					return;
					break;
				default:
					break;
			}

			int start;
			int w;
			Photo.Photo temp;
			for (int i = 0; i < _Album.Photos.Count; i++) {
				start = i + 1;
				while (start < _Album.Photos.Count) {
					w = _Album.Photos[i].CompareTo(_Album.Photos[start]);

					if (w > 0) {
						temp = _Album.Photos[i];
						_Album.Photos[i] = _Album.Photos[start];
						_Album.Photos[start] = temp;
					}
					start++;
				}
			}

			// update the il
			ImageCollection icTemp = new ImageCollection();
			foreach (Photo.Photo p in _Album.Photos)
				icTemp.Add((Image) p.Tag);
			ilThumbs.Images.Clear();
			foreach (Image i in icTemp)
				ilThumbs.Images.Add(i);

			// save the album and update the lv
			_Album.SaveAlbum();
			ReloadAlbum();
		}

		/// <summary>
		/// rearranges the photos alphabetically (z->a)
		/// </summary>
		private void RearrangePhotosZA() {
			// check if the album can be saved
			DialogResult dlgRes = SaveAlbum(MessageBoxButtons.AbortRetryIgnore);
			switch (dlgRes) {
				case DialogResult.Abort: return;
				case DialogResult.Retry:
					RearrangePhotosAZ();
					return;
					break;
				default:
					break;
			}

			int start;
			int w;
			Photo.Photo temp;
			for (int i = 0; i < _Album.Photos.Count; i++) {
				start = i + 1;
				while (start < _Album.Photos.Count) {
					w = _Album.Photos[i].CompareTo(_Album.Photos[start]);

					if (w < 0) {
						temp = _Album.Photos[i];
						_Album.Photos[i] = _Album.Photos[start];
						_Album.Photos[start] = temp;
					}
					start++;
				}
			}

			// update the il
			ImageCollection icTemp = new ImageCollection();
			foreach (Photo.Photo p in _Album.Photos)
				icTemp.Add((Image) p.Tag);
			ilThumbs.Images.Clear();
			foreach (Image i in icTemp)
				ilThumbs.Images.Add(i);

			// save the album and update the lv
			_Album.SaveAlbum();
			ReloadAlbum();
		}

		/// <summary>
		/// rearranges the photos in chronological order
		/// </summary>
		private void RearrangePhotosChro() {
			// check if the album can be saved
			DialogResult dlgRes = SaveAlbum(MessageBoxButtons.AbortRetryIgnore);
			switch (dlgRes) {
				case DialogResult.Abort: return;
				case DialogResult.Retry:
					RearrangePhotosAZ();
					return;
					break;
				default:
					break;
			}

			int start;
			int w;
			Photo.Photo temp;
			DateTime timeI, timeStart;
			for (int i = 0; i < _Album.Photos.Count; i++) {
				try { timeI = DateTime.Parse(_Album.Photos[i].TimeTaken); }
				catch {
					MessageBox.Show(LsArrChroErr, _Album.Photos[i].Title, MessageBoxButtons.OK, MessageBoxIcon.Error);
					_Album.SaveAlbum();
					ReloadAlbum();
					return;
				}

				start = i + 1;
				while (start < _Album.Photos.Count) {
					try { timeStart = DateTime.Parse(_Album.Photos[start].TimeTaken); }
					catch {
						MessageBox.Show(LsArrChroErr, _Album.Photos[start].Title, MessageBoxButtons.OK, MessageBoxIcon.Error);
						_Album.SaveAlbum();
						ReloadAlbum();
						return;
					}

					w = timeI.CompareTo(timeStart);

					if (w > 0) {
						temp = _Album.Photos[i];
						_Album.Photos[i] = _Album.Photos[start];

⌨️ 快捷键说明

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