功能齐全界面漂亮的c#音乐播放器

//精简模式前端显示

功能齐全界面漂亮的c#音乐播放器_第1张图片

//程序LOGO

功能齐全界面漂亮的c#音乐播放器_第2张图片
//XML播放列表
功能齐全界面漂亮的c#音乐播放器_第3张图片



//系统托盘最小化显示

功能齐全界面漂亮的c#音乐播放器_第4张图片


//查找功能
功能齐全界面漂亮的c#音乐播放器_第5张图片



源码地址:http://wuyidexinsheng.download.csdn.net/

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Media;
using System.Xml;

namespace StarMusicPlayer
{
    public partial class FormMain : Form
    {
        WMPLib.WindowsMediaPlayerClass MediaPlayer;
        string xmlMediaListPath = System.Environment.CurrentDirectory+"//MediaList.xml";//MediaList.xml播放列表的存取位置
        Point mousePoint;//按住标题移动窗体时,mousePoint记录鼠标按下时的位置
        bool FormShowState;//窗体显示状态false为正常true为最小化
        int screens = 0;//播放列表的显示屏数
        string playMode = "随机播放";//播放模式
        public FormMain()
        {
            InitializeComponent();
            this.ShowInTaskbar = false;
            MediaPlayer = new WMPLib.WindowsMediaPlayerClass();
            loadMediaList(xmlMediaListPath);
            //MediaPlayer.settings.setMode("loop", true);
            for (int i = 0; i < listView.Items.Count; i++)
            {
                if (System.IO.File.Exists(listView.Items[0].SubItems[3].Text))
                {
                    PlayingMedia(listView.Items[0].SubItems[3].Text);
                    listView.Items[0].Selected = true;
                    break;
                }
                else
                    listView.Items[0].Remove();
            }
        }

        private void label_Click(object sender, MouseEventArgs e)
        {
            Label l = (Label)sender;
            switch (l.Tag.ToString())
            {
                case "closeMainForm": this.Close(); break;
                case "min": this.WindowState = FormWindowState.Minimized; break;
                case "prev": prevOperation(); break;
                case "play": playOperation(); break;
                case "pause": pauseOperation(); break;
                case "stop": stopOperation(); break;
                case "next": nextOperation(); break;
                case "open": openOperation(); break;
                case "showList": showListOperation(); break;
                case "closeList": closeListOperation(); break;
                case "close_List": closeListOperation(); break;
                case "about": aboutOperation(); break;                   
            }
        }

        private void label_MouseDown(object sender, MouseEventArgs e)
        {
            Label l = (Label)sender;
            switch (l.Tag.ToString())
            {
                case "closeMainForm": l.Image = StarMusicPlayer.Properties.Resources.close_down; break;
                case "min": l.Image = StarMusicPlayer.Properties.Resources.min_down; break;
                case "prev": l.Image = StarMusicPlayer.Properties.Resources.prev_down; break;
                case "play": l.Image = StarMusicPlayer.Properties.Resources.play_down; break;
                case "pause": l.Image = StarMusicPlayer.Properties.Resources.pause_down; break;
                case "stop": l.Image = StarMusicPlayer.Properties.Resources.stop_down; break;
                case "next": l.Image = StarMusicPlayer.Properties.Resources.next_down; break;
                case "open": l.Image = StarMusicPlayer.Properties.Resources.open_down; break;
                case "showList": l.Image = StarMusicPlayer.Properties.Resources.list_down; break;
                case "about": l.Image = StarMusicPlayer.Properties.Resources.about_down; break;
                case "close_List": l.Image = StarMusicPlayer.Properties.Resources.close_down; break;
                case "closeList": l.Image = StarMusicPlayer.Properties.Resources.list_down; break;
            }
        }

