c#基於winform制作音樂播放器

前言:項目是c#的winform 寫的,使用的播放器是基於AxWindowsMediaPlayer。

AxWindowsMediaPlayer的方法

1 首先新建一個頁面 如圖所示: 圖片左側是列表 使用listview 右側是背景圖片。圖片框框的地方是後面可以實現的,+和-按鈕分別代表添加文件和刪除文件 還有就是控制播放的順序。下面的分別是修改歌詞的字體 和展示/隱藏

2 新建一個透明的歌詞頁面[窗體]

3 新建一個半透明的頁面[窗體]

4 業務代碼

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WMPLib;
using System.Media;
using System.IO;
using System.Text.RegularExpressions;
using AxWMPLib;
using System.Drawing.Drawing2D;
using CCWin;


namespace KenMusicPlayer
{
 public partial class MusicPlayer : Skin_DevExpress
 {


  public int index = 1;
  public int listIndex;
  private bool first_in = true; //是否第一次進入歌詞區域
  private bool showLrc = true;//默認顯示歌詞
  private int imageInd = 0;//播放的圖片下標
  private List<string> imageList;//播放的圖片
  private Point closePoint;//關閉按鈕的位置
  private Size dfSize;//最初的位置


  //聲音
  SoundPlayer player = new SoundPlayer();
  Dictionary<string, string> dic = new Dictionary<string, string>();

  //播放列表
  Dictionary<string, IWMPMedia> playListDict = new Dictionary<string, IWMPMedia>();

  List<string> al = new List<string>(); //當前歌詞時間表  

  IWMPMedia media;

  /*
 *下面這一大段API調用,主要是用來設置歌詞窗口的滾動條的
 *但其實後面,我並沒有怎麼用到,隻是在將滾動條滾動到底部時,用瞭一下
 */
  private const int WM_VSCROLL = 0x115;
  private const int SB_HORZ = 0;
  private const int SB_VERT = 1;
  private const int SB_CTL = 2;
  private const int SB_BOTH = 3;
  private const int SB_LINEUP = 0;
  private const int SB_LINELEFT = 0;
  private const int SB_LINEDOWN = 1;
  private const int SB_LINERIGHT = 1;
  private const int SB_PAGEUP = 2;
  private const int SB_PAGELEFT = 2;
  private const int SB_PAGEDOWN = 3;
  private const int SB_PAGERIGHT = 3;
  private const int SB_THUMBPOSITION = 4;
  private const int SB_THUMBTRACK = 5;
  private const int SB_TOP = 6;
  private const int SB_LEFT = 6;
  private const int SB_BOTTOM = 7;
  private const int SB_RIGHT = 7;
  private const int SB_ENDSCROLL = 8;
  private const int WM_PAINT = 0x000F;


  [System.Runtime.InteropServices.DllImport("user32.dll")]
  public static extern bool ScrollWindow(IntPtr hWnd, int XAmount, int YAmount, ref Rectangle lpRect, ref Rectangle lpClipRect);

  [System.Runtime.InteropServices.DllImport("user32.dll")]
  public static extern int SetScrollPos(IntPtr hwnd, int nBar, int nPos, bool bRedraw);

  [System.Runtime.InteropServices.DllImport("user32.dll")]
  public static extern int SetScrollPos(int nBar, int nPos, bool bRedraw);

  [System.Runtime.InteropServices.DllImport("user32.dll")]
  public static extern int GetScrollPos(IntPtr hwnd, int nBar);


  [System.Runtime.InteropServices.DllImport("user32.dll")]
  public static extern bool UpdateWindow(IntPtr hWnd);

  [System.Runtime.InteropServices.DllImport("user32.dll")]
  public static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);

  public void setWord()
  {
   
  }


  public MusicPlayer()
  {
   this.StartPosition = FormStartPosition.CenterScreen;//窗口居中顯示
   InitializeComponent();
  }


  private void MusicPlayer_Load(object sender, EventArgs e)
  {
   InitLoad();
  }


