实例介绍
【实例简介】
1. 添加标签,然后在标签下 添加音乐
【实例截图】
【核心代码】
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.IO; //Path类用到
//using System.Media; //SoundPlayer命名空间
//using System.Runtime.InteropServices;
namespace MyMusic
{
public partial class MusicMainFrom : Form
{
//List<string> listsongs = new List<string>(); //用来存储音乐文件的全路径
Dictionary<string, string> songPath = new Dictionary<string, string>();
//SoundPlayer sp = new SoundPlayer();
OpenFileDialog ofd = new OpenFileDialog();
List<ListBox> listBoxs = new List<ListBox>();
public MusicMainFrom()
{
InitializeComponent();
lbErr.Text = "";
}
/**
* 添加一个标签页
* */
private void BtnNewTab_Click(object sender, EventArgs e)
{
AddControls();
txName.Text = "";
}
/**
* 添加歌曲到播放列表
* */
private void BtnAdd_Click(object sender, EventArgs e)
{
FileDirectory();
}
/**
* 下一曲
**/
private void BtnNext_Click(object sender, EventArgs e)
{
int index = listBoxs[tcCentent.SelectedIndex].SelectedIndex; //获得当前选中歌曲的索引
index ;
SongIndex(index);
}
/**
* 上一曲
* */
private void BtnPre_Click(object sender, EventArgs e)
{
int index = listBoxs[tcCentent.SelectedIndex].SelectedIndex; //获得当前选中歌曲的索引
index--;
SongIndex(index);
}
/**
* 播放
* */
private void BtnPlay_Click(object sender, EventArgs e)
{
int index = listBoxs[tcCentent.SelectedIndex].SelectedIndex; //获得当前选中歌曲的索引
SongIndex(index);
}
private void BtnStop_Click(object sender, EventArgs e)
{
string songName = Convert.ToString(listBoxs[tcCentent.SelectedIndex].SelectedItem);
//sp.SoundLocation = songPath[songName];
//sp.Stop();
axWindowsMedia.Ctlcontrols.stop();
}
//移除对应的listBoxs的对应歌曲/视频
private void BtnDelListBox_Click(object sender, EventArgs e)
{
int index = listBoxs[tcCentent.SelectedIndex].SelectedIndex;
if (index != -1){
lbErr.Text = "";
listBoxs[tcCentent.SelectedIndex].Items.RemoveAt(listBoxs[tcCentent.SelectedIndex].SelectedIndex);
}
else{
lbErr.Text = "请选择歌曲/视频再删除";
}
}
//选择文件并把路径记录在listBoxs上
private void FileDirectory()
{
ofd.Title = "请选择音乐/视频文件"; //打开对话框的标题
ofd.InitialDirectory = @"F:\music-cut"; //设置打开对话框的初始设置目录
ofd.Multiselect = true; //设置多选
ofd.Filter = @"音乐文件|*.mp3|音乐文件|*.wav|视频文件|*.mp4|所有文件|*.*"; //设置文件格式筛选
ofd.ShowDialog(); //显示打开对话框
string[] pa_th = ofd.FileNames; //获得在文件夹中选择的所有文件的全路径
if (null != pa_th && pa_th.Length != 0)
{
for (int i = 0; i < pa_th.Length; i )
{
if(songPath.ContainsKey(Path.GetFileName(pa_th[i])))
{
return;
}
listBoxs[tcCentent.SelectedIndex].Items.Add(Path.GetFileName(pa_th[i])); //将音乐文件的文件名加载到listBox中
songPath.Add(Path.GetFileName(pa_th[i]), pa_th[i]); //将音乐文件的全路径存储到泛型集合中
}
}
}
//歌曲下标
private void SongIndex(int index)
{
if(listBoxs[tcCentent.SelectedIndex].Items.Count == 0)
{
lbErr.Text = "请添加歌曲/视频";
return;
}
if (index == listBoxs[tcCentent.SelectedIndex].Items.Count)
{
index = 0;
}
lbErr.Text = "";
listBoxs[tcCentent.SelectedIndex].SelectedIndex = index; //将改变后的索引重新赋值给我当前选中项的索引
string songName = Convert.ToString(listBoxs[tcCentent.SelectedIndex].SelectedItem);
//sp.SoundLocation = songPath[songName];
//sp.Play();
if (songPath.ContainsKey(songName))
{
lbErr.Text = "";
axWindowsMedia.URL = songPath[songName];
}
else
{
lbErr.Text = "请选择歌曲/视频";
}
}
//添加控件
private void AddControls()
{
TabPage page = new TabPage
{
Name = txName.Text,
Text = txName.Text
};
page.TabIndex = page.TabIndex 1;
Button button = new Button();
button.SetBounds(520, 10, 126, 25);
button.Text = "添加音乐";
button.Click = new EventHandler(BtnAdd_Click);
page.Controls.Add(button);
Button button2 = new Button();
button2.SetBounds(656, 10, 126, 25);
button2.Text = "删除音乐";
button2.Click = new EventHandler(BtnDelListBox_Click);
page.Controls.Add(button2);
Button button3 = new Button();
button3.SetBounds(520, 55, 100, 25);
button3.Text = "播放";
button3.Click = new EventHandler(BtnPlay_Click);
page.Controls.Add(button3);
Button button4 = new Button();
button4.SetBounds(640, 55, 100, 25);
button4.Text = "停止";
button4.Click = new EventHandler(BtnStop_Click);
page.Controls.Add(button4);
Button button5 = new Button();
button5.SetBounds(520, 90, 100, 25);
button5.Text = "上一曲";
button5.Click = new EventHandler(BtnPre_Click);
page.Controls.Add(button5);
Button button6 = new Button();
button6.SetBounds(640, 90, 100, 25);
button6.Text = "下一曲";
button6.Click = new EventHandler(BtnNext_Click);
page.Controls.Add(button6);
ListBox listBox = new ListBox();
listBox.SetBounds(10, 10, 500, 200);
page.Controls.Add(listBox);
listBoxs.Add(listBox);
this.tcCentent.Controls.Add(page);
}
/**
* 删除标签页
* */
private void BtnDelTab_Click(object sender, EventArgs e)
{
listBoxs.RemoveAt(tcCentent.SelectedIndex);
tcCentent.Controls.RemoveAt(tcCentent.SelectedIndex);
}
}
}
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论