zoukankan      html  css  js  c++  java
  • C#作业总结(3)

    接着上面的总结,这部分准备详细的分析一下音乐播放器,当然这里不分析歌词部分,网上完整的代码地址 http://download.csdn.net/detail/zhoupeng39/8256843

    先给出程序的运行图片

    我打算分为一下几个部分

    1.增加音乐-----歌词列表显示

    2.皮肤更换(更换背景图片)

    3.音乐播放Windows Media Player使用

    忘了说了,源程序在播放音乐和歌词检索的时候使用了多线程。

    一。增加音乐 

            private  string[] m_names;          //保存路径
            private int m_CurrentPage = 1;      //当前页数
            //增加按钮的响应 
            private void pictureBoxAdd_Click(object sender, EventArgs e)
            {
                string[] oldFile;                  //保存以前的路径
                string[] newFile;                  //排序 清除重复之后的路径
                OpenFileDialog of = new OpenFileDialog();
                of.InitialDirectory = "c:\";       //默认目录
                of.Filter = "mp3|*.mp3|wav|*.wav";  //文件格式
                of.RestoreDirectory = true;         //保存选中历史目录
                of.FilterIndex = 1;                 
                of.Multiselect = true;             //可以多选 
                if (of.ShowDialog() == DialogResult.OK)
                {
                    int k = 0;
                    int same = 0;                   //记录相同数量
                    //第一次增加 
                    if (m_names == null)
                    {
                        oldFile = new string[of.FileNames.Length];  //分配空间
                        foreach (var i in of.FileNames)
                        {
                            oldFile[k] = i;
                            k++;  //保存旧的路径
                        }
                    }
                    else
                    {
                        oldFile = new string[of.FileNames.Length + m_names.Length];  //分配空间
                        for (int y = 0; y < m_names.Length; y++)
                        {
                            oldFile[k] = m_names[y];
                            k++;  
                        }
                        foreach (var i in of.FileNames)
                        {
                            oldFile[k] = i;
                            k++;  //保存旧的和新的路径
    
                        }
                    }
                    //计算重复的数量
                    for (int i = 0; i < oldFile.Length; i++)
                    {
                        for (int j = i + 1; j < oldFile.Length; j++)
                        {
                            if (oldFile[i] == oldFile[j])
                            {
                                same++;
                            }
                        }
                    }
                    //重复的置空  
                    for (int i = 0; i < oldFile.Length; i++)
                    {
                        for (int j = i + 1; j < oldFile.Length; j++)
                        {
                            if (oldFile[i] == oldFile[j])
                            {
                                oldFile[i] = "null";
                            }
                        }
                    }
                    //消除重复歌曲
                    int w = 0;
                    newFile = new string[oldFile.Length - same];
                    for (int i = 0; i < oldFile.Length; i++)
                    {
                        if (oldFile[i] != "null")
                        {
                            newFile[w] = oldFile[i];
                            w++;
                        }
                    }
                    m_names = newFile;  //保存
                }
                //设置列表
                setMusicList(m_CurrentPage);
            }
    
            private string getFileName(string path)
            {   
                //返回路径
                return System.IO.Path.GetFileNameWithoutExtension(path);
            }
    
            private void setMusicList(int page)
            {
                int pages = 1;   //歌曲列表页数 每页5首
                int index = 0;   //下标
                int noMusic;     //取摸剩下的没有的歌曲
    
                if (m_names != null)
                {
                    labelSongNums.Text = m_names.Length.ToString();
                    Song1.Text = null;  
                    Song2.Text = null;
                    Song3.Text = null;
                    Song4.Text = null;
                    Song5.Text = null;
                    pages = (int)m_names.Length / 5;  //页数
                    index = (page - 1) * 5;           //当前索引
                    noMusic = m_names.Length % 5;     //最后一页的歌曲的个数
                    try
                    {
                        Song1.Text = getFileName(m_names[index + 0]);   //填充
                        Song2.Text = getFileName(m_names[index + 1]);
                        Song3.Text = getFileName(m_names[index + 2]);
                        Song4.Text = getFileName(m_names[index + 3]);
                        Song5.Text = getFileName(m_names[index + 4]);
                    }
                    catch (Exception)
                    {
    
                    }
                    labelPageCount.Text = (m_names.Length / 5 + 1).ToString();  //总页数计算
                }
            }

    二。皮肤更换部分

    1.首先这一块使用Panel控件,这里使用了一张有透明通道的背景图片,直接做出了效果,之前没想过

    2.然后在里面添加PictureBox控件,然后增加图片,增加图片点击的事件

    3.正常情况下隐藏该部分,点击Skin时显示该部分

    代码如下

            private bool m_picEnter = false;   //增加了鼠标移入移出图片出现边框的效果
            private void pictureBox1_MouseEnter(object sender, EventArgs e)
            {
                m_picEnter = true;
                PictureBox pic = sender as PictureBox;
                pic.Invalidate();       //重绘
            }
            private void pictureBox1_MouseLeave(object sender, EventArgs e)
            {
                m_picEnter = false;
                PictureBox pic = sender as PictureBox;
                pic.Invalidate();
            }
            private void pictureBox1_Paint(object sender, PaintEventArgs e)
            {
                PictureBox pic = sender as PictureBox;
                Rectangle rect=pic.ClientRectangle;   //获取绘制区域
                if (m_picEnter)
                {
                    e.Graphics.FillRectangle(new SolidBrush(Color.Silver), rect);         //绘制边框
                    e.Graphics.DrawImage(pic.BackgroundImage, new Rectangle(rect.Left + 2, rect.Top + 2, rect.Width - 4, rect.Height - 4)); //绘制图片
                }
                else
                    e.Graphics.DrawImage(pic.BackgroundImage, rect); //直接绘制图片
            }
            // 点击部分 更换 背景
            private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
            {
                PictureBox pic = sender as PictureBox;
                this.BackgroundImage = pic.BackgroundImage;  //直接更换背景图片
            }
    
            //选择背景图片部分
            private string picfile;     //保存copy源
            private string picName;
            private void pictureBox6_Click(object sender, EventArgs e)
            {
                if (Directory.Exists(".\Background") == false)   //检查背景文件夹是否存在
                {
                    Directory.CreateDirectory(".\Background");  //创建背景文件
                    OpenFileDialog of1 = new OpenFileDialog();
                    of1.InitialDirectory = "c:\";
                    of1.Filter = "png|*.png|jpg|*.jpg|bmp|*.bmp";
                    of1.RestoreDirectory = true;
                    of1.FilterIndex = 1;
                    if (of1.ShowDialog() == DialogResult.OK)
                    {
                        picfile = of1.FileName;         //获取文件路径
                        picName = of1.SafeFileName;
                        try
                        {
                            File.Copy(picfile, string.Format("Background\{0}", picName, true));  //将选择的图片放到背景文件夹里面
                        }
                        catch (Exception)
                        {
                        }
                        this.BackgroundImage = Image.FromFile(string.Format("Background\{0}", picName));  //设置背景图片
                    }
                }
                else
                {
                    OpenFileDialog of = new OpenFileDialog();   //直接打开
                    of.InitialDirectory = "c:\";
                    of.Filter = "png|*.png|jpg|*.jpg|bmp|*.bmp";
                    of.RestoreDirectory = true;
                    of.FilterIndex = 1;
                    if (of.ShowDialog() == DialogResult.OK)
                    {
                        picfile = of.FileName;
                        picName = of.SafeFileName;
                        try
                        {
                            File.Copy(picfile, string.Format("Background\{0}", picName, true));    //存储
                        }
                        catch (Exception)
                        {
                        }
                        this.BackgroundImage = Image.FromFile(string.Format("Background\{0}", picName));  //设置
                    }
                }
                panelSkin.Visible = false;  //皮肤容器消失
            }

    三。Windows Media Player控件的使用

    关于这个播放器的使用,我不想说什么,直接给出相关代码

            public void getmusicTime()
            {
                thistime = this.axWindowsMediaPlayer1.Ctlcontrols.currentPosition;   //当前位置
                alltime = this.axWindowsMediaPlayer1.currentMedia.duration;          //所有时间
                bfb = thistime / alltime;    //比例
                thisX = 310 * bfb;           //白色的长度
                panelProgressUp.Size = new Size((int)thisX, 3);  
            }
            public void play(string namepath)
            {
                this.axWindowsMediaPlayer1.URL = namepath;                       //设置路径
                labelMusic.Text = this.axWindowsMediaPlayer1.currentMedia.name;  //获取音乐名称
                getNum();                                                        //标题移动  获取基本参数
                m_IsPlay = true;
                timer3.Enabled = true;  
                getmusicTime();
                if (m_IsPlay)
                {
                    pictureBoxPause.BackgroundImage = ImgElementPlay[0];         //设置播放图片
                }
            }

    其实这个程序的核心在歌词部分,但是不得不说能用简单的控件做出这样的效果还是很不错的,至少我用MFC是要花很长时间才能写出来。

    歌词部分我也看了,难点在

    1.LRC文件的解析(每一行有对应的显示时间和歌词)

    2.从网站获取歌词  这部分要分析从网站反馈回来的歌词流,其实看看还是能看懂的

    其他的就没有什么了。

  • 相关阅读:
    day22-20180522笔记
    day20-20180517笔记
    day19-20180515笔记
    day18-20180513笔记
    day17-20180510笔记
    day16-20180508笔记
    Python之初识面向对象
    Python之常用模块(2)
    Python之常用模块(1)
    Python之模块与包(下)
  • 原文地址:https://www.cnblogs.com/fightfuture/p/4162028.html
Copyright © 2011-2022 走看看