c#实现文件管理系统案例

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Media;
using System.Drawing.Drawing2D;


namespace 音乐播放器2
{
    public partial class Form1 : Form
    {
        public Form1()   
        {
            InitializeComponent();
        }
        Bitmap start = new Bitmap(20, 20);
        Bitmap pause = new Bitmap(20, 20);
        Bitmap Last = new Bitmap(20, 20);
        Bitmap Next = new Bitmap(20, 20);
        Graphics graphics1;

        private void Form1_Load(object sender, EventArgs e)
        {
            //设置音量第一种方法:
            //uiMode播放器界面模式设置,可为Full, Mini, None, Invisible
            this.axWindowsMediaPlayer1.uiMode = "None";
            this.trackBar1.Minimum = 0;//设定音量Bar最小值为最小音量值(0)
            this.trackBar1.Maximum = 100;//设定音量Bar最大值为最大音量值(100)
            //设置音量第二种方法:
            graphics1 = panel2.CreateGraphics();
            this.trackBar1.Value = this.axWindowsMediaPlayer1.settings.volume;

            //画音乐的播放进度条
            graphics = panel1.CreateGraphics();
            silderBrush = new LinearGradientBrush(new PointF(0, 0), new PointF(panel1.Width, 0), Color.Red, Color.Gold);
            //获取音乐信息
            openFileDialog1.Filter = "(选择音乐文件)|*.wav;*.mp3";
            DirectoryInfo directroy = new DirectoryInfo(paths);
            FileInfo[] imginfo = directroy.GetFiles();
            foreach (var item in imginfo)
            {
                string kuo = item.Extension;
                if (kuo == ".jpg" || kuo == ".png")
                {
                    bgList.Add(item.Name);
                }
                pictureBox3.Image = start;
                //画音乐播放声音的进度条
                axWindowsMediaPlayer1.settings.volume = 50;
                graphics1.FillRectangle(Brushes.Blue, new RectangleF(0, 0, panel2.Width * 0.5f, panel2.Height));
                label4.Text = axWindowsMediaPlayer1.settings.volume.ToString();
            }
        }
        //音乐路径
        string[] MusicRoad;
        //string path;
        string paths = "../../Img";
        // 装所有的音乐名称
        List<string> muiscList = new List<string>();
        // 装所有音乐的路径
        List<string> musicPath = new List<string>();
        private void button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                MusicRoad = openFileDialog1.FileNames;
                for (int i = 0; i < MusicRoad.Length; i++)
                {
                    FileInfo info = new FileInfo(MusicRoad[i]);
                    bool b = false;
                    if (muiscList.Count > 0)
                    {
                        b = muiscList.Contains(info.Name);
                    }
                    if (b == false)
                    {
                        muiscList.Add(info.Name);
                        listBox1.Items.Add(info.Name);
                        musicPath.Add(info.FullName);
                    }
                } 
            }
        }
        // 记录当前播放的音乐的索引
        int currentIndex = 0;
        private void listBox1_DoubleClick(object sender, EventArgs e)
        {
            object o = listBox1.SelectedItem;
            //获取当前选中歌曲的索引
            //indexMusic = miuscList.IndexOf(o.ToString());
            //PlayMusic(o.ToString());
            currentIndex = muiscList.IndexOf(o.ToString());
            PlayMusic(musicPath[currentIndex]);
            pictureBox3.Image = pause;
        }

        List<string> bgList = new List<string>();
        int bgIndex = 0;
        //设置背景图片
        private void SetBG(string file)
        {
            Image image = Image.FromFile("../../Img/" + file);

            Bitmap bg = new Bitmap(300, 300);
            Graphics graphics = Graphics.FromImage(bg);
            graphics.DrawImage(image, new Rectangle(0, 0, bg.Width, bg.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel);
            pictureBox1.Image = bg;
        }
        //切换背景
        private void 上一张ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            bgIndex++;
            if (bgIndex >= bgList.Count)
            {
                bgIndex = 0;
            }
            SetBG(bgList[bgIndex]);
        }

        private void 下一张ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            bgIndex--;
            if (bgIndex < 0)
            {
                bgIndex = bgList.Count - 1;
            }
            SetBG(bgList[bgIndex]);
        }
        string musicString = "未找到对应歌词!";
        // 装每首歌对应的歌词
        List<string> musicChar = new List<string>();
        List<float> musicTime = new List<float>();
        private void PlayMusic(string o)
        {
            musicChar.Clear();
            musicTime.Clear();
            if (graphics != null)
            {
                graphics.Clear(Color.Cyan);
            }
            axWindowsMediaPlayer1.URL = o;
            axWindowsMediaPlayer1.Ctlcontrols.play();

            int lastIndex = o.LastIndexOf(".");
            //歌词文件
            o = o.Remove(lastIndex) + ".lrc";

            //o已经是歌词文件的完整路径
            try
            {
                StreamReader reader = new StreamReader(o, Encoding.Default);
                musicString = reader.ReadToEnd();
                musicChar.AddRange(musicString.Split('\n'));
                if (musicChar.Count > 0)
                {
                    foreach (var item in musicChar)
                    {
                        string[] str = item.Split('[', ']');
                        string[] timeStr = str[1].Split(':');
                        float times = float.Parse(timeStr[0]) * 60 + float.Parse(timeStr[1]);
                        musicTime.Add(times);
                    }
                }
            }
            catch (Exception)
            {
                musicString = "未找到对应歌词!";
            }
        }
        //设置上一曲下一曲暂停开始按钮
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            //Brush brush = new LinearGradientBrush(new RectangleF(0, 0,textBox1.Width,textBox1.Height), Color.Plum, Color.Blue, 0.0);
            //Graphics graphics = textBox1.CreateGraphics();
            //graphics.DrawString("文字颜色渐变", new Font("宋体", 20, FontStyle.Bold), brush, new PointF(0, 0));
            SetBG("z.jpg");
            Graphics start1 = Graphics.FromImage(start);
            start1.FillEllipse(Brushes.DeepSkyBlue, new RectangleF(0, 0, start.Width, start.Height));
           

            Graphics pause1 = Graphics.FromImage(pause);
            pause1.FillPolygon(Brushes.Red, new Point[] { new Point(0, 0), new Point(20, 0), new Point(20, 20), new Point(0, 20) });

            Graphics Last1 = Graphics.FromImage(Last);
            Last1.FillPolygon(Brushes.Green, new Point[] { new Point(0, 10), new Point(20, 0), new Point(20, 20) });
            pictureBox2.Image = Last;

            Graphics Next1 = Graphics.FromImage(Next);
            Next1.FillPolygon(Brushes.Green, new Point[] { new Point(0, 0), new Point(20, 10), new Point(0, 20) });
            pictureBox4.Image = Next;


        }
        //设置歌曲暂停按钮
        private void pictureBox3_Click(object sender, EventArgs e)
        {
            if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsPlaying)
            {
                axWindowsMediaPlayer1.Ctlcontrols.pause();
                pictureBox3.Image = start;
            }
            else if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsPaused)
            {
                axWindowsMediaPlayer1.Ctlcontrols.play();
                pictureBox3.Image = pause;
            }
        }
        //上一曲
        private void pictureBox2_Click(object sender, EventArgs e)
        {
            currentIndex--;
            if (currentIndex < 0)
            {
                currentIndex = muiscList.Count - 1;
            }
            NextMusic(currentIndex);
        }
        //下一曲
        private void pictureBox4_Click(object sender, EventArgs e)
        {
            currentIndex++;
            if (currentIndex >= muiscList.Count)
            {
                currentIndex = 0;
            }
            NextMusic(currentIndex);
        }
        // 播放下一首音乐
        private void NextMusic(int index)
        {
            if (graphics != null)
            {
                graphics.Clear(Color.Green);
            }
            PlayMusic(musicPath[currentIndex]);
            listBox1.SelectedItem = muiscList[currentIndex];
        }
        //设置进度条
        Graphics graphics;
        Brush silderBrush;
        private void timer1_Tick(object sender, EventArgs e)
        {
            float prent = 0;
            //如果一首歌播放完成会自动为空
            if (axWindowsMediaPlayer1.currentMedia != null)
            {
                label1.Text = axWindowsMediaPlayer1.currentMedia.durationString;
                label2.Text = axWindowsMediaPlayer1.Ctlcontrols.currentPositionString;
                double duration = axWindowsMediaPlayer1.currentMedia.duration;
                double current = axWindowsMediaPlayer1.Ctlcontrols.currentPosition;
                prent = (float)current / (float)duration;
                //尽量在使用对象的时候进行空指针判断
                if (graphics != null && silderBrush != null)
                {
                    graphics.FillRectangle(silderBrush, new RectangleF(0, 0, panel1.Width * prent, panel1.Height));
                }
                if (axWindowsMediaPlayer1.Ctlcontrols.currentPositionString != "")
                {
                    //截取播放的时间
                    string[] currentPosition = axWindowsMediaPlayer1.Ctlcontrols.currentPositionString.Split(':');

                    float nowTime = float.Parse(current.ToString("f2"));
                    if (musicTime.Count == 0)
                    {
                        textBox1.Text = "未找到对应歌词!";
                    }
                    for (int i = 0; i < musicTime.Count; i++)
                    {
                        if (nowTime >= musicTime[i])
                        {
                            string[] charList = musicChar[i].Split(']');
                            if (charList[1] == "")
                            {
                                charList = musicChar[i - 1].Split(']');
                                textBox1.Text = charList[1];

                            }
                            else
                            {
                                textBox1.Text = charList[1];
                            }
                        }
                    }
                }
            }
            ////判断自动进入下一曲
            if (prent >= 0.999f)
            {
                currentIndex++;
                if (currentIndex >= muiscList.Count)
                {
                    currentIndex = 0;
                }
                NextMusic(currentIndex);
            }
        }
        //设置拖动进度条
        private void panel1_MouseClick(object sender, MouseEventArgs e)
        {
                graphics.Clear(Color.Cyan);
                graphics.FillRectangle(silderBrush, new RectangleF(0, 0, e.X, panel1.Height));
                //保证音乐的播放时间一致
                double rate = (double)e.X / panel1.Width;
                double currentTime = axWindowsMediaPlayer1.currentMedia.duration * rate;
                axWindowsMediaPlayer1.Ctlcontrols.currentPosition = currentTime;
        }
        //设置调节音量(第二种声音的方法)
        private void trackBar1_ValueChanged(object sender, EventArgs e)
        {
            ValueChangeSize();
        }
        private void ValueChangeSize()
        {
            this.axWindowsMediaPlayer1.settings.volume = this.trackBar1.Value;
        }
        //加大音量
        private void button2_Click(object sender, EventArgs e)
        {
            graphics1.Clear(Color.Gray);
            int add = this.axWindowsMediaPlayer1.settings.volume +=5;
            ChangeVoice(add);
        }
        private void ChangeVoice(int add)
        {
            float changepro = (float)add / (float)100;
            graphics1.FillRectangle(Brushes.Blue, new RectangleF(0, 0, panel2.Width * changepro, panel2.Height));
            label4.Text = add.ToString();
        }
        //减小音量
        private void button3_Click(object sender, EventArgs e)
        {
            graphics1.Clear(Color.Gray);
            int add=this.axWindowsMediaPlayer1.settings.volume -= 5;
            ChangeVoice(add);
        }
        private void panel2_Paint(object sender, PaintEventArgs e)
        {
            //画声音的进度条
            graphics1.FillRectangle(Brushes.Gray, new RectangleF(0, 0, panel2.Width * 0.5f, panel2.Height));
            label4.Text = axWindowsMediaPlayer1.settings.volume.ToString();
        }
        private void panel2_MouseClick(object sender, MouseEventArgs e)
        {
            graphics1.Clear(Color.Gray);
            graphics1.FillRectangle(Brushes.Blue, new RectangleF(0, 0, e.X, panel2.Height));
            axWindowsMediaPlayer1.settings.volume = Convert.ToInt32(100 * ((double)e.X / panel2.Width));
            label4.Text = axWindowsMediaPlayer1.settings.volume.ToString();
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_43552118/article/details/87109125