  /// <summary>
  /// 初始化 加載播放列表 如歌詞 背景圖 定時器等等
  /// </summary>
  private void InitLoad()
  {
   try
   {
    bool flag = false;
    string folder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bgImages");
    DirectoryInfo root = new DirectoryInfo(folder);
    FileInfo[] files = root.GetFiles();
    string fileName;
    for (int i = 0; i < files.Length; i++)
    {
     fileName = files[i].Name.ToLower();
     if (fileName.EndsWith(".png") || fileName.EndsWith(".jpeg") || fileName.EndsWith(".jpg"))
     {
      if (!flag)
      {
       imageList = new List<string>();
       this.pictureBox1.Image = Image.FromFile(files[i].FullName);
      }
      imageList.Add(files[i].FullName);
      flag = true;
     }
    }

    playerType.Text = playerType.Items[0].ToString();//默認第一個
    closePoint = this.skinButtonClose.Location;
    dfSize = this.Size;
    richTextBox1.BackColor = this.TransparencyKey;
    skinComboBoxFontName.Text = skinComboBoxFontName.Items[0].ToString();//默認第一個
    skinComboBoxFontSize.Text = skinComboBoxFontSize.Items[0].ToString();//默認第一個
    ComboBoxSkinSelect.Text = ComboBoxSkinSelect.Items[0].ToString();//默認第一個
                     //this.BackPalace = Image.FromFile(ComboBoxSkinSelect.Items[0].ToString());//默認第一個

    lvDetail.AllowDrop = true;
    lvDetail.View = View.Details;
    lvDetail.DragEnter += Files_DragEnter;//對象拖拽事件
    lvDetail.DragDrop += Files_DragDrop;//拖拽操作完成事件
             //wmp.OpenStateChange += WMP_OpenStateChange;
    wmp.PlayStateChange += WMP_PlayStateChange;
    timerImgs.Start();
   }
   catch (Exception ex)
   {
    Console.WriteLine("錯誤:" + ex.Message);
   }
  }

  /// <summary>
  /// 提供給透明歌詞窗口的定時器調用的
  /// </summary>
  /// <param name="s"></param>
  public void showTmform(bool s)
  {
   if (s)
   {
    this.btmForm.Show();
    if (this.first_in)
    {
     this.lrcForm.TopMost = true;
     Point point = this.Location;
     point.Y = point.Y + this.Height;
     this.lrcForm.Location = point;
     this.btmForm.Location = point;
     this.lrcForm.Width = this.Width;
     this.btmForm.Width = this.Width;
     this.first_in = false;
    }
   }
   else
   {
    this.first_in = true;
    this.btmForm.Hide();
   }
  }

  /// <summary>
  /// 播放時會進入這個事件
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  private void WMP_PlayStateChange(object sender, _WMPOCXEvents_PlayStateChangeEvent e)
  {
   loadLrc();
  }


