黑马程序员技术交流社区
标题:
给大家分享个我自己写的播放器
[打印本页]
作者:
cancle
时间:
2014-3-15 09:35
标题:
给大家分享个我自己写的播放器
本帖最后由 cancle 于 2014-3-15 09:37 编辑
public Form1()
{
InitializeComponent();
}
string appPath = Environment.CurrentDirectory;
private void Form1_Load(object sender, EventArgs e)
{
labtime.Text = DateTime.Now.ToString();
//pictureBox1.Image = Image.FromFile(@"D:\照片\DCIM\13070126.jpg");
panel1.Visible = true;
panel2.Visible = false;
}
private void button1_Click(object sender, EventArgs e)
{
panel1.Visible = false;
}
private void button3_Click(object sender, EventArgs e)
{
panel2.Visible = false;
}
private void 显示播放列表ToolStripMenuItem_Click(object sender, EventArgs e)
{
if(显示播放列表ToolStripMenuItem.Text == "☆显示播放列表")
{
panel1.Visible = true;
}
else
{
panel1.Visible = false;
}
}
private void 清空当前播放列表ToolStripMenuItem_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
}
private void timer1_Tick(object sender, EventArgs e)
{
labtime.Text = DateTime.Now.ToString();
if (panel1.Visible == true)
{
显示播放列表ToolStripMenuItem.Text = "★取消播放列表";
}
else
{
显示播放列表ToolStripMenuItem.Text = "☆显示播放列表";
}
if(panel2.Visible==true)
{
显示文本ToolStripMenuItem.Text= "▲隐藏文本";
}
else
{
显示文本ToolStripMenuItem.Text = "△显示文本";
}
}
private void 显示文本ToolStripMenuItem_Click(object sender, EventArgs e)
{
if(显示文本ToolStripMenuItem.Text == "△显示文本")
{
panel2.Visible = true;
}
else
{
panel2.Visible = false;
}
}
List<string> list = new List<string>();//存放歌曲路径
private void btnMusic_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "请选择你要添加的歌曲";
ofd.InitialDirectory = @"C:\Users\Administrator\Desktop";
ofd.Multiselect = true;
ofd.Filter = "音乐文件|*.mp3";
ofd.ShowDialog();
string[] str = ofd.FileNames;
if(str==null)
{
return;
}
list.AddRange(str);
for (int i = 0; i < str.Length; i++)
{
listBox1.Items.Add(Path.GetFileName(str[i]));
}
}
private void btnText_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "请选择你要打开的文档";
ofd.InitialDirectory = @"C:\Users\Administrator\Desktop";
ofd.Filter = "文本文件|*.txt";
ofd.ShowDialog();
string str = ofd.FileName;
if(str=="")
{
return;
}
using(FileStream fsRead=new FileStream(str,FileMode.OpenOrCreate,FileAccess.Read))
{
byte[] buffer = new byte[1024 * 1024 * 5];
int val = fsRead.Read(buffer, 0, buffer.Length);
textBox1.Text = Encoding.Default.GetString(buffer, 0, val);
}
MessageBox.Show("打开完成");
panel2.Visible = true;
}
private void 清除文本ToolStripMenuItem_Click(object sender, EventArgs e)
{
textBox1.Clear();
textBox1.Focus();
}
private void 更换背景图片ToolStripMenuItem1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "请选择你要设置的背景图片";
ofd.InitialDirectory = @"C:\Users\Administrator\Desktop";
ofd.Filter = "图片文件|*.jpg|所有文件|*.*";
ofd.ShowDialog();
string str = ofd.FileName;
if(str=="")
{
return;
}
pictureBox1.Image = Image.FromFile(str);
}
private void 关于xBox10ToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("名称:MusicBox\r\n版本:1.0\r\n最终解释权归宇星所有。");
}
private void listBox1_DoubleClick(object sender, EventArgs e)
{
Music.URL = list[listBox1.SelectedIndex];
}
private void 保存文本ToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Title = "请选择要保存的路径";
sfd.InitialDirectory = @"C:\Users\Administrator\Desktop";
sfd.Filter = "文本文件|*.txt";
sfd.ShowDialog();
string str = sfd.FileName;
if (str == "")
{
return;
}
using(FileStream fsWrite=new FileStream(str,FileMode.OpenOrCreate,FileAccess.Write))
{
byte[] buffer = Encoding.Default.GetBytes(textBox1.Text);
fsWrite.Write(buffer,0,buffer.Length);
}
MessageBox.Show("保存成功");
}
复制代码
播放器.zip
(1.63 MB, 下载次数: 121)
2014-3-15 09:34 上传
点击文件名下载附件
如图
1.png
(248.78 KB, 下载次数: 81)
下载附件
2014-3-15 09:29 上传
后面会附上部分代码,这是文件http://bbs.itheima.com/forum.php?mod=attachment&aid=MzYzODB8NzUxYjhlMDE5N2FmNTdmMzY5NGNmZTM1NzY4MGUzZDB8MTczMTI5ODQxOA%3D%3D&request=yes&_f=.zip
作者:
鬼鬼
时间:
2014-3-15 15:24
恭喜楼主终于成功啦!
另:
1.其实楼主只需要把“AxInterop.WMPLib.dll”“Interop.WMPLib.dll”“音乐播放器.exe”这三个文件打包,程序就可以运行了。
2.提个建议:楼主为了界面美观设置了窗口大小不可改,却忘了禁用最大化按钮……
3.其实固定窗口尺寸并不是最好的选择,楼主感兴趣的话可以看看WPF方面的东西,它对界面元素的处理方法还是挺不错的。
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2