C#自定義音樂(lè)播放器進(jìn)度條
有些時(shí)候我們做的程序需要進(jìn)度條,而vs提供的控件不是我們想要的。先看效果圖:

進(jìn)度條閃爍動(dòng)畫(huà),當(dāng)然背景可設(shè)為T(mén)ransparent
之前想手繪進(jìn)度條線條的,結(jié)果控件運(yùn)行時(shí)會(huì)閃爍,所以直接用了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("單擊時(shí)X的坐標(biāo)")]
public int LocationX
{
get { return locationX; }
}
private int current = 0;
[Description("當(dāng)前進(jìn)度")]
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("點(diǎn)下鼠標(biāo)")]
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, 給我們的位圖路徑計(jì)算使用
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座標(biāo)的顏色值
p0 = p[0];
p1 = p[1];
p2 = p[2];
int start = -1;
// 行座標(biāo) ( Y col )
for (int Y = 0; Y < height; Y++)
{
// 列座標(biāo) ( X row )
for (int X = 0; X < width; X++)
{
if (start == -1 && (p[0] != p0 || p[1] != p1 || p[2] != p2)) //如果 之前的點(diǎn)沒(méi)有不透明 且 不透明
{
start = X; //記錄這個(gè)點(diǎn)
}
else if (start > -1 && (p[0] == p0 && p[1] == p1 && p[2] == p2)) //如果 之前的點(diǎn)是不透明 且 透明
{
g.AddRectangle(new Rectangle(start, Y, X - start, 1)); //添加之前的矩形到
start = -1;
}
if (X == width - 1 && start > -1) //如果 之前的點(diǎn)是不透明 且 是最后一個(gè)點(diǎn)
{
g.AddRectangle(new Rectangle(start, Y, X - start + 1, 1)); //添加之前的矩形到
start = -1;
}
p += 3; //下一個(gè)內(nèi)存地址
}
p += offset;
} bitmap.UnlockBits(bmData);
bitmap.Dispose();
// 返回計(jì)算出來(lái)的不透明圖片路徑
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);
}
}
}
用到的素材:


直接右鍵另存為圖片,之所以用黑色背景是因?yàn)閳D片是白色的看不見(jiàn),不用多說(shuō)了。
提示:這里用到了unsafe關(guān)鍵字,需要設(shè)置項(xiàng)目的屬性-----允許運(yùn)行不安全的代碼,沒(méi)有設(shè)置的同學(xué)不要以為程序錯(cuò)了
- c# 實(shí)現(xiàn)圓形的進(jìn)度條(ProgressBar)
- C#實(shí)現(xiàn)炫酷啟動(dòng)圖-動(dòng)態(tài)進(jìn)度條效果
- C# Oracle批量插入數(shù)據(jù)進(jìn)度條的實(shí)現(xiàn)代碼
- C#實(shí)現(xiàn)帶百分比的進(jìn)度條功能示例
- C#實(shí)現(xiàn)帶進(jìn)度條的ListView
- C# Winform下載文件并顯示進(jìn)度條的實(shí)現(xiàn)代碼
- c#根據(jù)文件大小顯示文件復(fù)制進(jìn)度條實(shí)例
- c#進(jìn)度條 progressBar 使用方法的小例子
- C#中常使用進(jìn)度條的代碼
- Winform 實(shí)現(xiàn)進(jìn)度條彈窗和任務(wù)控制
- C#使用winform實(shí)現(xiàn)進(jìn)度條效果
相關(guān)文章
.NET實(shí)現(xiàn)定時(shí)發(fā)送郵件代碼(兩種方式)
經(jīng)常發(fā)郵件的朋友都知道,郵箱有個(gè)特殊功能,可以設(shè)定郵件發(fā)送時(shí)間,定時(shí)發(fā)送,這個(gè)功能是怎么實(shí)現(xiàn)的呢?接下來(lái),小編給大家分享.NET實(shí)現(xiàn)定時(shí)發(fā)送郵件的代碼,有需要的朋友可以參考下2015-08-08
C#結(jié)合Minio實(shí)現(xiàn)文件上傳存儲(chǔ)與更新
MinIO是一個(gè)開(kāi)源的對(duì)象存儲(chǔ)服務(wù)器,專(zhuān)門(mén)設(shè)計(jì)用于在大規(guī)模數(shù)據(jù)存儲(chǔ)環(huán)境中運(yùn)行,這篇文章主要為大家介紹了C#如何結(jié)合Minio實(shí)現(xiàn)文件上傳存儲(chǔ)與更新,需要的可以參考下2024-03-03
c#實(shí)現(xiàn)獲取字符串陣列中元素最長(zhǎng)或最短的長(zhǎng)度
下面小編就為大家分享一篇c#實(shí)現(xiàn)獲取字符串陣列中元素最長(zhǎng)或最短的長(zhǎng)度方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助2017-12-12
C#常用多線程(線程同步,事件觸發(fā),信號(hào)量,互斥鎖,共享內(nèi)存,消息隊(duì)列)
這篇文章主要介紹了C#常用多線程(線程同步,事件觸發(fā),信號(hào)量,互斥鎖,共享內(nèi)存,消息隊(duì)列),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-09-09
C#實(shí)現(xiàn)簡(jiǎn)單的JSON序列化功能代碼實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)簡(jiǎn)單的JSON序列化功能,大家可以參考使用2013-11-11
利用C#實(shí)現(xiàn)HTML模板的循環(huán)輸出
模板循環(huán)輸出 ,是指使用 UI 前端設(shè)計(jì)的 HTML 模板片斷,并結(jié)合數(shù)據(jù)記錄進(jìn)行循環(huán)輸出的過(guò)程,本文將介紹如何中通過(guò) C# 實(shí)現(xiàn)操作 HTML 模板的循環(huán)輸出,文章通過(guò)代碼示例講解的非常詳細(xì),需要的朋友可以參考下2024-06-06