        private void label_MouseEnter(object sender, EventArgs e)
        {
            Label l = (Label)sender;
            switch (l.Tag.ToString())
            {
                case "closeMainForm": l.Image = StarMusicPlayer.Properties.Resources.close_over; break;
                case "min": l.Image = StarMusicPlayer.Properties.Resources.min_over; break;
                case "prev": l.Image = StarMusicPlayer.Properties.Resources.prev_over; break;
                case "play": l.Image = StarMusicPlayer.Properties.Resources.play_over; break;
                case "pause": l.Image = StarMusicPlayer.Properties.Resources.pause_over; break;
                case "stop": l.Image = StarMusicPlayer.Properties.Resources.stop_over; break;
                case "next": l.Image = StarMusicPlayer.Properties.Resources.next_over; break;
                case "open": l.Image = StarMusicPlayer.Properties.Resources.open_over; break;
                case "showList": l.Image = StarMusicPlayer.Properties.Resources.list_over; break;
                case "about": l.Image = StarMusicPlayer.Properties.Resources.about_over; break;
                case "close_List": l.Image = StarMusicPlayer.Properties.Resources.close_over; break;
                case "closeList": l.Image = StarMusicPlayer.Properties.Resources.list_over; break;
            }
        }

        private void label_MouseLeave(object sender, EventArgs e)
        {
            Label l = (Label)sender;
            switch (l.Tag.ToString())
            {
                case "closeMainForm": l.Image = StarMusicPlayer.Properties.Resources.close; break;
                case "min": l.Image = StarMusicPlayer.Properties.Resources.min; break;
                case "prev": l.Image = StarMusicPlayer.Properties.Resources.prev; break;
                case "play": l.Image = StarMusicPlayer.Properties.Resources.play; break;
                case "pause": l.Image = StarMusicPlayer.Properties.Resources.pause; break;
                case "stop": l.Image = StarMusicPlayer.Properties.Resources.stop; break;
                case "next": l.Image = StarMusicPlayer.Properties.Resources.next; break;
                case "open": l.Image = StarMusicPlayer.Properties.Resources.open; break;
                case "showList": l.Image = StarMusicPlayer.Properties.Resources.list; break;
                case "about": l.Image = StarMusicPlayer.Properties.Resources.about; break;
                case "close_List": l.Image = StarMusicPlayer.Properties.Resources.close; break;
                case "closeList": l.Image = StarMusicPlayer.Properties.Resources.list; break;
            }
        }

        private void PlayingMedia(String MediaPath)
        {
            MediaPlayer.URL = MediaPath;
            MediaPlayer.play();
            timer.Enabled = true;
            label_playORpause.Image = StarMusicPlayer.Properties.Resources.pause;
            label_playORpause.Tag = "pause";
        }

        private void prevOperation()
        {
            if ((int)listView.SelectedItems[0].Index - 1 < 0)
            {
                listView.Items[listView.Items.Count - 1].Selected = true;
                listView.Items[0].Selected = false;
                PlayingMedia(listView.SelectedItems[0].SubItems[3].Text);
            }
            else
            {
                listView.Items[(int)listView.SelectedItems[0].Index - 1].Selected = true;
                listView.Items[(int)listView.SelectedItems[1].Index].Selected = false;
                PlayingMedia(listView.SelectedItems[0].SubItems[3].Text);               
            }
        }

        private void playOperation()
        {
            MediaPlayer.controls.play();
            label_playORpause.Image = StarMusicPlayer.Properties.Resources.pause;
            label_playORpause.Tag = "pause";
        }

        private void pauseOperation()
        {
            MediaPlayer.controls.pause();
            label_playORpause.Image = StarMusicPlayer.Properties.Resources.play;
            label_playORpause.Tag = "play";
        }

        private void stopOperation()
        {
            MediaPlayer.controls.stop();
            label_playORpause.Image = StarMusicPlayer.Properties.Resources.play;
            label_playORpause.Tag = "play";
        }

