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

📄 frmpiclib.cs

📁 个人信息的源代码
💻 CS
📖 第 1 页 / 共 4 页
字号:
				| System.Windows.Forms.AnchorStyles.Right)));
			this.txtPicNotes.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.txtPicNotes.HideSelection = false;
			this.txtPicNotes.Location = new System.Drawing.Point(264, 64);
			this.txtPicNotes.Name = "txtPicNotes";
			this.txtPicNotes.ShowContextMenu = false;
			this.txtPicNotes.Size = new System.Drawing.Size(360, 96);
			this.txtPicNotes.TabIndex = 1;
			this.txtPicNotes.Text = "";
			this.txtPicNotes.TextChanged += new System.EventHandler(this.txtPicNotes_TextChanged);
			this.txtPicNotes.Leave += new System.EventHandler(this.txtPicNotes_Leave);
			// 
			// lstFiles
			// 
			this.lstFiles.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left)));
			this.lstFiles.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.lstFiles.HorizontalScrollbar = true;
			this.lstFiles.ItemHeight = 12;
			this.lstFiles.Location = new System.Drawing.Point(24, 64);
			this.lstFiles.Name = "lstFiles";
			this.lstFiles.Size = new System.Drawing.Size(224, 110);
			this.lstFiles.TabIndex = 0;
			this.lstFiles.SelectedIndexChanged += new System.EventHandler(this.lstFiles_SelectedIndexChanged);
			// 
			// label2
			// 
			this.label2.AutoSize = true;
			this.label2.BackColor = System.Drawing.Color.Transparent;
			this.label2.Location = new System.Drawing.Point(264, 44);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(66, 17);
			this.label2.TabIndex = 2;
			this.label2.Text = "图片说明:";
			// 
			// saveFileDialog1
			// 
			this.saveFileDialog1.Filter = "*.*|*.*";
			this.saveFileDialog1.RestoreDirectory = true;
			// 
			// openFileDialog1
			// 
			this.openFileDialog1.Filter = "支持的图像类型|*.jpg;*.bmp;*.ico;*.wmf;*.gif;*.tif;*.png|所有文件|*.*";
			this.openFileDialog1.Multiselect = true;
			this.openFileDialog1.RestoreDirectory = true;
			// 
			// frmPicLib
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(640, 406);
			this.Controls.Add(this.pnlPicLib);
			this.Name = "frmPicLib";
			this.Text = "frmPicLib";
			this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
			this.pnlPicLib.ResumeLayout(false);
			this.panel1.ResumeLayout(false);
			this.tabControl1.ResumeLayout(false);
			this.tbpPictureView.ResumeLayout(false);
			this.pnlPicView.ResumeLayout(false);
			this.tbpAlbumsDescription.ResumeLayout(false);
			this.panel2.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		#region 变量区

		private AttachFileAccessObj FileSaver;

		private PicLibAccessObj PicLibSaver;

		private MemoryStream picMem = new MemoryStream();

		public frmPicLib(PicLibAccessObj PicLibSaver)
		{
			InitializeComponent();
			this.PicLibSaver = PicLibSaver;
			this.FileSaver = new AttachFileAccessObj(PicLibSaver.dbObj);
		}

		private PicLib _PicLib = null;

		public PicLib PicLibObject
		{
			get
			{
				return this._PicLib;
			}
			set
			{
				if(this._PicLib != null)
				{
					this.UpdateDB();
				}
				this._PicLib = value;
				this.ShowObjInForm();
			}
		}
		
		//上次选择的文件索引
		private int LastFileIndex = -1;

		#endregion

		#region 系统功能代码

		/// <summary>
		/// 清空所有输出框
		/// </summary>
		public void ClearInput()
		{
			this.txtComment.Text = "";
			this.lstFiles.Items.Clear();
			this.tbtnAddPic.Enabled = true;
			this.tbtnDelPic.Enabled = false;
			this.tbtnExportPic.Enabled = false;
		}
		/// <summary>
		/// 将文件名显示在ListBox中,同时装入FileID
		/// </summary>
		private void ShowObjInForm()
		{
			if(this._PicLib.RTFText.Trim() == string.Empty)
				this.txtComment.Text = this._PicLib.Text;
			else
				this.txtComment.Rtf = this._PicLib.RTFText;

			//必须调用自定义的Clear过程,以同步清除所有的Tag对象
			this.lstFiles.Clear();
			
			Attachment file;
			for(int i=0; i<this._PicLib.AttachFiles.Count; i++)
			{
				file = this._PicLib.AttachFiles[i] as Attachment;
				this.lstFiles.Add(Path.GetFileName(file.FileName), file);
			}
			//显示第一幅图像(如果有的话)
			if(this._PicLib.AttachFiles.Count > 0)
			{
				file = this._PicLib.AttachFiles[0] as Attachment;
				try
				{
					this.pictureBox1.Image = this.PicLibSaver.GetPicFromDB(file);
				}
				catch(NotImageTypeException ex)
				{
					MessageBox.Show(ex.Message, "出错信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
					this.pictureBox1.Image = null;//清空图片框
					return;
				}
				this.lstFiles.SelectedIndex = 0;
			}
			else
			{
				this.pictureBox1.Image = null;//清空图片框
			}
		}
		/// <summary>
		/// 增加图片文件,同时保存到数据库中
		/// </summary>
		private void AddPicFile()
		{
			string FileName;
			Attachment fileObj;
			if(this.openFileDialog1.ShowDialog() == DialogResult.OK)
			{
				FileName = this.openFileDialog1.FileName;
				if(FileName.Trim() == string.Empty)
					return;

				try
				{
					this.Cursor = Cursors.WaitCursor;
					foreach(string fileName in this.openFileDialog1.FileNames)
					{
						fileObj = this.PicLibSaver.AddFile(this._PicLib, fileName);
						//文件名和Attachment对象加到列表中
						this.lstFiles.Add(Path.GetFileName(fileObj.FileName), fileObj);
						//将文件加入到ArrayList中
						this._PicLib.AttachFiles.Add(fileObj);
					}
					this._PicLib.HasChanged = true;

					this.tbtnDelPic.Enabled = true;
					this.tbtnExportPic.Enabled = true;
				}
				catch(Exception ex)
				{
					MessageBox.Show(ex.Message, "出错信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
				finally
				{
					this.Cursor = Cursors.Default;
				}
			}
		}
		/// <summary>
		/// 删除图像文件,同时删除文件内容和关联的记录
		/// </summary>
		private void DeletePicFile()
		{
			if(this.lstFiles.SelectedIndex == -1)
			{
				MessageBox.Show("请先选择一幅图片!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
				return;
			}

			int fileID = ((Attachment)this.lstFiles.GetItemTag(this.lstFiles.SelectedIndex)).FileID;
			
			try
			{
				//清除数据库中的内容
				this.PicLibSaver.DeleteFile(this._PicLib.PicLibID, fileID);
				//清除ArrayList中的对象
				this._PicLib.AttachFiles.RemoveAt(this.lstFiles.SelectedIndex);
				this._PicLib.HasChanged = true;
				//清除列表中的项
				this.lstFiles.RemoveAt(this.lstFiles.SelectedIndex);
				//上次选中的项不再有效
				this.LastFileIndex = -1;
				//不选中任何项
				this.lstFiles.SelectedIndex = -1;
				this.txtPicNotes.Text = "";
				//清空图片框
				this.pictureBox1.Image = null;

				//禁止相关的按钮
				this.tbtnDelPic.Enabled = false;
				this.tbtnExportPic.Enabled = false;
			}
			catch(Exception ex)
			{
				MessageBox.Show(ex.Message, "出错信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}
		/// <summary>
		/// 导出图像文件
		/// </summary>
		private void ExprotPicFile()
		{
			if(this.lstFiles.SelectedIndex == -1)
			{
				MessageBox.Show("请先选择一幅图片!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
				return;
			}

			string saveFileName;
			//int fileID;
			
			this.saveFileDialog1.FileName = Path.GetFileName(this.lstFiles.GetItemText(this.lstFiles.SelectedItem));
			if(this.saveFileDialog1.ShowDialog() == DialogResult.OK)
			{
				saveFileName = this.saveFileDialog1.FileName;
				//fileID = ((Attachment)this.lstFiles.GetItemTag(this.lstFiles.SelectedIndex)).FileID;
				try
				{
					this.Cursor = Cursors.WaitCursor;
					this.PicLibSaver.SaveAttachFileToDB(this._PicLib.AttachFiles[this.lstFiles.SelectedIndex] as Attachment, saveFileName);
					MessageBox.Show("文件已经成功导出到:" + saveFileName, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
				}
				catch(Exception ex)
				{
					MessageBox.Show(ex.Message, "出错信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
				finally
				{
					this.Cursor = Cursors.Default;
				}
			}
		}
		/// <summary>
		/// 当用户选择了不同的图片
		/// </summary>
		private void OnlstFilesSelectedIndexChanged()
		{
			//用户有可能连续删除多项,因此LastIndex可能不再有效
			if(this.LastFileIndex > this.lstFiles.Items.Count - 1)
				this.LastFileIndex = -1;
			//当前选中项的索引
			int curIndex = this.lstFiles.SelectedIndex;
			//当前选中项的文件对象
			Attachment fileObj;

			//检测用户是否己更改了图片说明
			if(this.LastFileIndex != -1)
			{
				fileObj = this.lstFiles.GetItemTag(this.LastFileIndex) as Attachment;
				if(fileObj.HasChanged)
				{
					//如果更改了,将信息保存到数据库中
					this.FileSaver.UpdateAttachmentObjInDB(fileObj);
					fileObj.HasChanged = false;
				}
			}
			
			//当前选中了有效的一项
			if(curIndex != -1)
			{
				this.tbtnExportPic.Enabled = true;
				this.tbtnDelPic.Enabled = true;
				fileObj = this.lstFiles.GetItemTag(curIndex) as Attachment;
				//显示图片说明
				this.txtPicNotes.Text = fileObj.FileNotes;
				//显示图片内容
				try
				{
					this.pictureBox1.Image = this.PicLibSaver.GetPicFromDB(fileObj);
				}
				catch(Exception ex)
				{
					MessageBox.Show(ex.Message, "出错信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
					this.pictureBox1.Image = null;
				}
				//保存当前索引
				this.LastFileIndex = this.lstFiles.SelectedIndex;

⌨️ 快捷键说明

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