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

📄 frmmediaplayer.cs.cs

📁 csharp编写的类似芊芊静听的播放器!
💻 CS
📖 第 1 页 / 共 3 页
字号:
			{
				MessageBox.Show("删除列表时出错:"+ex.Message.ToString(),"异常",MessageBoxButtons.OK,MessageBoxIcon.Asterisk);;
			}
		}

		private void menuItem15_Click(object sender, System.EventArgs e)
		{
			this.menuItem3.PerformClick();
		}

		private void menuItem24_Click(object sender, System.EventArgs e)
		{
			this.menuItem2.PerformClick();
		}

		private void menuItem16_Click(object sender, System.EventArgs e)
		{
			menuItem4.PerformClick();
		}

		private void menuItem18_Click(object sender, System.EventArgs e)
		{
			this.lstMusicName_DoubleClick(sender,e);
		}

		private void menuItem19_Click(object sender, System.EventArgs e)//删除一首或多首歌曲
		{
			try
			{
				if(this.lstMusicName.Items.Count>0)
				{	
					//当所选择的歌曲是当前列表中的所有歌曲时
					if(this.lstMusicName.SelectedItems.Count==this.lstMusicName.Items.Count)
					{
						if(this.currentListIndex==this.lstMusic.SelectedIndex)
						{
							int tempIndex=this.lstMusic.SelectedIndex;
							string tempListName=this.lstMusic.SelectedItem.ToString();
							this.menuItem4.PerformClick();//调用删除列表
							this.lstMusic.Items.Insert(tempIndex,tempListName);
							this.lstMusic.SelectedIndex=tempIndex;
						}
						else
						{
							this.lstMusicName.Items.Clear();
							this.HTMusic.Remove(this.lstMusic.SelectedItem.ToString());
						}
						this.clearAllHT();
						return;
					}
					//当选择的是部分歌曲要删除时
					//定义一个临时ArrayList数组
					ArrayList tempArrayList=new ArrayList();
					//将所选择的全部加载至ArrayList中
					foreach(object obj in this.lstMusicName.SelectedItems)
					{
						tempArrayList.Add(obj.ToString());
					}//加载全后一一删除
					foreach(object obj in tempArrayList)
					{
						this.lstMusicName.Items.Remove(obj.ToString());
					}//删除后清空Arraylist
					tempArrayList.Clear();
					//将剩余的项(未删除的)重新加载至ArrayList
					foreach(object obj in this.lstMusicName.Items)
					{
						tempArrayList.Add(obj.ToString());
					}
					//调用私有方法将临时的ArrayList转换为Array
					Array tempArray=this.changeArrayListToArray(tempArrayList);
					//将原有的此歌曲列表全部删除
					this.HTMusic.Remove(this.lstMusic.SelectedItem.ToString());
					//将现有的tepArray加载至原有的位置(即更新)
					this.HTMusic.Add(this.lstMusic.SelectedItem.ToString(),tempArray);
					if(this.currentListIndex==this.lstMusic.SelectedIndex)
					{
						string tempURL=this.WMMusic.URL;
						if(this.lstMusicName.Items.Contains(tempURL)==true)
						{
							this.lstMusicName.SelectedIndex=this.lstMusicName.Items.IndexOf(tempURL);
							
						}
						else
						{
							this.lstMusicName.SelectedIndex=0;
							this.WMMusic.URL=this.lstMusicName.SelectedItem.ToString();
							if(File.Exists(this.WMMusic.URL)==false)
							{
								MessageBox.Show("读取文件路径时出错,找不到文件:"+this.WMMusic.URL,"文件不存在",MessageBoxButtons.OK,MessageBoxIcon.Asterisk);
							}
						}
						this.currentMusicIndex=this.lstMusicName.SelectedIndex;
						this.currentMusicNames=(Array)this.HTMusic[this.lstMusic.SelectedItem.ToString()];
					}
					this.clearAllHT();
				}
			}
			catch(Exception ex)
			{
				MessageBox.Show("删除歌曲时错误:"+ex.Message.ToString(),"异常",MessageBoxButtons.OK,MessageBoxIcon.Asterisk);
			}
		}
		private void clearAllHT()
		{
			if(this.HTMusic.Count==0)
			{
				this.HTMusic.Clear();
				this.currentListIndex=0;
				this.currentMusicIndex=0;
				this.currentMusicNames=null;
				this.isStart=false;
			}
		}
		private void menuItem5_Click(object sender, System.EventArgs e)
		{
			if(this.lstMusic.Items.Count==0)
				return;
			this.txtRename.Visible=true;
			this.txtRename.Text=this.lstMusic.SelectedItem.ToString();
			this.txtRename.Focus();
			this.txtRename.SelectAll();
		}
		private void txtRename_DoubleClick(object sender, System.EventArgs e)//重命名
		{
			try
			{
				if(this.txtRename.Text.Trim()==""||this.lstMusic.Items.Count==0)
				{
					this.txtRename.Visible=false;
					return;
				}
				if(this.lstMusic.SelectedItem.ToString()==this.txtRename.Text.Trim())
				{
					this.txtRename.Visible=false;
					return;
				}
				if(this.lstMusic.Items.Contains(this.txtRename.Text.Trim()))
				{
					this.txtRename.Focus();
					this.txtRename.SelectAll();
					throw new Exception("已经存在相同名称的列表!");
				}
				int index=this.lstMusic.SelectedIndex;
				string oldName=this.lstMusic.SelectedItem.ToString();
				string newName=this.txtRename.Text;
				Array oldList=(Array)this.HTMusic[oldName];
				this.lstMusic.Items.RemoveAt(index);
				this.lstMusic.Items.Insert(index,newName);
				if(this.HTMusic.ContainsKey(oldName)==true)
				{
					this.HTMusic.Remove(oldName);
					this.HTMusic.Add(newName,oldList);
				}
				this.txtRename.Visible=false;
				this.lstMusic.SelectedIndex=index;
			}
			catch(Exception ex)
			{
				MessageBox.Show("重命名时出错:"+ex.Message.ToString(),"异常",MessageBoxButtons.OK,MessageBoxIcon.Asterisk);;
			}
		}

		private void menuItem17_Click(object sender, System.EventArgs e)
		{
			this.menuItem5.PerformClick();
		}
		//退出出保存所有记录,保存格式为XML
		private void menuItem8_Click(object sender, System.EventArgs e)
		{
			if(MessageBox.Show("您确定要退出吗?","退出确认",MessageBoxButtons.OKCancel,MessageBoxIcon.Question)==DialogResult.OK)
			{
				this.menuItem13.PerformClick();
			}
		}

		private void menuItem13_Click(object sender, System.EventArgs e)
		{
			this.saveAll();
			this.Close();
			Application.Exit();
		}
		private bool saveAll()//保存所有列表的私有方法
		{
			try
			{
				string CurrentURL=Application.StartupPath;
				if(this.HTMusic.Count==0)
				{
					if(File.Exists(CurrentURL+@"\TTPlayer.xml"))
					{
						File.Delete(CurrentURL+@"\TTPlayer.xml");
					}
					return false;
				}
				this.lstMusic.SelectedIndex=this.currentListIndex;
				string strXML="<?xml version='1.0'?>"+"\n";
				strXML+="<root currentListIndex='"+this.lstMusic.Items[this.currentListIndex].ToString()+"' currentMusicIndex='"+this.lstMusicName.Items[this.currentMusicIndex]+"'>"+"\n";
				Array temp=null;

				foreach(object obj in this.lstMusic.Items)
				{
					if(this.HTMusic.ContainsKey(obj.ToString()))
					{
						temp=(Array)this.HTMusic[obj.ToString()];
						foreach(object objArr in temp)
						{
							strXML+="<musicName>"+objArr.ToString()+"</musicName>"+"\n";
						}
						strXML+="<musicListName>"+obj.ToString()+"</musicListName>"+"\n";
					}
				}
				strXML+="</root>";
				StreamWriter SWWriteXML;
				if(File.Exists(CurrentURL+@"\TTPlayer.xml"))
				{
					File.Delete(CurrentURL+@"\TTPlayer.xml");
				}
				SWWriteXML=File.AppendText(CurrentURL+@"\TTPlayer.xml");
				SWWriteXML.Write(strXML);
				SWWriteXML.Close();
				return true;
			}
			catch(Exception ex)
			{
				MessageBox.Show("保存列表时出错:"+ex.Message.ToString()+"\n如果程序关闭后您的任务栏下面仍然还有此程序图标,请将鼠标移动至任务栏刷新任务栏后方可正常运行。","异常",MessageBoxButtons.OK,MessageBoxIcon.Asterisk);
				return false;
			}
		}
		private void readAll()//读取所有列表,并将读取内容显示至当前状态的私有方法
		{
			try
			{
				XmlDocument document=new XmlDocument();//创建XML文档对象
				document.Load("TTPlayer.xml");//加载XML文件
				//获取所有的Node
				XmlNodeList nodeList=document.GetElementsByTagName("*");
				XmlNamedNodeMap namedNodeMap;//可以访问子节点属性的集合
				XmlElement element;//声明一个元素
				XmlAttribute attribute;//声明一个属性
				string tempListName="";
				string tempCurrentListName="";
				string tempCurrentMusicName="";
				ArrayList tempMusicNames=new ArrayList();
				for(int i=0;i<nodeList.Count;i++)
				{
					element=(XmlElement)nodeList.Item(i);
					if(element.Name=="musicName")
						tempMusicNames.Add(element.ChildNodes[0].Value);
					namedNodeMap=element.Attributes;
					if(namedNodeMap!=null)
					{
						for(int j=0;j<namedNodeMap.Count;j++)
						{
							attribute=(XmlAttribute)namedNodeMap.Item(j);
							if(attribute.Name=="currentListIndex")
							{
								tempCurrentListName=attribute.Value;
							}

							if(attribute.Name=="currentMusicIndex")
							{
								tempCurrentMusicName=attribute.Value;
							}
						}
					}
					if(element.Name=="musicListName")
					{
						tempListName=element.ChildNodes[0].Value;
						if(tempMusicNames.Count>0)
						{
							this.HTMusic.Add(tempListName,this.changeArrayListToArray(tempMusicNames));
							tempMusicNames.Clear();
						}
					}
				}
				foreach(DictionaryEntry objDE in this.HTMusic)
				{
					this.lstMusic.Items.Add(objDE.Key.ToString());
				}
				this.lstMusic.SelectedIndex=this.lstMusic.Items.IndexOf(tempCurrentListName);
				this.currentListIndex=this.lstMusic.SelectedIndex;
				this.lstMusicName.SelectedIndex=this.lstMusicName.Items.IndexOf(tempCurrentMusicName);
				this.currentMusicIndex=this.lstMusicName.SelectedIndex;
				this.currentMusicNames=(Array)this.HTMusic[this.lstMusic.SelectedItem.ToString()];
				this.WMMusic.URL=this.lstMusicName.SelectedItem.ToString();
				if(File.Exists(this.WMMusic.URL)==false)
				{
					MessageBox.Show("读取文件路径时出错,找不到文件:"+this.WMMusic.URL,"文件不存在",MessageBoxButtons.OK,MessageBoxIcon.Asterisk);
				}
				this.menuItem28.PerformClick();
				this.timMusic.Interval=1000;
				this.timMusic.Enabled=true;
				this.isStart=true;
			}
			catch(Exception e)
			{
				if(File.Exists("TTPlayer.xml"))
				{
					File.Delete("TTPlayer.xml");
				}
				MessageBox.Show("读取信息列表时出错:"+e.Message.ToString(),"异常",MessageBoxButtons.OK,MessageBoxIcon.Asterisk);
				this.lstMusic.Items.Clear();
				this.lstMusicName.Items.Clear();
				this.lstMusic.Items.Add("[默认列表]");
				this.lstMusic.SelectedIndex=0;
				this.isStart=false;
			}
		}
		private Array changeArrayListToArray(ArrayList tempMusicNames)//将ArrayList轮换成Array的方法
		{
			int index=0;
			Array tempArray=Array.CreateInstance(typeof(string),tempMusicNames.Count);
			foreach(string str in tempMusicNames)
			{	
				tempArray.SetValue(str,index++);
			}
			return tempArray;
		}

		private void menuItem28_Click(object sender, System.EventArgs e)//按名称排序
		{
			try
			{
				string tempListName=this.lstMusic.Items[this.currentListIndex].ToString();
				Array tempList=Array.CreateInstance(typeof(string),this.lstMusic.Items.Count);
				for(int index=0;index<this.lstMusic.Items.Count;index++)
				{
					tempList.SetValue(this.lstMusic.Items[index].ToString(),index);
				}
				Array.Sort(tempList);
				this.lstMusic.Items.Clear();
				foreach(object obj in tempList)
				{
					this.lstMusic.Items.Add(obj.ToString());
				}
				this.lstMusic.SelectedIndex=this.lstMusic.Items.IndexOf(tempListName);
				if(this.HTMusic.Count==0)
				{
					return;
				}
				this.lstMusicName.SelectedIndex=this.currentMusicIndex;
				this.currentListIndex=this.lstMusic.SelectedIndex;
			}
			catch(Exception ex)
			{
				MessageBox.Show("排序时出错:"+ex.Message.ToString(),"异常",MessageBoxButtons.OK,MessageBoxIcon.Asterisk);;
			}
		}

		private void menuItem26_Click(object sender, System.EventArgs e)
		{
			if(this.saveAll())
				MessageBox.Show("信息已经成功保存!","保存成功",MessageBoxButtons.OK,MessageBoxIcon.Asterisk);
		}
		//用户按回车键时重命名生效
		private void txtRename_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
		{	
			if(e.KeyChar==(char)13)
			{
				this.txtRename_DoubleClick(sender,e);
			}
		}
		//只允许打开一个应用程序实例
		public static Process RunningInstance()
		{
			Process current = Process.GetCurrentProcess();
			Process[] processes = Process.GetProcessesByName (current.ProcessName);

			//Loop through the running processes in with the same name
			foreach (Process process in processes)
			{
				//Ignore the current process
				if (process.Id != current.Id)
				{
					//Make sure that the process is running from the exe file.
					if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") ==
						current.MainModule.FileName)
					{
						//Return the other process instance.
						return process;
					}
				}
			}

			//No other instance was found, return null.
			return null;
		}

		
		public static void HandleRunningInstance(Process instance)
		{
			//Make sure the window is not minimized or maximized
			ShowWindowAsync (instance.MainWindowHandle , WS_SHOWNORMAL);

			//Set the real intance to foreground window
			SetForegroundWindow (instance.MainWindowHandle);
		}

		[DllImport("User32.dll")] 

		private static extern bool ShowWindowAsync(
			IntPtr hWnd, int cmdShow);
		[DllImport("User32.dll")] private static extern bool
			SetForegroundWindow(IntPtr hWnd);
		private const int WS_SHOWNORMAL = 1;

	
		
	}
}

⌨️ 快捷键说明

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