        private void nextOperation()
        {
           switch(playMode)
           {
               case "随机播放":
                   {
                       for(int i=0;i                           listView.SelectedItems[0].Selected=false;
                       Random ran =new Random ();
                       int j=ran.Next(listView.Items.Count);
                       listView.Items[j].Selected=true;
                       PlayingMedia(listView.SelectedItems[0].SubItems[3].Text);
                       if(j>5)
                           listView.TopItem=listView.Items[j-5];
                       else
                           listView.TopItem=listView.Items[j];
                   }
                   break;
               case "顺序循环":
                   {

                       if (listView.SelectedItems.Count == 0)
                            listView.Items[0].Selected = true;
                       if ((int)listView.SelectedItems[0].Index + 1 > listView.Items.Count - 1)
                       {
                            listView.Items[0].Selected = true;
                            listView.Items[listView.Items.Count - 1].Selected = false;
                            PlayingMedia(listView.Items[0].SubItems[3].Text);
                            if (System.IO.File.Exists(listView.SelectedItems[0].SubItems[3].Text))
                                PlayingMedia(listView.SelectedItems[0].SubItems[3].Text);
                            else
                            {
                                listView.Items[(int)(listView.SelectedItems[0].Index)+1].Selected = true;
                                listView.SelectedItems[0].Remove();
                                PlayingMedia(listView.SelectedItems[0].SubItems[3].Text);
                            }
                       }
                       else
                       {
                            listView.Items[(int)listView.SelectedItems[0].Index + 1].Selected = true;
                            listView.Items[(int)listView.SelectedItems[0].Index].Selected = false;
                            if (System.IO.File.Exists(listView.SelectedItems[0].SubItems[3].Text))
                                PlayingMedia(listView.SelectedItems[0].SubItems[3].Text);
                            else
                            {
                                listView.Items[(int)(listView.SelectedItems[0].Index)+1].Selected = true;
                                listView.SelectedItems[0].Remove();
                                PlayingMedia(listView.SelectedItems[0].SubItems[3].Text);
                            }
                       }
                   }
                   break;
               case "单曲循环":
                   {
                       PlayingMedia(listView.SelectedItems[0].SubItems[3].Text);
                   }
                   break;
               case "单曲播放":
                   {
                       PlayingMedia(listView.SelectedItems[0].SubItems[3].Text);
                       playMode=string.Empty;
                   }
                   break;
           }
        }

        private void openOperation()
        {
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                string[] mediaNames = openFileDialog.FileNames;
                listView.Items.Clear();
                for (int i = 0; i < mediaNames.Length; i++)
                {
                    listView.Items.Add(i.ToString());//i为歌曲序号
                    listView.Items[i].SubItems.Add(MediaPlayer.newMedia(mediaNames[i]).name);
                    listView.Items[i].SubItems.Add(MediaPlayer.newMedia(mediaNames[i]).durationString);
                    listView.Items[i].SubItems.Add(MediaPlayer.newMedia(mediaNames[i]).sourceURL);
                }
                PlayingMedia(mediaNames[0]);
                listView.Items[0].Selected = true;
                saveMediaList(mediaNames,xmlMediaListPath);
            }
        }

        private void showListOperation()
        {
            this.Size = new Size(389, 400);
            label_list.Tag = "closeList";
        }

        private void closeListOperation()
        {
            this.Size = new Size(389, 140);
            label_list.Tag = "showList";
        }

        private void aboutOperation()
        {
            MessageBox.Show("特别感谢百度小花朵提供参考程序/n本程序作者:伍新生,QQ:497596754 Email:QQ号[email protected]/n"+
                            "参考作品:小花朵MP3播放器,作者:百度小花朵。Email:[email protected]/n"+
                            "开发时间:2010-12-01/n开发语言:c#(VS2005)/n对初学c#的同学,要想学好c#:多做。多加c#群,Never give up!/n推荐网站:www.codeproject.com","关于",MessageBoxButtons.OK,MessageBoxIcon.Information);
        }

