C#简单音乐播放器(三)

本文介绍了如何使用C# Winform进行音乐播放器的UI美化,重点在于控件的重绘,包括ListBox和ListView。通过GDI+绘制,实现了自定义的按钮交互和菜单、播放列表的视觉效果。同时,分享了项目源码的码云地址。
摘要由CSDN通过智能技术生成

简单音乐播放器(三)

【上一篇】简单音乐播放器(二)

一、前言

  • 时隔一年,终于回来填坑了。前段时间把这个以前写的东西拿出来大改了UI,然后用去作为数字媒体开发课的作业交了,课上演示效果还不错。因为到了要找实习的时间了,最近的半年主要精力都花在学Java了,C#用得也少了许多。
  • 这次主要是把项目的UI进行了美化,自己重绘了两个控件,现在的界面不再是小白框了。(先上界面)

    (专辑封面界面)


    (播放列表界面)
  • 对于使用C#开发桌面程序而已,更漂亮的界面与更好看的动画几乎在Winform上很难实现,更应该去考虑使用WPF。虽然Winform框架已经过时,但博主使用起来更顺手,开发起来更快,所以选择继续使用Winform开发,尽量的美化了程序的UI。
  • 本篇博客不再讲程序里的代码逻辑,这一次分享美化界面,控件重绘的相关内容(如果想了解播放器的代码逻辑部分,可以参考本系列的前两篇博客内容)。
  • 另外本项目源码及资源都已上传至码云,有需要可进行下载(代码存在有些许bug,如有疑问,乐意回答)。

二、界面设计

资源

  1. UI设计主要参考了花瓣网上的播放器UI以及网易云音乐播放器(虽然比网易云丑多了…);
  2. 图标都下载自阿里iconfont网站,根据需要适当调整颜色;

交互

按钮

程序中使用的按钮几乎所有都是picturebox控件,通过MouseEnter与MouseLeave事件,更改按钮的显示样式。

这里写图片描述

(按钮交互)

列表

程序中主要包含了两个列表,左侧的菜单栏,右侧的播放列表。
1. 左侧菜单使用ListBox控件重绘,播放列表使用ListView控件重绘。所以接下来会分别介绍这两种控件的重绘方法与重绘过程。
2. 另外进度条与音量条没有进行重绘(感兴趣的读者可以自己尝试着去重绘控件)。

三、重绘控件

菜单

控件介绍

左侧的菜单栏使用控件ListBox,功能是用于选择不同的列表(本地音乐、收藏夹等)。


(菜单选中效果)

本程序中只实现了List(本地音乐)、Favorite(收藏夹)这两个页面的功能,其他的功能读者可以按照个人需要进行设计和添加。

重绘事件
  • DrawItem(绘制子项)
  • MeasureItem(设置子项高度)

ListBox通过以上两个事件,使用GDI+即可按照自己需要的样式进行重绘(如果还不会使用GDI+绘图,推荐学习下,做开发迟早都会使用到)。

重绘步骤
  1. 首先要将ListBox控件的DrawMode修改为OwnerDrawVariable(默认是由系统帮我们自动绘制)。
  2. 编写DrawItem与MeasureItem事件代码。
重绘代码

注释已经很详细了,稍懂GDI+绘图函数应该很容易就能看懂。
绘制时,需要注意两个问题:

  • 重绘闪烁:重绘控件必遇到的问题。解决办法有有很多(比如双缓冲、缓存等),本次使用的是缓存方式,即将要绘制的内容先全部绘制在一个Bitmap上,在所有绘制结束后再将其给重绘的控件。
  • 绘制顺序:后绘制的内容会覆盖先绘制的内容,有点PS里图层相互遮盖的意思。

                
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace MyQQ2010 { public partial class MusicForm : Form { public MusicForm() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { //打开文件夹选项 OpenFileDialog f = new OpenFileDialog(); //设置能够选中的多个选项 f.Multiselect = true; //弹出文件对话框 DialogResult result = f.ShowDialog(); if ( DialogResult.OK==result) { //获取选中的曲的路径 string[] music = f.FileNames; //将曲添加到ListBox中 foreach (string item in music) { this.lstMusic.Items.Add(item); } } } //双击播放曲 private void lstmusic2_DoubleClick(object sender, EventArgs e) { //获取选中的曲 string music = this.lstmusic2.SelectedItem.ToString(); //播放曲 Player.URL = music; } private void 添加到播放列表ToolStripMenuItem_Click_1(object sender, EventArgs e) { if (this.lstMusic.SelectedItems.Count == 0) { MessageBox.Show("请选中你要选中的曲"); return; } else { //获取选中的曲 string music = this.lstMusic.SelectedItem.ToString(); //把选中的曲移动到播放列表中 this.lstmusic2.Items.Add(music); //从lstmusic中移除 this.lstMusic.Items.Remove(music); } } private void 删除音乐ToolStripMenuItem_Click_1(object sender, EventArgs e) { if (this.lstMusic.SelectedItems.Count == 0) { MessageBox.Show("请选中你要删除的曲"); } else { //获取选中的曲 string music = this.lstMusic.SelectedItem.ToString(); //把选中的曲删除掉 this.lstMusic.Items.Remove(music); } } private void 从列表中移除ToolStripMenuItem_Click_1(object sender, EventArgs e) { if (this.lstmusic2.SelectedItems.Count == 0) { MessageBox.Show("请选中你要删除的曲"); } else { //获取选中的曲 string music = this.lstmusic2.SelectedItem.ToString(); //把选中的曲删除 this.lstmusic2.Items.Remove(music); } } private void MusicForm_Load_1(object sender, EventArgs e) { //播放顺序默认为列表播放 this.cboPlayOrder.SelectedIndex = 0; this.trm1.Start(); } private void trm1_Tick(object sender, EventArgs e) { //判断曲播放的顺序 if (Player.playState == WMPLib.WMPPlayState.wmppsStopped) { if (this.cboPlayOrder.Text == "列表播放") { if (this.lstmusic2.SelectedIndex < this.lstmusic2.Items.Count - 1) { this.lstmusic2.SelectedIndex++; this.lstMusic.Items.Add(this.lstmusic2.SelectedItem.ToString()); Player.URL = this.lstmusic2.SelectedItem.ToString(); } else { this.lstmusic2.SelectedIndex = 0; Player.URL = this.lstmusic2.SelectedItem.ToString(); } } else if (this.cboPlayOrder.Text == "随机播放") { Random r = new Random(); int index = r.Next(0, this.lstmusic2.Items.Count - 1); this.lstmusic2.SelectedIndex = index; string item = this.lstmusic2.SelectedItem.ToString(); this.lstMusic.Items.Add(item);//随机播放的曲也要在lbmusic2中显示 Player.URL = item; } else { int index = this.lstmusic2.SelectedIndex; Player.URL = this.lstmusic2.SelectedItem.ToString(); } } } } }
评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值