C#自定義音樂 器進(jìn)度條
有些時(shí)候我們做的程序需要進(jìn)度條,而vs提供的控件不是我們想要的。先看效果圖:
進(jìn)度條閃爍動(dòng)畫,當(dāng)然背景可設(shè)為Transparent
之前想手繪進(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)沒有不透明 且 不透明 { 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ì)算出來的不透明圖片路徑 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片是白色的看不見,不用多說了。
提示:這里用到了unsafe關(guān)鍵字,需要設(shè)置項(xiàng)目的屬性-----允許運(yùn)行不安全的代碼,沒有設(shè)置的同學(xué)不要以為程序錯(cuò)了
上一篇:asp.net(c#)編程實(shí)現(xiàn)將彩色圖片變灰階圖片的方法示例
欄 目:C#教程
下一篇:C#如何自定義線性節(jié)點(diǎn)鏈表集合
本文標(biāo)題:C#自定義音樂 器進(jìn)度條
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/5570.html
您可能感興趣的文章
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動(dòng)新聞效果的方法
- 01-10C#自定義簽名章實(shí)現(xiàn)方法
- 01-10WinForm實(shí)現(xiàn)自定義右下角提示效果的方法
- 01-10C#實(shí)現(xiàn)自定義windows系統(tǒng)日志的方法
- 01-10C#自定義事件監(jiān)聽實(shí)現(xiàn)方法
- 01-10C#編程實(shí)現(xiàn)自定義熱鍵的方法
- 01-10輕松學(xué)習(xí)C#的方法
- 01-10C#自定義DataGridViewColumn顯示TreeView
- 01-10C#實(shí)現(xiàn)的自定義郵件發(fā)送類完整實(shí)例(支持多人多附件)
- 01-10在C#使用字典存儲(chǔ)事件示例及實(shí)現(xiàn)自定義事件訪問器


閱讀排行
- 1C語言 while語句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語言實(shí)現(xiàn)“百馬百擔(dān)”問題方法
- 4C語言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 04-02jquery與jsp,用jquery
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10delphi制作wav文件的方法
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什