        private void saveMediaList(string[] mediaNames,string MediaListPath)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            settings.IndentChars = "  ";
            XmlWriter xml = XmlWriter.Create(MediaListPath, settings);
            xml.WriteStartElement("MediaList");
            foreach (string mediaPath in mediaNames)
            {
                xml.WriteElementString("Media", mediaPath);
            }
            xml.WriteEndElement();
            xml.Flush();
            xml.Close();
        }

        private void loadMediaList(string MediaListPath)
        {
            if (listView.Items.Count > 0)
                listView.Items.Clear();
            XmlDocument xml_mediaList = new XmlDocument();
            if(System.IO.File.Exists(MediaListPath))
            {
                xml_mediaList.Load(MediaListPath);
                XmlNodeList nodelist = xml_mediaList.SelectSingleNode("MediaList").ChildNodes;
                int i = 0;//歌曲序号
                foreach (XmlNode node in nodelist)
                {
                    listView.Items.Add(i.ToString());//i为歌曲序号
                    listView.Items[i].SubItems.Add(MediaPlayer.newMedia(node.InnerText).name);
                    listView.Items[i].SubItems.Add(MediaPlayer.newMedia(node.InnerText).durationString);
                    listView.Items[i].SubItems.Add(MediaPlayer.newMedia(node.InnerText).sourceURL);
                    i++;
                }
            }
        }

        private void listView_DoubleClick(object sender, EventArgs e)
        {
            PlayingMedia(listView.SelectedItems[0].SubItems[3].Text);
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            if (MediaPlayer.currentMedia.getItemInfo("Author") == "")
                label_mediaInfo.Text = MediaPlayer.currentMedia.getItemInfo("name");
            else
                label_mediaInfo.Text = MediaPlayer.currentMedia.getItemInfo("Author") + "-" + MediaPlayer.currentMedia.getItemInfo("name");

            if (this.TopMost)
            {
                if (MediaPlayer.currentMedia.getItemInfo("Author") != "")
                {
                    label_caption.Text = MediaPlayer.currentMedia.getItemInfo("Author") + ":" +
                        MediaPlayer.currentMedia.getItemInfo("Title");
                }
                else
                {
                    label_caption.Text = MediaPlayer.currentMedia.getItemInfo("Title");
                }
            }
            label_duration.Text = MediaPlayer.currentMedia.durationString;
            double percent = MediaPlayer.controls.currentPosition / MediaPlayer.currentMedia.duration;
            label_percent.Text = percent.ToString("P");
            label_time.Text = MediaPlayer.controls.currentPositionString;
            mediaSlider_progressBar.Value = (int)(percent * 100);
            mediaSlider_volumeBar.Value = MediaPlayer.volume;
            if ((int)MediaPlayer.currentMedia.duration-(int)MediaPlayer.controls.currentPosition==1)
                nextOperation();
        }

        private void mediaSlider_volumeBar_ValueChanged(object sender, EventArgs e)
        {
            MediaPlayer.volume = mediaSlider_volumeBar.Value;
        }

        private void mediaSlider_progressBar_Scrolled(object sender, EventArgs e)
        {
            MediaPlayer.controls.currentPosition = MediaPlayer.currentMedia.duration * mediaSlider_progressBar.Value / 100;
        }

        private void label_caption_MouseDown(object sender, MouseEventArgs e)
        {
            mousePoint = new Point(e.X, e.Y);
        }