  /// <summary>
  /// 拖拽操作完成事件
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  private void Files_DragDrop(object sender, DragEventArgs e)
  {
   try
   {
    string fileName, fileExtension, fileSize, temp;
    FileInfo fi = null;
    ListViewItem lvi = null;
    Array array = (Array)e.Data.GetData(DataFormats.FileDrop);
    Regex regex = new Regex("(\\.mp3|\\.wav|\\.wma)");
    string filePath;
    for (int i = 0; i < array.Length; i++)
    {
     filePath = array.GetValue(i).ToString();
     //屬於音樂文件 且列表中不存在
     if (regex.IsMatch(filePath) &&
      !dic.ContainsKey(filePath))
     {
      wmp.Ctlcontrols.stop();
      InsertPlayList(out fileName, out fileExtension, out fileSize, out temp, out fi, out lvi, filePath);
     }
    }

   }
   catch (Exception ex)
   {
    MessageBox.Show(ex.Message, "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
   }
  }


  /// <summary>
  /// 插入播放列表 和字典集
  /// </summary>
  /// <param name="fileName"></param>
  /// <param name="fileExtension"></param>
  /// <param name="fileSize"></param>
  /// <param name="temp"></param>
  /// <param name="fi"></param>
  /// <param name="lvi"></param>
  /// <param name="filePath"></param>
  private void InsertPlayList(out string fileName, out string fileExtension, out string fileSize, out string temp, out FileInfo fileInfo, out ListViewItem listViewItem, string filePath)
  {
   fileInfo = new FileInfo(filePath);
   temp = filePath.Remove(filePath.LastIndexOf('.'));
   fileName = Path.GetFileNameWithoutExtension(filePath);
   fileExtension = Path.GetExtension(filePath);
   fileSize = (fileInfo.Length / 1024).ToString() + "KB";

   listViewItem = new ListViewItem();
   listViewItem.Text = index++.ToString();
   listViewItem.SubItems.AddRange(new string[] { fileName, fileExtension, fileSize, filePath });

   lvDetail.Items.Add(listViewItem);
   //添加到播放列表
   media = wmp.newMedia(filePath);
   //listIndex++, 
   //wmp.currentPlaylist.insertItem(media);
   wmp.currentPlaylist.appendItem(media);
   playListDict.Add(filePath, media);
   //杜絕重復項
   dic.Add(filePath, fileName);
  }

  /// <summary>
  /// 文件拖拽進入
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  private void Files_DragEnter(object sender, DragEventArgs e)
  {
   if (e.Data.GetDataPresent(DataFormats.FileDrop))
   {
    e.Effect = DragDropEffects.Link;
   }
   else
   {
    e.Effect = DragDropEffects.None;
   }
  }

  /// <summary>
  /// 導入文件
  /// </summary>
  private void tsmiLoading_Click(object sender, EventArgs e)
  {
   string fileName, fileExtension, fileSize, temp;
   FileInfo fi = null;
   ListViewItem lvi = null;
   openFileDialog1.Multiselect = true;
   openFileDialog1.Filter = "mp3文件(*.mp3)|*.mp3|wav文件(*.wav)|*.wav|wma文件(*.wma)|*.wma";
   if (openFileDialog1.ShowDialog() == DialogResult.OK)
   {
    //順序播放
    wmp.settings.setMode("shuffle", false);
    foreach (string filePath in openFileDialog1.FileNames)
    {
     if (!dic.ContainsKey(filePath))
     {
      InsertPlayList(out fileName, out fileExtension, out fileSize, out temp, out fi, out lvi, filePath);
     }
    }
   }
  }

  /// <summary>
  /// 清除列表
  /// </summary>
  private void ListViewClear_Click(object sender, EventArgs e)
  {
   lvDetail.Items.Clear();
  }

  /// <summary>
  /// 播放
  /// </summary>
  private void tsmiPlayer_Click(object sender, EventArgs e)
  {
   wmp.Ctlcontrols.play();
  }

  /// <summary>
  /// 暫停
  /// </summary>
  private void tsmiWaiting_Click(object sender, EventArgs e)
  {
   wmp.Ctlcontrols.pause();
  }

  /// <summary>
  /// 停止
  /// </summary>
  private void tsmiStop_Click(object sender, EventArgs e)
  {
   wmp.Ctlcontrols.stop();
  }

  /// <summary>
  /// 退出
  /// </summary>
  private void tsmiExit_Click(object sender, EventArgs e)
  {
   Application.Exit();
  }

  /// <summary>
  /// 雙擊某項播放
  /// </summary>
  private void lvDetail_DoubleClick(object sender, EventArgs e)
  {
   if (lvDetail.SelectedItems.Count > 0)
   {
    wmp.currentMedia = wmp.currentPlaylist.Item[int.Parse(lvDetail.SelectedItems[0].Text) - 1];
   }
  }

  /// <summary>
  /// 循環播放
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  private void tsmiXunHuan_Click(object sender, EventArgs e)
  {
   //點擊項
   ToolStripMenuItem currentItem = (ToolStripMenuItem)sender;

   ToolStripItemCollection items = this.tsmiSet.DropDownItems;

   ToolStripMenuItem item;
   for (int i = 0; i < items.Count; i++)
   {

    item = (ToolStripMenuItem)items[i];
    item.Checked = false;
    if (item.Name == currentItem.Name)
    {
     item.Checked = true;
    }
   }

   wmp.settings.setMode("loop", true);
  }

  //順序
  private void tsmiDanQu_Click(object sender, EventArgs e)
  {
   IWMPMedia currentMedia = wmp.currentMedia;
   //點擊項
   ToolStripMenuItem currentItem = (ToolStripMenuItem)sender;

   ToolStripItemCollection items = this.tsmiSet.DropDownItems;

   ToolStripMenuItem item;
   for (int i = 0; i < items.Count; i++)
   {

    item = (ToolStripMenuItem)items[i];
    item.Checked = false;
    if (item.Name == currentItem.Name)
    {
     item.Checked = true;
    }
   }
   //順序播放
   wmp.settings.setMode("shuffle", false);
  }


  /// <summary>
  /// 圖片寬高設置 
  /// </summary>
  /// <param name="imgToResize"></param>
  /// <param name="size"></param>
  /// <returns></returns>
  public static Image ResizeImage(Image imgToResize, Size size)
  {
   //獲取圖片寬度
   int sourceWidth = imgToResize.Width;
   //獲取圖片高度
   int sourceHeight = imgToResize.Height;

   float nPercent = 0;
   float nPercentW = 0;
   float nPercentH = 0;
   //計算寬度的縮放比例
   nPercentW = ((float)size.Width / (float)sourceWidth);
   //計算高度的縮放比例
   nPercentH = ((float)size.Height / (float)sourceHeight);

   if (nPercentH < nPercentW)
    nPercent = nPercentH;
   else
    nPercent = nPercentW;
   //期望的寬度
   int destWidth = (int)(sourceWidth * nPercent);
   //期望的高度
   int destHeight = (int)(sourceHeight * nPercent);

   Bitmap b = new Bitmap(destWidth, destHeight);
   Graphics g = Graphics.FromImage((System.Drawing.Image)b);
   g.InterpolationMode = InterpolationMode.HighQualityBicubic;
   //繪制圖像
   g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
   g.Dispose();
   return (System.Drawing.Image)b;
  }


  private void playerType_SelectedIndexChanged(object sender, EventArgs e)
  {
   if (playerType.Text == "順序播放")
   {
    //順序播放
    wmp.settings.setMode("shuffle", false);
   }
   else if (playerType.Text == "循環播放")
   {
    wmp.settings.setMode("loop", true);
   }
   else if (playerType.Text == "隨機播放")
   {
    //順序播放
    wmp.settings.setMode("shuffle", true);
   }

  }


  /// <summary>
  /// 定時器執行的方法,每隔1秒執行一次 歌詞逐行顯示
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  private void ShowLineLrc(object sender, EventArgs e)
  {
   this.label1.Text = this.wmp.Ctlcontrols.currentPositionString;

   if (this.lrcForm == null)
   {
    this.lrcForm = new TMForm();

    Point pos = this.Location;
    pos.Y = lrcForm.Location.Y + lrcForm.Height;
    this.Location = pos;

    this.btmForm = new BTMForm();
    MusicPlayer mainForm = (MusicPlayer)this.Owner;
    btmForm.Location = pos;
    
    this.lrcForm.Owner = this;
    this.btmForm.Owner = this;
    this.btmForm.Hide();
    this.lrcForm.Show();
   }



   if (this.wmp.currentMedia == null)
   {
    this.richTextBox1.Text = "";
    return;
   }


   string durationString = this.wmp.currentMedia.durationString;
   int trackBarValue = Convert.ToInt32(this.wmp.Ctlcontrols.currentPosition);
   //this.trackBar1.Maximum = Convert.ToInt32(this.wmp.currentMedia.duration);
   //this.trackBar1.Value = Convert.ToInt32(this.wmp.Ctlcontrols.currentPosition);

   if (this.richTextBox1.Text != "歌詞文件不存在" && this.richTextBox1.Text != "歌詞文件內容為空")
   {
    int pos = al.IndexOf(trackBarValue.ToString());
    bool isAr = this.richTextBox1.Text.Contains("歌手:");
    bool isTi = this.richTextBox1.Text.Contains("歌名:");


    if (pos >= 0)
    {
     int n = isAr ? 1 : 0;
     int m = isTi ? 1 : 0;

     int height = 28 * (this.al.Count + m + n);
     int max = height - this.richTextBox1.Height;


     this.richTextBox1.SelectAll();
     this.richTextBox1.SelectionColor = Color.Black;
     this.richTextBox1.SelectionLength = 0;/**/

     int l = this.richTextBox1.Lines[pos + m + n].Length;
     this.richTextBox1.Select(this.richTextBox1.GetFirstCharIndexFromLine(pos + m + n), l);
     this.richTextBox1.SelectionColor = Color.OrangeRed;
     this.richTextBox1.SelectionLength = 0;
     //this.Text = GetScrollPos(this.richTextBox1.Handle, SB_VERT).ToString() + "-" + al.Count + "-" + this.richTextBox1.Height;

     if ((pos + m + n) * 28 <= max)
     {
      int start = this.richTextBox1.GetFirstCharIndexFromLine(pos + m + n);
      this.richTextBox1.SelectionStart = start;
      this.richTextBox1.ScrollToCaret();

     }
     else
     {
      //this.richTextBox1.Focus();
      SendMessage(this.richTextBox1.Handle, WM_VSCROLL, SB_BOTTOM, 0);
      UpdateWindow(this.richTextBox1.Handle);
      //this.richTextBox1.SelectionStart = this.richTextBox1.Text.Length;
      //this.richTextBox1.ScrollToCaret();
     }

     if (this.lrcForm != null)
     {
      string l1 = this.richTextBox1.Lines[pos + m + n];
      string l2;
      if ((pos + m + n) < this.richTextBox1.Lines.Length - 1)
      {
       l2 = this.richTextBox1.Lines[pos + m + n + 1];
      }
      else
      {
       l2 = "。。。。。";
      }

      this.lrcForm.setLrc(l1, l2, pos);
      //this.lrcForm.setLrc(ArrayList al,);

     }

    }
   }
   //this.Text = this.trackBar1.Value.ToString();
   /*if (trackBarValue >= (this.trackBar1.Maximum - 2))
   {
    this.PlayModeChange();
   }*/
  }

  /// <summary>
  /// 載入歌詞
  /// </summary>
  /// <param name="lrc_filename">歌詞路徑名</param>
  public void loadLrc()
  {

   if (this.wmp.playState == WMPPlayState.wmppsPlaying)
   {

    IWMPMedia currentMedia = wmp.currentMedia;
    string fullPath = currentMedia.sourceURL;
    string geciPath = Path.Combine(Path.GetDirectoryName(fullPath), Path.GetFileNameWithoutExtension(fullPath) + ".lrc");

    //播放哪個資源 列表需選中
    ListView.ListViewItemCollection listViewItems = lvDetail.Items;
    lvDetail.FullRowSelect = true;
    for (int i = 0; i < listViewItems.Count; i++)
    {
     listViewItems[i].Checked = false;
     listViewItems[i].Selected = false;
     listViewItems[i].BackColor = Color.White;
     if (listViewItems[i].SubItems[4].Text == fullPath)
     {
      listViewItems[i].Checked = true;
      listViewItems[i].Selected = true;
      listViewItems[i].BackColor = Color.LightBlue;
     }
    }

    if (!File.Exists(geciPath))
    {
     this.richTextBox1.Text = "歌詞文件內容為空";
     return;
    }


    using (StreamReader sr = new StreamReader(new FileStream(geciPath, FileMode.Open), Encoding.Default))
    {
     string tempLrc = "";
     while (!sr.EndOfStream)
     {
      tempLrc = sr.ReadToEnd();
     }

     if (tempLrc.Trim() == "")
     {
      this.richTextBox1.Text = "歌詞文件內容為空";
      return;
     }

     tempLrc = tempLrc.Trim();
     Regex rg = new Regex("\r*\n*\\[ver:(.*)\\]\r*\n*");
     tempLrc = rg.Replace(tempLrc, "");
     rg = new Regex("\r*\n*\\[al:(.*)\\]\r*\n*");
     tempLrc = rg.Replace(tempLrc, "");
     rg = new Regex("\r*\n*\\[by:(.*)\\]\r*\n*");
     tempLrc = rg.Replace(tempLrc, "");
     rg = new Regex("\r*\n*\\[offset:(.*)\\]\r*\n*");
     tempLrc = rg.Replace(tempLrc, "");
     rg = new Regex("\r*\n*\\[ar:(.*)\\]\r*\n*");
     Match mtch;
     mtch = rg.Match(tempLrc);
     tempLrc = rg.Replace(tempLrc, "\n歌手:" + mtch.Groups[1].Value + "\n");
     rg = new Regex("\r*\n*\\[ti:(.+?)\\]\r*\n*"); //這裡註意貪婪匹配問題.+?
     mtch = rg.Match(tempLrc);
     tempLrc = rg.Replace(tempLrc, "\n歌名:" + mtch.Groups[1].Value + "\n");
     rg = new Regex("\r*\n*\\[[0-9][0-9]:[0-9][0-9].[0-9][0-9]\\]");
     MatchCollection mc = rg.Matches(tempLrc);
     al.Clear();

     foreach (Match m in mc)
     {
      string temp = m.Groups[0].Value;
      //this.Text += temp + "+";      
      string mi = temp.Substring(temp.IndexOf('[') + 1, 2);
      string se = temp.Substring(temp.IndexOf(':') + 1, 2);
      string ms = temp.Substring(temp.IndexOf('.') + 1, 2); //這是毫秒,其實我隻精確到秒,毫秒後面並沒有用
                    //this.Text += mi + ":" + se + "+";
      string time = Convert.ToInt32(mi) * 60 + Convert.ToInt32(se) + ""; //這裡並沒有添加毫秒
      al.Add(time);
     }

     tempLrc = rg.Replace(tempLrc, "\n");
     char[] remove = { '\r', '\n', ' ' };
     this.richTextBox1.Text = tempLrc.TrimStart(remove);
     this.timer1.Interval = 1000;
     this.timer1.Tick += ShowLineLrc;
     this.timer1.Start();
    }
   }

  }

  private void wmp_Enter(object sender, EventArgs e)
  {
   loadLrc();//點擊播放
  }



  /// <summary>
  /// 刪除選中的文件 並停止播放
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  private void skinButton2_Click(object sender, EventArgs e)
  {
   try
   {
    ListView.SelectedIndexCollection indexes = this.lvDetail.SelectedIndices;
    if (indexes.Count > 0)
    {
     int index = indexes[0];
     string path = this.lvDetail.Items[index].SubItems[4].Text;
     IWMPPlaylist iWMPPlaylist = wmp.currentPlaylist;
     //先移除播放列表 再移除listview列表
     wmp.currentPlaylist.removeItem(playListDict[path]);
     playListDict.Remove(path);
     this.lvDetail.Items[index].Remove();
     dic.Remove(path);
     wmp.Ctlcontrols.stop();
    }
   }
   catch (Exception ex)
   {
    MessageBox.Show("操作失敗!\n" + ex.Message, "提示", MessageBoxButtons.OK,
     MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);

   }
  }

  /// <summary>
  /// 列表鼠標雙擊事件
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  private void lvDetail_MouseDoubleClick(object sender, MouseEventArgs e)
  {
   try
   {
    ListView.SelectedIndexCollection indexes = this.lvDetail.SelectedIndices;
    if (indexes.Count > 0)
    {
     int index = indexes[0];
     string path = this.lvDetail.Items[index].SubItems[4].Text;
     wmp.Ctlcontrols.playItem(playListDict[path]);
     //wmp.URL = path;//這種方式會失去播放列表【不是肉眼可見的listview列表】
     wmp.Ctlcontrols.stop();
     wmp.Ctlcontrols.play();
    }
   }
   catch (Exception ex)
   {
    MessageBox.Show("操作失敗!\n" + ex.Message, "提示", MessageBoxButtons.OK,
     MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);

   }
  }

  /// <summary>
  /// 字體改變
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  private void skinComboBoxFontName_SelectedIndexChanged(object sender, EventArgs e)
  {
   if (this.lrcForm != null)
   {
    this.lrcForm.ChangeLabelFont(this.skinComboBoxFontName.SelectedItem.ToString(), this.skinComboBoxFontSize.SelectedItem.ToString());

   }
  }

  /// <summary>
  /// 字體大小改變
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  private void skinComboBox1_SelectedIndexChanged(object sender, EventArgs e)
  {
   if (this.lrcForm != null)
   {
    this.lrcForm.ChangeLabelFont(this.skinComboBoxFontName.SelectedItem.ToString(), this.skinComboBoxFontSize.SelectedItem.ToString());

   }
  }

  /// <summary>
  /// 歌詞是否顯示
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  private void skinButton5_Click(object sender, EventArgs e)
  {
   if (this.lrcForm == null)
   {
    return;
   }
   this.showLrc = !this.showLrc;
   this.skinButton5.Text = this.showLrc ? "關閉" : "顯示";
   if (this.showLrc)
   {
    this.btmForm.Show();
    this.lrcForm.Show();
   }
   else
   {
    this.btmForm.Hide();
    this.lrcForm.Hide();
   }
    
  }

  /// <summary>
  /// 背景圖片切換
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  private void timerImgs_Tick(object sender, EventArgs e)
  {
   if (imageInd <= imageList.Count - 2)
   {
    imageInd++;
    this.pictureBox1.Image.Dispose();
    this.pictureBox1.Image = Image.FromFile(imageList[imageInd]);
   }
   else
   {
    imageInd=0;
   }
  }


  /// <summary>
  /// 粘貼音樂到播放列表
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  private void lvDetail_KeyDown(object sender, KeyEventArgs e)
  {
   System.Collections.Specialized.StringCollection stringCollection = Clipboard.GetFileDropList();
   if (stringCollection != null)
   {
    string fileName, fileExtension, fileSize, temp;
    FileInfo fi = null;
    ListViewItem lvi = null;
    foreach (var item in stringCollection)
    {
     if ((item.ToLower().EndsWith(".mp3") ||
      item.ToLower().EndsWith(".wav") ||
      item.ToLower().EndsWith(".wma")) && !dic.ContainsKey(item) )
     {
      InsertPlayList(out fileName, out fileExtension, out fileSize, out temp, out fi, out lvi, item);
     }
    }
   }
  }

  
  //選擇皮膚
  private void ComboBoxSkinSelect_SelectedIndexChanged(object sender, EventArgs e)
  {
   string bgColor = ComboBoxSkinSelect.SelectedItem.ToString();
   if (!string.IsNullOrEmpty(ComboBoxSkinSelect.SelectedItem.ToString()))
   {
    switch (bgColor)
    {
     case "漸變黑":
      this.BorderPalace = kenMusicPlayer.Properties.Resources.bg_black;
      break;
     case "天藍色":
      this.BorderPalace = kenMusicPlayer.Properties.Resources.bg_light_blue;
      break;
     case "墨綠色":
      this.BorderPalace = kenMusicPlayer.Properties.Resources.bg_green;
      break;
     case "藍色":
      this.BorderPalace = kenMusicPlayer.Properties.Resources.bg_s_blue;
      break;
     case "淺灰色":
      this.BorderPalace = kenMusicPlayer.Properties.Resources.bg_grey;
      break; 
     case "亮色":
      this.BorderPalace = kenMusicPlayer.Properties.Resources.bg_light;
      break;
     default:
      this.BorderPalace = kenMusicPlayer.Properties.Resources.bg_black;
      break;
    }
    /*this.UpdateStyles();
    this.Update();*/
    //this.BackPalace = Image.FromFile(ComboBoxSkinSelect.SelectedItem.ToString());
   }
  }

  /// <summary>
  /// 退出
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  private void skinButtonClose_Click(object sender, EventArgs e)
  {
   //this.Close();
   Application.Exit();
  }

  private void MusicPlayer_Resize(object sender, EventArgs e)
  {
   try
   {
    Size size = this.Size;
    int x = (size.Width - dfSize.Width) + closePoint.X;
    int y = closePoint.Y;//(size.Height - dfSize.Height) + closePoint.Y;
    Point point = new Point(x, y);
    this.skinButtonClose.Location = point;
   }
   catch (Exception ex)
   {
    Console.WriteLine("異常:" + ex.Message);
   }

  }


  /// <summary>
  /// 改方法也是提供給透明歌詞窗口用的,用來修改半透明窗體的位置,是透明歌詞窗口和半透明窗體始終重合
  /// </summary>
  /// <param name="pos"></param>
  public void moveTmform(Point pos)
  {
   this.btmForm.Location = pos;
  }
 }
}

源碼地址:https://gitee.com/ten-ken/mus…

以上就是c#基於winform制作音樂播放器的詳細內容,更多關於c# 音樂播放器的資料請關註WalkonNet其它相關文章!

推薦閱讀: