标签:
其实很早就有这个想法了,不求界面多么华丽,只求功能实现,想来也不会很难,音乐播放器无非也就是些IO的操作,我写的山寨音乐播放器暂不支持在线播放,如果要播放的话,要将音乐文件下载到本地,通过IO操作读入程序中来,废话不多说了,界面如下(参考了传智播客蒋坤老师的相关课程):
一些变量的定义及含义:
string temp1 = null; string temp2 = null; //设置添加曲目的路径 string strSearchPath; //定时退出的时间 int iSecond; bool IsVisible = false; //存放选择歌曲全路径 List<string> strFullPath = new List<string>(); //存放整理后的歌词 string[] sLrc = null; //存放搜索歌曲的全路径 List<string> strSearchLst = new List<string>();
1.加载文件(“打开”按钮)
1 private void BtnOpen_Click(object sender, EventArgs e) 2 { 3 #region 加载播放列表,并开始播放 4 using (OpenFileDialog ofd=new OpenFileDialog()) 5 { 6 //从配置文件读取歌曲所在路径 7 ofd.InitialDirectory = ConfigurationManager.AppSettings["FilePath"]; 8 ofd.Title = "加载音乐文件"; 9 ofd.Multiselect = true; 10 ofd.Filter = "音乐文件(*.mp3)|*.mp3|其他格式(*.avi)|*.avi|所有文件(*.*)|*.*"; 11 int n = -1; 12 13 if (ofd.ShowDialog() == DialogResult.OK) 14 { 15 string[] strFullName = ofd.FileNames; 16 if (strFullName.Length > 0) 17 { 18 for (int i = 0; i < strFullName.Length; i++) 19 { 20 if (!strFullPath.Contains(strFullName[i])) 21 { 22 strFullPath.Add(strFullName[i]); 23 //F:\音乐\Beyond-海阔天空.mp3 24 string[] str = strFullName[i].Split(‘\\‘); 25 lbSongsList.Items.Add(str[str.Length - 1]); 26 PlaySongs(); 27 } 28 else 29 { 30 //包含该歌曲 31 if (MessageBox.Show("播放列表中已经包含该歌曲,现在播放吗?","提示",MessageBoxButtons.OKCancel)==DialogResult.OK) 32 { 33 string s = ofd.FileName; 34 for (int j = 0; j < strFullPath.Count; j++) 35 { 36 if (strFullPath[j] == s) 37 { 38 n = j; 39 break; 40 } 41 } 42 } 43 else 44 { 45 return; 46 } 47 } 48 } 49 50 if (n != -1) 51 { 52 PlaySongs(n); 53 } 54 } 55 } 56 } 57 #endregion 58 }
PlaySongs()方法的定义如下:
1 /// <summary> 2 /// 默认播放方法 3 /// </summary> 4 private void PlaySongs() 5 { 6 PlaySongs(0); 7 } 8 9 /// <summary> 10 /// 播放方法 11 /// </summary> 12 /// <param name="i"></param> 13 private void PlaySongs(int i) 14 { 15 lbLrc1.Text = ""; 16 lbLrc2.Text = ""; 17 lbLrc3.Text = ""; 18 sLrc = null; 19 20 if (i!=-1) 21 { 22 timer1.Enabled = true; 23 axWindowsMediaPlayer1.URL = strFullPath[i]; 24 lbSongsList.SelectedIndex = i; 25 this.Text = "当前播放曲目:" + lbSongsList.SelectedItem.ToString(); 26 axWindowsMediaPlayer1.Ctlcontrols.play(); 27 LoadLrc(axWindowsMediaPlayer1.URL); 28 } 29 30 if (lbSongsList.Items.Count>0) 31 { 32 groupBox1.Enabled = true; 33 txtSelect.Enabled = true; 34 BtnSelect.Enabled = true; 35 BtnClear.Enabled = true; 36 temp1 = lbSongsList.SelectedItem.ToString(); 37 lbMsg.Text = "当前列表共有歌曲" + strFullPath.Count + "首歌,当前播放曲目为:" + lbSongsList.SelectedItem.ToString(); 38 } 39 }
LoadLrc()是加载歌词文件的方法,定义如下:
1 /// <summary> 2 /// 加载歌词 3 /// </summary> 4 /// <param name="p"></param> 5 private void LoadLrc(string p) 6 { 7 //F:\音乐\Beyond-光辉岁月(国语版).mp3 8 string[] strLrcLines; 9 int k = p.LastIndexOf(‘.‘); 10 string sMusicLrc = p.Substring(0,k) + ".lrc"; 11 12 if (File.Exists(sMusicLrc)) 13 { 14 #region 废弃 15 //Encoding codeType = GetFileEncodingTye(sMusicLrc); 16 //if (codeType.BodyName=="UTF8") 17 //{ 18 // strLrcLines = File.ReadAllLines(sMusicLrc, Encoding.UTF8); 19 // SelectLrc(strLrcLines); 20 //} 21 //else if (codeType.BodyName=="gb2312") 22 //{ 23 // strLrcLines = File.ReadAllLines(sMusicLrc, Encoding.Default); 24 // SelectLrc(strLrcLines); 25 //} 26 //else 27 //{ 28 // strLrcLines = File.ReadAllLines(sMusicLrc, Encoding.Default); 29 // SelectLrc(strLrcLines); 30 //} 31 #endregion 32 33 strLrcLines = File.ReadAllLines(sMusicLrc, Encoding.UTF8); 34 SelectLrc(strLrcLines); 35 } 36 else 37 { 38 lbLrc2.Text = "--该歌词未找到--"; 39 strLrcLines = null; 40 } 41 }
其中,加载歌词文件,涉及到歌词的整理,定义方法为SelectLrc(),定义如下:
1 /// <summary> 2 /// 整理歌词文件 3 /// </summary> 4 /// <param name="strLrcLines"></param> 5 private void SelectLrc(string[] strLrcLines) 6 { 7 List<string> sTemp = new List<string>(); 8 //[00:33.28]在他生命里仿佛带点唏嘘 9 if (strLrcLines.Length>0) 10 { 11 for (int i = 0; i < strLrcLines.Length; i++) 12 { 13 string[] s1 = strLrcLines[i].Split(new char[] { ‘[‘, ‘]‘ }, StringSplitOptions.RemoveEmptyEntries); 14 for (int j = 0; j < s1.Length; j++) 15 { 16 if (s1[j].Contains(‘:‘)) 17 { 18 sTemp.Add(s1[j] + "|" + s1[s1.Length - 1]); 19 } 20 } 21 } 22 } 23 sTemp.Sort(); 24 sLrc = sTemp.ToArray(); 25 }
下面就要将整理好的歌词文件和音乐播放同步了,使用的是timer控件,将其间隔时间设为1000(1秒钟触发一次),写其Tick事件:
1 #region 歌词显示 2 if (lbSongsList.SelectedIndex != -1) 3 { 4 #region 信息显示 5 temp1 = temp1.Length > 10 ? temp1.Substring(1) + temp1[0] : temp1; 6 lbName.Text = "歌名:" + temp1; 7 temp2 = temp2.Length > 15 ? temp2.Substring(1) + temp2[0] : temp2; 8 lbState.Text = "播放状态:" + temp2; 9 lbPosition.Text = "当前位置:" + axWindowsMediaPlayer1.Ctlcontrols.currentPositionString; 10 lbTotalTime.Text = "总时长:" + axWindowsMediaPlayer1.currentMedia.durationString; 11 if (axWindowsMediaPlayer1.settings.mute == true) 12 { 13 lbVol.Text = "音量:静音 "; 14 } 15 else 16 { 17 lbVol.Text = "音量:" + axWindowsMediaPlayer1.settings.volume.ToString(); 18 } 19 #endregion 20 21 if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsPlaying) 22 { 23 if (sLrc != null) 24 { 25 for (int i = 0; i < sLrc.Length - 1; i++) 26 { 27 //00:02.01|光辉岁月 (指定曲目) - Beyond 28 if (string.Compare(axWindowsMediaPlayer1.Ctlcontrols.currentPositionString, sLrc[i]) > 0 && string.Compare(axWindowsMediaPlayer1.Ctlcontrols.currentPositionString, sLrc[i + 1]) < 0) 29 { 30 //if (i-1>0) 31 //{ 32 // lbLrc1.Text = sLrc[i - 1].Split(‘|‘)[1]; 33 //} 34 //else 35 //{ 36 // lbLrc1.Text = ""; 37 //} 38 lbLrc1.Text = i - 1 > 0 ? sLrc[i - 1].Split(‘|‘)[1] : ""; 39 lbLrc2.Text = sLrc[i].Split(‘|‘)[1]; 40 lbLrc3.Text = sLrc[i + 1].Split(‘|‘)[1]; 41 } 42 43 if (string.Compare(axWindowsMediaPlayer1.Ctlcontrols.currentPositionString, sLrc[0]) < 0) 44 { 45 lbLrc1.Text = ""; 46 lbLrc2.Text = lbSongsList.SelectedItem.ToString(); 47 lbLrc3.Text = sLrc[1].Split(‘|‘)[1]; 48 } 49 if (string.Compare(axWindowsMediaPlayer1.Ctlcontrols.currentPositionString, sLrc[sLrc.Length - 1]) > 0) 50 { 51 lbLrc1.Text = sLrc[sLrc.Length - 2].Split(‘|‘)[1]; 52 lbLrc2.Text = sLrc[sLrc.Length - 1].Split(‘|‘)[1]; 53 lbLrc3.Text = ""; 54 } 55 } 56 } 57 } 58 } 59 #endregion
temp2存储的是播放状态,在播放器的StatusChange()事件中为其赋值。
1 void axWindowsMediaPlayer1_StatusChange(object sender, EventArgs e) 2 { 3 temp2 = axWindowsMediaPlayer1.status; 4 }
接下来是播放控制区的功能实现(上一曲,下一曲,播放,暂停等功能)
1 private void BtnPause_Click(object sender, EventArgs e) 2 { 3 #region 控制播放/暂停功能 4 if (lbSongsList.Items.Count > 0) 5 { 6 if (BtnPause.Text == "暂停") 7 { 8 BtnPause.Text = "播放"; 9 axWindowsMediaPlayer1.Ctlcontrols.pause(); 10 } 11 else if (BtnPause.Text == "播放") 12 { 13 BtnPause.Text = "暂停"; 14 BtnStop.Text = "终止"; 15 axWindowsMediaPlayer1.Ctlcontrols.play(); 16 } 17 } 18 #endregion 19 }
1 private void BtnStop_Click(object sender, EventArgs e) 2 { 3 #region 控制终止功能 4 if (lbSongsList.Items.Count > 0) 5 { 6 if (BtnStop.Text == "终止") 7 { 8 BtnStop.Text = "取消终止"; 9 BtnPause.Text = "播放"; 10 axWindowsMediaPlayer1.Ctlcontrols.stop(); 11 } 12 else if (BtnStop.Text == "取消终止") 13 { 14 BtnStop.Text = "终止"; 15 BtnPause.Text = "暂停"; 16 axWindowsMediaPlayer1.Ctlcontrols.play(); 17 } 18 } 19 #endregion 20 }
1 private void BtnPrev_Click(object sender, EventArgs e) 2 { 3 #region 上一曲 4 //int j = lbSongsList.SelectedIndex; 5 //if (j!=-1&&lbSongsList.Items.Count>0) 6 //{ 7 // j = j == 0 ? strFullPath.Count - 1 : --j; 8 // PlaySongs(j); 9 //} 10 11 int j = -1; 12 string s = axWindowsMediaPlayer1.URL; 13 14 if (strFullPath.Count > 0) 15 { 16 for (int i = 0; i < strFullPath.Count; i++) 17 { 18 if (strFullPath[i]==s) 19 { 20 j = i; 21 break; 22 } 23 } 24 25 if (j!=-1) 26 { 27 j = j == 0 ? strFullPath.Count - 1 : --j; 28 PlaySongs(j); 29 } 30 } 31 #endregion 32 }
1 private void BtnNext_Click(object sender, EventArgs e) 2 { 3 #region 下一曲 4 //int j = lbSongsList.SelectedIndex; 5 //if (j!=-1&&lbSongsList.Items.Count>0) 6 //{ 7 // j = j == strFullPath.Count - 1 ? 0 : ++j; 8 // PlaySongs(j); 9 //} 10 11 int j = -1; 12 string s = axWindowsMediaPlayer1.URL; 13 14 if (strFullPath.Count>0) 15 { 16 for (int i = 0; i < strFullPath.Count; i++) 17 { 18 if (strFullPath[i]==s) 19 { 20 j = i; break; 21 } 22 } 23 24 if (j!=-1) 25 { 26 j = j == strFullPath.Count - 1 ? 0 : ++j; 27 PlaySongs(j); 28 } 29 } 30 #endregion 31 }
控制音量:
1 private void BtnVolUp_Click(object sender, EventArgs e) 2 { 3 #region 音量加 4 if (lbSongsList.Items.Count>0) 5 { 6 axWindowsMediaPlayer1.settings.volume += 5; 7 cbMute.Checked = false; 8 } 9 #endregion 10 } 11 12 private void BtnVolDown_Click(object sender, EventArgs e) 13 { 14 #region 音量减 15 if (lbSongsList.Items.Count>0) 16 { 17 axWindowsMediaPlayer1.settings.volume -= 5; 18 cbMute.Checked = false; 19 } 20 #endregion 21 }
静音:
1 private void cbMute_CheckedChanged(object sender, EventArgs e) 2 { 3 #region 静音 4 if (lbSongsList.Items.Count > 0) 5 { 6 axWindowsMediaPlayer1.settings.mute = cbMute.Checked; 7 } 8 #endregion 9 }
接下来是歌曲搜索的功能(暂不完善):
1 private void BtnSelect_Click(object sender, EventArgs e) 2 { 3 #region 歌曲检索 4 int j = -1; 5 var sSelect = txtSelect.Text; 6 7 if (!string.IsNullOrEmpty(sSelect)) 8 { 9 string sPattern = ".*" + sSelect + ".*"; 10 for (int i = 0; i <strFullPath.Count; i++) 11 { 12 while(Regex.IsMatch(strFullPath[i],sPattern)) 13 { 14 if (MessageBox.Show("已经找到匹配歌曲,现在播放吗?","提示",MessageBoxButtons.OKCancel)==DialogResult.OK) 15 { 16 j = i; 17 break; 18 } 19 else 20 { 21 return; 22 } 23 } 24 } 25 } 26 else 27 { 28 MessageBox.Show("搜索歌名不能为空,请重新搜索"); 29 txtSelect.Focus(); 30 return; 31 } 32 33 if (j!=-1) 34 { 35 PlaySongs(j); 36 } 37 else 38 { 39 //MessageBox.Show("你输入的歌曲未找到,先去下载吧。", "提示"); 40 using (Process pr=new Process()) 41 { 42 pr.StartInfo.FileName = "firefox.exe";//iexplore.exe 43 pr.StartInfo.Arguments = "http://music.baidu.com/"; 44 pr.Start(); 45 } 46 txtSelect.Text = ""; 47 return; 48 } 49 #endregion 50 }
当清空搜索框中的文本时,播放器回归默认播放,从第一首歌开始播放:
1 private void BtnClear_Click(object sender, EventArgs e) 2 { 3 if (txtSelect.Text != "" || txtSearch.Text!="") 4 { 5 txtSelect.Text = ""; 6 txtSearch.Text = ""; 7 PlaySongs(); 8 } 9 }
双击播放列表,实现播放:
1 private void lbSongsList_DoubleClick(object sender, EventArgs e) 2 { 3 #region 双击播放 4 var item = lbSongsList.SelectedItem; 5 int m = -1; 6 if (item!=null) 7 { 8 var s = item.ToString(); 9 for (int i = 0; i < lbSongsList.Items.Count; i++) 10 { 11 if (lbSongsList.Items[i].ToString()==s) 12 { 13 m = i; 14 break; 15 } 16 } 17 18 if (m != -1) 19 { 20 PlaySongs(m); 21 } 22 } 23 #endregion 24 }
右击播放列表,添加歌曲,删除歌曲,清空播放列表功能实现:
1 private void tsmiAdd_Click(object sender, EventArgs e) 2 { 3 #region 右击添加歌曲 4 int m = lbSongsList.SelectedIndex; 5 if (m==-1) 6 { 7 return; 8 } 9 10 int n = -1; 11 12 using (OpenFileDialog ofd=new OpenFileDialog()) 13 { 14 ofd.InitialDirectory = ConfigurationManager.AppSettings["FilePath"]; 15 ofd.Title = "添加单首音乐文件"; 16 ofd.Multiselect = false; 17 ofd.Filter = "音乐文件(*.mp3)|*.mp3|其他格式(*.avi)|*.avi|所有文件(*.*)|*.*"; 18 19 if (ofd.ShowDialog()==DialogResult.OK) 20 { 21 string sName = ofd.FileName; 22 if (sName.Length>0) 23 { 24 if (strFullPath.Contains(sName)) 25 { 26 //当前播放列表包含该歌曲 27 if (MessageBox.Show("当前播放列表包含该歌曲,现在播放吗?","提示",MessageBoxButtons.OKCancel)==DialogResult.OK) 28 { 29 //播放 30 for (int i = 0; i < strFullPath.Count; i++) 31 { 32 if (strFullPath[i]==sName) 33 { 34 n = i; 35 break; 36 } 37 } 38 } 39 else 40 { 41 return; 42 } 43 } 44 else 45 { 46 //当前播放列表不包含该歌曲 47 if (MessageBox.Show("当前播放列表不包含该歌曲,现在添加播放吗?","提示",MessageBoxButtons.YesNo)==DialogResult.Yes) 48 { 49 //添加音乐文件,并播放该文件 50 strFullPath.Insert(m, sName); 51 string[] str = sName.Split(‘\\‘); 52 lbSongsList.Items.Insert(m, str[str.Length - 1]); 53 n = m; 54 } 55 else 56 { 57 return; 58 } 59 } 60 } 61 else 62 { 63 return; 64 } 65 } 66 } 67 68 if (n!=-1) 69 { 70 PlaySongs(n); 71 } 72 #endregion 73 } 74 75 private void tsmiDelete_Click(object sender, EventArgs e) 76 { 77 //删除当前歌曲 78 int i = -1; 79 var s = axWindowsMediaPlayer1.URL; 80 for (int j = 0; j < strFullPath.Count; j++) 81 { 82 if (strFullPath[j]==s) 83 { 84 i = j; 85 break; 86 } 87 } 88 if (i==-1) 89 { 90 return; 91 } 92 93 if (MessageBox.Show("你确定要删除选中的歌曲吗?","提示",MessageBoxButtons.YesNo)==DialogResult.Yes) 94 { 95 //从当前播放列表移除该歌曲,自动播放下一首歌 96 strFullPath.RemoveAt(i); 97 lbSongsList.Items.RemoveAt(i); 98 99 if (lbSongsList.Items.Count > 0) 100 { 101 if (i == lbSongsList.Items.Count) 102 { 103 //如果是最后一首,则删除后播放第一首歌 104 i = 0; 105 } 106 PlaySongs(i); 107 } 108 } 109 else 110 { 111 return; 112 } 113 } 114 115 private void tsmiClear_Click(object sender, EventArgs e) 116 { 117 #region 清空播放列表 118 if (MessageBox.Show("你确定要清空当前播放列表吗?","提示",MessageBoxButtons.YesNo)==DialogResult.Yes) 119 { 120 //清空 121 if (strFullPath.Count>0) 122 { 123 ClearMedia(); 124 } 125 } 126 else 127 { 128 return; 129 } 130 #endregion 131 }
1 /// <summary> 2 /// 清空播放列表时的操作 3 /// </summary> 4 private void ClearMedia() 5 { 6 strFullPath.Clear(); 7 lbSongsList.Items.Clear(); 8 axWindowsMediaPlayer1.Ctlcontrols.stop(); 9 this.Text = "音乐播放器"; 10 lbName.Text = "歌名:"; 11 lbState.Text = "播放状态:"; 12 lbPosition.Text = "当前位置:"; 13 lbTotalTime.Text = "总时长:"; 14 lbVol.Text = "音量:"; 15 lbLrc1.Text = ""; 16 lbLrc2.Text = ""; 17 lbLrc3.Text = ""; 18 sLrc = null; 19 File.Delete("music.txt"); 20 }
搜索指定路径中的歌曲文件,并添加到播放列表中:
1 private void BtnPath_Click(object sender, EventArgs e) 2 { 3 #region 设定选择路径 4 using (FolderBrowserDialog fbd=new FolderBrowserDialog()) 5 { 6 fbd.Description = "选择路径"; 7 fbd.RootFolder = Environment.SpecialFolder.MyComputer; 8 9 if (fbd.ShowDialog()==DialogResult.OK) 10 { 11 strSearchPath = fbd.SelectedPath; 12 MessageBox.Show("路径设定完成"); 13 BtnPath.Enabled = false; 14 } 15 } 16 #endregion 17 }
1 private void BtnAddSearch_Click(object sender, EventArgs e) 2 { 3 #region 将搜索的曲目添加到右侧列表 4 if (string.IsNullOrEmpty(strSearchPath)) 5 { 6 MessageBox.Show("搜索路径不能为空,请选择"); 7 BtnPath.Focus(); 8 return; 9 } 10 11 lbSearchList.Items.Clear(); 12 13 var sSearch = txtSearch.Text; 14 if (!string.IsNullOrEmpty(sSearch) && strSearchPath != "") 15 { 16 string[] sSearchFName = Directory.GetFiles(strSearchPath, "*" + sSearch + "*.mp3"); 17 for (int i = 0; i < sSearchFName.Length; i++) 18 { 19 if (!strFullPath.Contains(sSearchFName[i])) 20 { 21 strSearchLst.Add(sSearchFName[i]); 22 string[] SearchName = sSearchFName[i].Split(‘\\‘); 23 lbSearchList.Items.Add(SearchName[SearchName.Length - 1]); 24 } 25 else 26 { 27 MessageBox.Show("播放列表中已经包含该文件,请搜索播放"); 28 txtSelect.Focus(); 29 return; 30 } 31 } 32 } 33 else 34 { 35 MessageBox.Show("请输入添加歌曲的关键词"); 36 txtSearch.Focus(); 37 return; 38 } 39 #endregion 40 }
1 private void BtnToLeft_Click(object sender, EventArgs e) 2 { 3 #region 将右侧列表中的歌曲添加到左边 4 if (lbSearchList.SelectedIndex!=-1) 5 { 6 //for (int i = lbSearchList.SelectedItems.Count; i > 0; i--) 7 //{ 8 // var s = lbSearchList.SelectedItem.ToString(); 9 // if (!lbSongsList.Items.Contains(s)) 10 // { 11 // lbSongsList.Items.Add(s); 12 // //strFullPath.Add(strSearchLst[i]); 13 // lbSearchList.Items.Remove(s); 14 // } 15 //} 16 17 int k = -1; 18 19 for (int i = 0; i < strSearchLst.Count; i++) 20 { 21 string[] str= strSearchLst[i].Split(‘\\‘); 22 if (lbSearchList.SelectedItem!=null) 23 { 24 if (str[str.Length - 1] == lbSearchList.SelectedItem.ToString()) 25 { 26 k = i; 27 strFullPath.Add(strSearchLst[k]); 28 lbSongsList.Items.Add(str[str.Length - 1]); 29 lbSearchList.Items.Remove(str[str.Length - 1]); 30 } 31 } 32 } 33 } 34 35 if (lbSongsList.SelectedItem!=null) 36 { 37 lbMsg.Text = "当前列表共有歌曲" + strFullPath.Count + "首歌,当前播放曲目为:" + lbSongsList.SelectedItem.ToString(); 38 } 39 #endregion 40 }
在播放器退出时,将最新的播放列表文件更新到文件中:
1 private void BtnExit_Click(object sender, EventArgs e) 2 { 3 if (strFullPath.Count > 0) 4 { 5 if (File.Exists("music.txt")) 6 { 7 File.Delete("music.txt"); 8 } 9 10 for (int i = 0; i <= strFullPath.Count - 1; i++) 11 { 12 File.AppendAllText("music.txt", strFullPath[i] + "\r\n", Encoding.Default); 13 } 14 } 15 this.Close(); 16 }
至此,音乐播放器的主要功能都已实现,下面是一些辅助功能的实现代码:
1.调整字体字号:
1 private void BtnSetting_Click(object sender, EventArgs e) 2 { 3 //调整字体字号 4 using (FontDialog fd=new FontDialog()) 5 { 6 fd.Font = lbLrc2.Font; 7 if (fd.ShowDialog()==DialogResult.OK) 8 { 9 lbLrc2.Font = fd.Font; 10 lbSongsList.Font = fd.Font; 11 } 12 } 13 }
2.设置字体颜色:
1 private void BtnSetColor_Click(object sender, EventArgs e) 2 { 3 //设置字体颜色 4 using (ColorDialog cd=new ColorDialog()) 5 { 6 cd.Color = lbLrc2.ForeColor; 7 if (cd.ShowDialog()==DialogResult.OK) 8 { 9 lbLrc2.ForeColor = cd.Color; 10 lbSongsList.ForeColor = lbLrc2.ForeColor; 11 } 12 } 13 }
3.设置背景色:
1 private void BtnBColor_Click(object sender, EventArgs e) 2 { 3 //设置背景色 4 using (ColorDialog cd = new ColorDialog()) 5 { 6 cd.Color = lbLrc2.BackColor; 7 if (cd.ShowDialog() == DialogResult.OK) 8 { 9 lbSongsList.BackColor = cd.Color; 10 } 11 } 12 }
4.实现系统托盘的功能,使用到的是notifyIcon控件:
1 private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e) 2 { 3 if (e.Button==MouseButtons.Left) 4 { 5 this.WindowState = FormWindowState.Normal; 6 //this.ShowInTaskbar = true; 7 notifyIcon1.Visible = false; 8 this.ShowInTaskbar = !notifyIcon1.Visible; 9 this.Activate(); 10 } 11 }
5.定时退出的功能实现(为系统托盘的关联菜单功能),定义TimeOver(时间参数),方法:
1 private void TimeOver(int i) 2 { 3 iSecond = i * 60; 4 IsVisible = true; 5 }
并在Time控件中计时:
1 if (IsVisible) 2 { 3 if (iSecond >= 1) 4 { 5 lbShow.Visible = true; 6 iSecond--; 7 string s = ConvertTime(iSecond); 8 lbShow.Text = s + "后程序将退出"; 9 lbShow.ForeColor = Color.BlueViolet; 10 lbShow.Font = new Font("微软雅黑", 12); 11 } 12 else 13 { 14 lbShow.Visible = false; 15 BtnExit_Click(sender, e); 16 } 17 }
1 /// <summary> 2 /// 时间转换方法 3 /// </summary> 4 /// <param name="iSecond"></param> 5 /// <returns></returns> 6 private string ConvertTime(int iSecond) 7 { 8 StringBuilder sb = new StringBuilder(); 9 10 if (iSecond/3600>=1) 11 { 12 int a = Convert.ToInt32(iSecond) / 3600; 13 sb.Append(a + "时"); 14 int b = iSecond - a * 3600; //剩余秒数 15 if (b/60>=1) 16 { 17 int c = Convert.ToInt32(b) / 60; //分钟数 18 sb.Append(c + "分"); 19 int d = b - c * 60; 20 if (d>0) 21 { 22 sb.Append(d + "秒"); 23 } 24 } 25 else 26 { 27 sb.Append(Convert.ToInt32(b) + "秒"); 28 } 29 } 30 else if (iSecond/60>=1) 31 { 32 int e=Convert.ToInt32(iSecond) / 60; 33 sb.Append(e + "分"); 34 int f = iSecond - e * 60; 35 if (f>0) 36 { 37 sb.Append(f + "秒"); 38 } 39 } 40 else 41 { 42 sb.Append(iSecond+"秒"); 43 } 44 return sb.ToString(); 45 }
当右击托盘时,显示此菜单(及最小化时在任务栏中不出现,点击此菜单“显示”时,显示主界面,托盘消失):
1 private void toolStripMenuItem1_Click(object sender, EventArgs e) 2 { 3 this.WindowState = FormWindowState.Normal; 4 //this.ShowInTaskbar = true; 5 notifyIcon1.Visible = false; 6 this.ShowInTaskbar = !notifyIcon1.Visible; 7 this.Activate(); 8 }
标签:
原文地址:http://www.cnblogs.com/chens2865/p/4452514.html