有些時候我們做的程序需要進度條,而vs提供的控件不是我們想要的。先看效果圖:
進度條閃爍動畫,當然背景可設為transparent
之前想手繪進度條線條的,結果控件運行時會閃爍,所以直接用了panel控件
源碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
[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關鍵字,需要設置項目的屬性-----允許運行不安全的代碼,沒有設置的同學不要以為程序錯了