最终的效果如上图.它主要是由几个图片绘制而成,左右两个圆角、经过的、未经过的还有一个%分数。原理非常简单,我们看看代码吧。
Code
1using System;
2
3using System.Collections.Generic;
4using System.ComponentModel;
5using System.Data;
6using System.Drawing;
7using System.Text;
8using System.Windows.Forms;
9using System.Drawing.Imaging;
10using System.Reflection;
11
12namespace Mediar.Controls
13{
14 public partial class ProgressBar : Control
15 {
16
17
18 private byte maximum = 100;
19 public byte Maximum
20 {
21 set { maximum = value; }
22 get { return maximum; }
23
24 }
25
26 private byte minimum = 0;
27 public byte Minimum
28 {
29 set { minimum = value; }
30 get { return minimum; }
31
32 }
33
34
35
36 public int Value { get; set; }
37
38
39 private Color passColor = Color.Blue;
40 public Color PassColor
41 {
42 set { passColor = value; }
43 get { return passColor; }
44
45 }
46
47 private Color unPassColor = Color.Gray;
48 public Color UnPassColor
49 {
50 set { unPassColor = value; }
51 get { return unPassColor; }
52
53 }
54
55 public ProgressBar()
56 {
57 this.Height = 33;
58 InitializeComponent();
59
60 }
61
62 protected override void OnPaint(PaintEventArgs pe)
63 {
64
65 DrawEllipse();
66
67
68 base.OnPaint(pe);
69 }
70 private void DrawEllipse()
71 {
72 //System.Drawing.Pen myPen;
73 //myPen = new System.Drawing.Pen(System.Drawing.Color.Red);
74
75 Brush bUnPass = new SolidBrush(unPassColor);
76
77 Brush bPass = new SolidBrush(passColor);
78
79 Assembly assembly = this.GetType().Assembly;
80 System.IO.Stream passStart = assembly.GetManifestResourceStream(@"Mediar.Controls.Images.passStart.png");
81 System.IO.Stream pass = assembly.GetManifestResourceStream(@"Mediar.Controls.Images.pass.png");
82 System.IO.Stream unpass = assembly.GetManifestResourceStream(@"Mediar.Controls.Images.unpass.png");
83 System.IO.Stream textBackgroud = assembly.GetManifestResourceStream(@"Mediar.Controls.Images.textBackgroud.png");
84 System.IO.Stream end = assembly.GetManifestResourceStream(@"Mediar.Controls.Images.end.png");
85
86
87 Image passStartpng = new Bitmap(passStart);
88
89 System.Drawing.Graphics formGraphics = this.CreateGraphics();
90
91 int iPass = (int)((Value * 1.0 / (Maximum - Minimum)) * this.Width);
92 int y = 10;
93
94 formGraphics.DrawImage(passStartpng, 0, y);
95
96
97 formGraphics.DrawImage(new Bitmap(pass), new Rectangle(8, y, iPass, 12), new Rectangle(0, 0, 1, 12), GraphicsUnit.Pixel);
98
99 formGraphics.DrawImage(new Bitmap(unpass), new Rectangle(8 + iPass, y, this.Width - iPass - 14, 12), new Rectangle(0, 0, 1, 12), GraphicsUnit.Pixel);
100
101
102 formGraphics.DrawImage(new Bitmap(end), this.Width - 8, y);
103
104 if (this.Width - iPass < 25)
105 iPass = this.Width - 25;
106 else if (iPass < 25)
107 iPass = 25;
108
109
110
111
112 formGraphics.DrawImage(new Bitmap(textBackgroud), iPass - 25, 0);
113
114 SizeF sf = formGraphics.MeasureString(string.Format("{0}%", Value), this.Font);
115 formGraphics.DrawString(string.Format("{0}%", Value), this.Font, new SolidBrush(this.ForeColor), iPass - sf.Width / 2, (33 - sf.Height) / 2);
116
117
118
119
120 bPass.Dispose();
121 formGraphics.Dispose();
122 }
123
124
125
126
127 internal Color GetTransparentColor(Bitmap image)
128 {
129 return image.GetPixel(0, 0);
130 }
131 }
132}
133
1using System;
2
3using System.Collections.Generic;
4using System.ComponentModel;
5using System.Data;
6using System.Drawing;
7using System.Text;
8using System.Windows.Forms;
9using System.Drawing.Imaging;
10using System.Reflection;
11
12namespace Mediar.Controls
13{
14 public partial class ProgressBar : Control
15 {
16
17
18 private byte maximum = 100;
19 public byte Maximum
20 {
21 set { maximum = value; }
22 get { return maximum; }
23
24 }
25
26 private byte minimum = 0;
27 public byte Minimum
28 {
29 set { minimum = value; }
30 get { return minimum; }
31
32 }
33
34
35
36 public int Value { get; set; }
37
38
39 private Color passColor = Color.Blue;
40 public Color PassColor
41 {
42 set { passColor = value; }
43 get { return passColor; }
44
45 }
46
47 private Color unPassColor = Color.Gray;
48 public Color UnPassColor
49 {
50 set { unPassColor = value; }
51 get { return unPassColor; }
52
53 }
54
55 public ProgressBar()
56 {
57 this.Height = 33;
58 InitializeComponent();
59
60 }
61
62 protected override void OnPaint(PaintEventArgs pe)
63 {
64
65 DrawEllipse();
66
67
68 base.OnPaint(pe);
69 }
70 private void DrawEllipse()
71 {
72 //System.Drawing.Pen myPen;
73 //myPen = new System.Drawing.Pen(System.Drawing.Color.Red);
74
75 Brush bUnPass = new SolidBrush(unPassColor);
76
77 Brush bPass = new SolidBrush(passColor);
78
79 Assembly assembly = this.GetType().Assembly;
80 System.IO.Stream passStart = assembly.GetManifestResourceStream(@"Mediar.Controls.Images.passStart.png");
81 System.IO.Stream pass = assembly.GetManifestResourceStream(@"Mediar.Controls.Images.pass.png");
82 System.IO.Stream unpass = assembly.GetManifestResourceStream(@"Mediar.Controls.Images.unpass.png");
83 System.IO.Stream textBackgroud = assembly.GetManifestResourceStream(@"Mediar.Controls.Images.textBackgroud.png");
84 System.IO.Stream end = assembly.GetManifestResourceStream(@"Mediar.Controls.Images.end.png");
85
86
87 Image passStartpng = new Bitmap(passStart);
88
89 System.Drawing.Graphics formGraphics = this.CreateGraphics();
90
91 int iPass = (int)((Value * 1.0 / (Maximum - Minimum)) * this.Width);
92 int y = 10;
93
94 formGraphics.DrawImage(passStartpng, 0, y);
95
96
97 formGraphics.DrawImage(new Bitmap(pass), new Rectangle(8, y, iPass, 12), new Rectangle(0, 0, 1, 12), GraphicsUnit.Pixel);
98
99 formGraphics.DrawImage(new Bitmap(unpass), new Rectangle(8 + iPass, y, this.Width - iPass - 14, 12), new Rectangle(0, 0, 1, 12), GraphicsUnit.Pixel);
100
101
102 formGraphics.DrawImage(new Bitmap(end), this.Width - 8, y);
103
104 if (this.Width - iPass < 25)
105 iPass = this.Width - 25;
106 else if (iPass < 25)
107 iPass = 25;
108
109
110
111
112 formGraphics.DrawImage(new Bitmap(textBackgroud), iPass - 25, 0);
113
114 SizeF sf = formGraphics.MeasureString(string.Format("{0}%", Value), this.Font);
115 formGraphics.DrawString(string.Format("{0}%", Value), this.Font, new SolidBrush(this.ForeColor), iPass - sf.Width / 2, (33 - sf.Height) / 2);
116
117
118
119
120 bPass.Dispose();
121 formGraphics.Dispose();
122 }
123
124
125
126
127 internal Color GetTransparentColor(Bitmap image)
128 {
129 return image.GetPixel(0, 0);
130 }
131 }
132}
133