        private void label_caption_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.SetBounds(this.Location.X + e.X - mousePoint.X, this.Location.Y + e.Y - mousePoint.Y, this.Width, this.Height);
            }
        }
        private void MainForm_ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem menu = (ToolStripMenuItem)sender;
            switch (menu.Text)
            {
                case "随机播放":
                    {
                        resetPlayMode("随机播放");
                        随机播放ToolStripMenuItem.Checked = true;
                    } break;
                case "顺序循环":
                    {
                        resetPlayMode("顺序循环");
                        顺序循环ToolStripMenuItem.Checked = true;
                    }
                     break;
                case "单曲循环":
                    {
                        resetPlayMode("单曲循环");
                        单曲循环ToolStripMenuItem.Checked = true;
                    }
                     break;
                 case "单曲播放":
                     {
                         resetPlayMode("单曲播放");
                         单曲播放ToolStripMenuItem.Checked = true;
                     }
                     break;
                case "播放": playOperation(); break;
                case "暂停": pauseOperation(); break;
                case "停止": stopOperation(); break;
                case "上一曲": prevOperation(); break;
                case "下一曲": nextOperation(); break;
                case "打开": openOperation(); break;
                case "关闭": this.Close(); break;
                case "最小化": this.WindowState = FormWindowState.Minimized; break;
                case "关于": aboutOperation(); break;
                case "精简模式": label_mini_Click(null, null); break;
                case "快捷键说明": MessageBox.Show("F1:最小化/nF2:精简模式/n向左键:上一曲/n向右键:下一曲/nEnter:确定键/n空格键:开始、暂停"); break;
                case "100%":
                    {
                        resetFormOpacity(1.0);
                        toolStripMenuItem2.Checked = true;
                    } break;
                case "90%":
                    {
                        resetFormOpacity(0.9);
                        toolStripMenuItem3.Checked = true;
                    } break;
                case "80%":
                    {
                        resetFormOpacity(0.8);
                        toolStripMenuItem4.Checked = true;
                    } break;
                case "70%":
                    {
                        resetFormOpacity(0.7);
                        toolStripMenuItem5.Checked = true;
                    } break;
                case "60%":
                    {
                        resetFormOpacity(0.6);
                        toolStripMenuItem6.Checked = true;
                    } break;
                case "50%":
                    {
                        resetFormOpacity(0.5);
                        toolStripMenuItem7.Checked = true;
                    } break;
                case "40%":
                    {
                        resetFormOpacity(0.4);
                        toolStripMenuItem8.Checked = true;
                    } break;
                case "30%":
                    {
                        resetFormOpacity(0.3);
                        toolStripMenuItem9.Checked = true;
                    } break;
                case "20%":
                    {
                        resetFormOpacity(0.2);
                        toolStripMenuItem10.Checked = true;
                    } break;
                case "10%":
                    {
                        resetFormOpacity(0.1);
                        toolStripMenuItem11.Checked = true;
                    } break;
                case "0%":
                    {
                        resetFormOpacity(0.0);
                        toolStripMenuItem12.Checked = true;
                    } break;
            }
        }

        private void resetFormOpacity(double opacity)
        {
            toolStripMenuItem2.Checked = false;
            toolStripMenuItem3.Checked = false;
            toolStripMenuItem4.Checked = false;
            toolStripMenuItem5.Checked = false;
            toolStripMenuItem6.Checked = false;
            toolStripMenuItem7.Checked = false;
            toolStripMenuItem8.Checked = false;
            toolStripMenuItem9.Checked = false;
            toolStripMenuItem10.Checked = false;
            toolStripMenuItem11.Checked = false;
            toolStripMenuItem12.Checked = false;
            this.Opacity = opacity;
        }

        private void resetPlayMode(string playmode)
        {
            随机播放ToolStripMenuItem.Checked = false;
            顺序循环ToolStripMenuItem.Checked = false;
            单曲循环ToolStripMenuItem.Checked = false;
            单曲播放ToolStripMenuItem.Checked = false;
            playMode = playmode;
        }

        private void MediaList_ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem menu = (ToolStripMenuItem)sender;
            switch (menu.Text)
            {
                case "添加": AddMedia(); break;
                case "删除(仅列表)": DelMedia(); break;
                case "删除列表及文件": DelMediaANDfile(); break;
                case "删除所有错误文件": DelerrorMedias(); break;
                case "导入新播放列表": overLoadMediaList(); break;
                case "导出当前列表": saveCurrentMediaList(); break;
                case "查找": SearchOperation(); break;
            }
        }


        private void AddMedia()
        {
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                string[] fileNames = openFileDialog.FileNames;
                XmlDocument xml = new XmlDocument();
                xml.Load(xmlMediaListPath);
                XmlNode node = xml.SelectSingleNode("MediaList").FirstChild;
                for (int i = 0; i < fileNames.Length; i++)
                {
                    listView.Items.Add(listView.Items.Count.ToString());
                    listView.Items[(int)listView.Items.Count-1].SubItems.Add(MediaPlayer.newMedia(fileNames[i]).name);
                    listView.Items[(int)listView.Items.Count-1].SubItems.Add(MediaPlayer.newMedia(fileNames[i]).durationString);
                    listView.Items[(int)listView.Items.Count-1].SubItems.Add(MediaPlayer.newMedia(fileNames[i]).sourceURL);
                    node.InnerText = fileNames[i];
                    xml.SelectSingleNode("MediaList").A(node);
                }
                xml.Save(xmlMediaListPath);
            }
        }

        private void DelMedia()
        {
            if (listView.SelectedItems.Count > 0)
            {
                XmlDocument xml = new XmlDocument();
                xml.Load(xmlMediaListPath);

                for (int i = 0; i < listView.SelectedItems.Count; i++)
                {
                    foreach (XmlNode node in xml.SelectSingleNode("MediaList").ChildNodes)
                    {
                        if (node.InnerText == listView.SelectedItems[i].SubItems[3].Text)
                        {
                            xml.SelectSingleNode("MediaList").RemoveChild(node);
                            break;
                        }
                    }
                }
                xml.Save(xmlMediaListPath);
                int count = listView.SelectedItems.Count;
                for (int i = 0; i < count; i++)
                {
                    listView.SelectedItems[0].Remove();
                }
            }
        }

        private void DelMediaANDfile()
        {
            if (listView.SelectedItems.Count > 0&&MessageBox.Show("你确定要删除源文件吗?","提示",MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.Yes)
            {
                XmlDocument xml = new XmlDocument();
                xml.Load(xmlMediaListPath);

                for (int i = 0; i < listView.SelectedItems.Count; i++)
                {
                    System.IO.File.Delete(listView.SelectedItems[i].SubItems[3].Text);
                    foreach (XmlNode node in xml.SelectSingleNode("MediaList").ChildNodes)
                    {
                        if (node.InnerText == listView.SelectedItems[i].SubItems[3].Text)
                        {
                            xml.SelectSingleNode("MediaList").RemoveChild(node);
                            break;
                        }
                    }
                }

                xml.Save(xmlMediaListPath);
                int count = listView.SelectedItems.Count;
                for (int i = 0; i < count; i++)
                {
                    listView.SelectedItems[0].Remove();
                }
            }
        }

        private void DelerrorMedias()
        {
            int errorNum = 0;
            for (int i = 0; i < listView.Items.Count; i++)
            {
                if(System.IO.File.Exists(listView.Items[i].SubItems[3].Text))
                    continue;
                else
                {
                    System.IO.File.Delete(listView.Items[i].SubItems[3].Text);
                    XmlDocument xml = new XmlDocument();
                    xml.Load(xmlMediaListPath);
                    XmlNodeList nodelist = xml.SelectSingleNode("MediaList").ChildNodes;
                    foreach (XmlNode node in nodelist)
                    {
                        if (node.InnerText == listView.Items[i].SubItems[3].Text)
                        {
                            xml.SelectSingleNode("MediaList").RemoveChild(node);
                            break;
                        }
                    }
                    xml.Save(xmlMediaListPath);
                    listView.Items[i].Remove();
                    i--;
                    errorNum++;
                }
            }
            MessageBox.Show("共删除了" + errorNum + "条错误信息!");
        }

        private void overLoadMediaList()
        {
            openFileDialog = new OpenFileDialog();
            openFileDialog.Multiselect = false;
            openFileDialog.InitialDirectory = System.Environment.CurrentDirectory;
            openFileDialog.Filter = @"*.xml|*.xml";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                loadMediaList(openFileDialog.FileName);
            }
        }

        private void saveCurrentMediaList()
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = @"*.xml|*.xml";
            sfd.FileName = "请在此输入播放列表名称";
            sfd.InitialDirectory = System.Environment.CurrentDirectory;
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                string[] fileNames = new string[listView.Items.Count];
                for (int i = 0; i < listView.Items.Count; i++)
                {
                    fileNames[i] = listView.Items[i].SubItems[3].Text;
                }
                saveMediaList(fileNames, sfd.FileName);
            }
        }

        private void SearchOperation()
        {
            textBox_search.Visible=true;
            button_OK.Visible = true;
        }

        private void button_OK_Click(object sender, EventArgs e)
        {
            if (listView.SelectedItems.Count > 0)
            {
                for (int i = 0; i < listView.SelectedItems.Count; i++)
                    listView.SelectedItems[0].Selected = false;
            }
            if (listView.FindItemWithText(textBox_search.Text, true, 0)!=null)
            {
                listView.FindItemWithText(textBox_search.Text, true, 0).Selected = true;  
                listView.TopItem = listView.SelectedItems[0];               
            }
            else
                MessageBox.Show("没有找到与 "+textBox_search.Text+" 相关的歌曲!");
            textBox_search.Visible = false;
            button_OK.Visible = false;
        }

        private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (!FormShowState)
                {
                    this.WindowState = FormWindowState.Minimized;
                    FormShowState = !FormShowState;
                }
                else
                {
                    this.WindowState = FormWindowState.Normal;
                    FormShowState = !FormShowState;
                }
            }
        }

        private void ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem menu = (ToolStripMenuItem)sender;
            switch (menu.Text)
            {
                case "关闭": this.Close(); break;
                case "关于": aboutOperation(); break;
                case "显示":
                    {
                        this.WindowState = FormWindowState.Normal;
                        this.Opacity = 1.0;
                    } break;
            }
        }

        private void pic_Click(object sender, EventArgs e)
        {
            PictureBox pic = (PictureBox)sender;
            switch (pic.Tag.ToString())
            {
                case "plus": AddMedia(); break;
                case "reduce": DelMedia(); break;
                case "prev": list_Prev(); break;
                case "next": list_next(); break;
                case "dir": overLoadMediaList(); break;//目录
                case "clr": DelerrorMedias(); break;//清除
                case "srt": break;//字幕
                case "sav": SearchOperation(); break;
            }
        }

        private void  list_Prev()
        {
            if (screens > 0)
            {
                screens--;
                listView.TopItem = listView.Items[screens * 14];
            }
            else
                MessageBox.Show("已经到最顶端了!");
        }

        private void list_next()
        {
            if (screens * 14 < listView.Items.Count)
            {
                screens++;
                listView.TopItem = listView.Items[screens * 14];
            }
            else
                MessageBox.Show("已经到最底端了!");
        }

        private void pic_MouseEnter(object sender, EventArgs e)
        {
            PictureBox pic = (PictureBox)sender;
            switch (pic.Tag.ToString())
            {
                case "plus": pic.Image = StarMusicPlayer.Properties.Resources.plus; break;
                case "reduce": pic.Image = StarMusicPlayer.Properties.Resources.reduce; break;
                case "prev": pic.Image = StarMusicPlayer.Properties.Resources.prev1; break;
                case "next": pic.Image = StarMusicPlayer.Properties.Resources.next1; break;
                case "dir": pic.Image = StarMusicPlayer.Properties.Resources.music_dir_in; break;
                case "clr": pic.Image = StarMusicPlayer.Properties.Resources.music_clr_in; break;
                case "srt": pic.Image = StarMusicPlayer.Properties.Resources.music_srt_in; break;
                case "sav": pic.Image = StarMusicPlayer.Properties.Resources.music_sav_in; break;
            }
        }

        private void pic_MouseLeave(object sender, EventArgs e)
        {
            PictureBox pic = (PictureBox)sender;
            switch (pic.Tag.ToString())
            {
                case "plus": pic.Image = StarMusicPlayer.Properties.Resources.项目5; break;
                case "reduce": pic.Image = StarMusicPlayer.Properties.Resources.项目6; break;
                case "prev": pic.Image = StarMusicPlayer.Properties.Resources.项目7; break;
                case "next": pic.Image = StarMusicPlayer.Properties.Resources.项目8; break;
                case "dir": pic.Image = StarMusicPlayer.Properties.Resources.music_dir; break;
                case "clr": pic.Image = StarMusicPlayer.Properties.Resources.music_clr; break;
                case "srt": pic.Image = StarMusicPlayer.Properties.Resources.music_srt; break;
                case "sav": pic.Image = StarMusicPlayer.Properties.Resources.music_sav; break;
            }
        }

        private void FormMain_KeyDown(object sender, KeyEventArgs e)
        {           
            switch (e.KeyCode)
            {
                case Keys.Enter: button_OK_Click(null, null); break;
                case Keys.Delete: DelMedia(); break;
                case Keys.Space:
                    {
                        if (MediaPlayer.playState == WMPLib.WMPPlayState.wmppsPaused)
                            playOperation();
                        else
                            pauseOperation();
                    } break;
                case Keys.Left: prevOperation();break;
                case Keys.Right: nextOperation();break;
                case Keys.F1: label_mini_Click(null, null);break;
                case Keys.F2:
                    {
                        if (this.WindowState == FormWindowState.Minimized)
                            this.WindowState = FormWindowState.Normal;
                        else
                            this.WindowState = FormWindowState.Minimized;
                    }
                    break;
            }
        }

        private void label_mini_Click(object sender, EventArgs e)
        {           
            this.TopMost = !this.TopMost;
            if (TopMost)
            {
                label_prev.Location = new Point(14, 3);
                label_playORpause.Location = new Point(40, 3);
                label_stop.Location = new Point(66, 3);
                label_next.Location = new Point(92, 3);
                pictureBox_logo.Visible = false;
                label_caption.Size = new Size(200, 23);
                label_caption.Location = new Point(118, 4);
                this.Size = new Size(389, 27);
                this.SetBounds((System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width - this.Width) / 2, 0, this.Width, this.Height);
                resetFormOpacity(0.5);
                toolStripMenuItem7.Checked = true;
                label_caption.TextAlign = ContentAlignment.MiddleCenter;
                timer1.Enabled = true;
            }
            else
            {
                label_prev.Location = new Point(14, 88);
                label_playORpause.Location = new Point(40, 88);
                label_stop.Location = new Point(66, 88);
                label_next.Location = new Point(92, 88);
                pictureBox_logo.Visible = true;
                label_caption.Size = new Size(283,23);
                label_caption.Location = new Point(37,4);
                this.Size = new Size(389, 400);
                this.StartPosition = FormStartPosition.CenterScreen;
                resetFormOpacity(1.0);
                toolStripMenuItem2.Checked = true;
                label_caption.Text = "星星音乐播放器";
                label_caption.TextAlign = ContentAlignment.MiddleLeft;
                label_caption.ForeColor = Color.ForestGreen;
                timer1.Enabled = false;
            }
        }

        private void label_mini_MouseEnter(object sender, EventArgs e)
        {
            label_mini.Image = StarMusicPlayer.Properties.Resources._Enter;
        }

        private void label_mini_MouseLeave(object sender, EventArgs e)
        {
            label_mini.Image = StarMusicPlayer.Properties.Resources._Leave;
        }

        private void label_mini_MouseDown(object sender, MouseEventArgs e)
        {
            label_mini.Image = StarMusicPlayer.Properties.Resources._click;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Random ran = new Random();
            int r = ran.Next(50,255);
            int g = ran.Next(100,255);
            int b = ran.Next(100,255);
            label_caption.ForeColor = Color.FromArgb(r, g, b);
        }
    }
}

你可能感兴趣的:(功能齐全界面漂亮的c#音乐播放器)