C#自定义音乐播放器进度条
作者:波谷 时间:2023-07-04 21:54:05
有些时候我们做的程序需要进度条,而vs提供的控件不是我们想要的。先看效果图:
进度条闪烁动画,当然背景可设为Transparent
之前想手绘进度条线条的,结果控件运行时会闪烁,所以直接用了panel控件
源码:
[DefaultEvent("ProgressClick")]
[ToolboxBitmap(typeof(TrackBar))]
public partial class ProcessBar : UserControl
{
public ProcessBar()
{
//InitializeComponent();
//this.SetStyle(ControlStyles.UserPaint, true);
//this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
//this.SetStyle(ControlStyles.DoubleBuffer, true);
}
private int locationX=0;
[Description("单击时X的坐标")]
public int LocationX
{
get { return locationX; }
}
private int current = 0;
[Description("当前进度")]
public int Current
{
get { return current; }
set
{
if (value > 232 || value < 0)
return;
current = value;
panelCurrent.Size = new Size(value, 1);
picture.Location = new Point(value - 4, -3);
Invalidate();
}
}
private bool isPlay = false;
[Description("是否播放")]
public bool IsPlay
{
get { return isPlay; }
set { isPlay = value; tmrCurrent.Enabled = isPlay; Invalidate(); }
}
public delegate void MouseHandle(object sender,EventArgs e);
[Description("点下鼠标")]
public event MouseHandle BarMouseDown;
int picturetype = 0;
private void tmrCurrent_Tick(object sender, EventArgs e)
{
if (picturetype == 0)
{ picture.Image = Properties.Resources.play_slider_thumb; picturetype = 1; }
else
{ picture.Image = Properties.Resources.play_slider_thumb_animate; picturetype = 0; }
GraphicsPath g = subGraphicsPath(picture.Image);
if (g == null) return;
picture.Region = new Region(g);
}
private unsafe static GraphicsPath subGraphicsPath(Image img)
{
if (img == null) return null;
// 建立GraphicsPath, 给我们的位图路径计算使用
GraphicsPath g = new GraphicsPath(FillMode.Alternate);
Bitmap bitmap = new Bitmap(img);
int width = bitmap.Width;
int height = bitmap.Height;
BitmapData bmData = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
byte* p = (byte*)bmData.Scan0;
int offset = bmData.Stride - width * 3;
int p0, p1, p2; // 记录左上角0,0座标的颜色值
p0 = p[0];
p1 = p[1];
p2 = p[2];
int start = -1;
// 行座标 ( Y col )
for (int Y = 0; Y < height; Y++)
{
// 列座标 ( X row )
for (int X = 0; X < width; X++)
{
if (start == -1 && (p[0] != p0 || p[1] != p1 || p[2] != p2)) //如果 之前的点没有不透明 且 不透明
{
start = X; //记录这个点
}
else if (start > -1 && (p[0] == p0 && p[1] == p1 && p[2] == p2)) //如果 之前的点是不透明 且 透明
{
g.AddRectangle(new Rectangle(start, Y, X - start, 1)); //添加之前的矩形到
start = -1;
}
if (X == width - 1 && start > -1) //如果 之前的点是不透明 且 是最后一个点
{
g.AddRectangle(new Rectangle(start, Y, X - start + 1, 1)); //添加之前的矩形到
start = -1;
}
p += 3; //下一个内存地址
}
p += offset;
} bitmap.UnlockBits(bmData);
bitmap.Dispose();
// 返回计算出来的不透明图片路径
return g;
}
private void panelTotal_MouseDown(object sender, MouseEventArgs e)
{
Current = e.Location.X;
locationX = e.Location.X;
if (BarMouseDown != null)
{
BarMouseDown.Invoke(sender, e);
}
}
private void panelCurrent_MouseDown(object sender, MouseEventArgs e)
{
Current = e.Location.X;
locationX = e.Location.X;
if (BarMouseDown != null)
{
BarMouseDown.Invoke(sender, e);
}
}
}
用到的素材:
直接右键另存为图片,之所以用黑色背景是因为图片是白色的看不见,不用多说了。
提示:这里用到了unsafe关键字,需要设置项目的属性-----允许运行不安全的代码,没有设置的同学不要以为程序错了
标签:C#,进度条
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
maven引入本地jar包运行报错java.lang.NoClassDefFoundError解决
2022-10-15 01:42:24
java设计模式-单例模式实现方法详解
2022-02-15 13:56:01
![](https://img.aspxhome.com/file/2023/5/119185_0s.png)
C#简单遍历指定文件夹中所有文件的方法
2022-12-16 18:42:27
聊聊Redis二进制数组Bitmap
2021-07-26 11:51:57
redisson分布式限流RRateLimiter源码解析
2021-05-29 13:10:15
![](https://img.aspxhome.com/file/2023/4/110924_0s.jpg)
C# 利用代理爬虫网页的实现方法
2023-02-26 18:51:43
Android Studio中debug功能详解
2022-05-22 08:16:01
![](https://img.aspxhome.com/file/2023/6/137566_0s.png)
Java编程swing组件JLabel详解以及使用示例
2023-12-15 22:28:50
Android 启动页白屏解决方案
2023-11-06 02:54:02
Android 自定义弹性ListView控件实例代码(三种方法)
2022-04-14 15:00:33
C#快速排序算法实例分析
2023-06-17 00:30:00
关于C#转换二进制所引起的一些思考
2021-06-02 18:31:22
![](https://img.aspxhome.com/file/2023/0/88340_0s.png)
Java命名规范
2022-10-03 00:06:59
如何在springBoot下搭建日志框架
2021-07-01 03:41:44
![](https://img.aspxhome.com/file/2023/5/89495_0s.png)
C# 图片剪切与缩小的实例
2021-12-31 14:32:53
java贪吃蛇游戏编写代码
2023-06-16 02:41:10
![](https://img.aspxhome.com/file/2023/3/74753_0s.jpg)
Java异常处理中的一些特殊情况举例
2021-10-11 20:31:54
Java源码深度分析String与StringBuffer及StringBuilder详解
2022-04-01 09:55:50
![](https://img.aspxhome.com/file/2023/2/132132_0s.png)
Java swing实现支持录音等功能的钢琴程序
2022-08-21 12:30:16
![](https://img.aspxhome.com/file/2023/1/80951_0s.jpg)
Android实现向Launcher添加快捷方式的方法
2022-09-14 09:26:41
![](https://img.aspxhome.com/file/2023/9/126719_0s.png)