|
|
1.1 ! root 1: using System; ! 2: using System.Drawing; ! 3: using System.Drawing.Drawing2D; ! 4: using System.Windows.Forms; ! 5: ! 6: namespace CleanFlashCommon { ! 7: public class SmoothProgressBar : UserControl { ! 8: int min = 0; ! 9: int max = 100; ! 10: int val = 0; ! 11: Color Color1 = Color.Black; ! 12: Color Color2 = Color.White; ! 13: ! 14: protected override void OnResize(EventArgs e) { ! 15: Invalidate(); ! 16: } ! 17: ! 18: protected override void OnPaint(PaintEventArgs e) { ! 19: using (Graphics graphics = e.Graphics) { ! 20: using (Brush brush = new LinearGradientBrush(ClientRectangle, Color1, Color2, 0.0F)) { ! 21: float percent = (val - min) / (float)(max - min); ! 22: Rectangle rect = ClientRectangle; ! 23: ! 24: // Calculate area for drawing the progress. ! 25: rect.Width = (int)(rect.Width * percent); ! 26: ! 27: // Draw the progress meter. ! 28: graphics.FillRectangle(brush, rect); ! 29: } ! 30: ! 31: // Draw a three-dimensional border around the control. ! 32: Draw3DBorder(graphics); ! 33: } ! 34: } ! 35: ! 36: public int Minimum { ! 37: get { ! 38: return min; ! 39: } ! 40: ! 41: set { ! 42: min = Math.Max(0, Math.Min(max, value)); ! 43: ! 44: if (val < min) { ! 45: val = min; ! 46: } ! 47: ! 48: Invalidate(); ! 49: } ! 50: } ! 51: ! 52: public int Maximum { ! 53: get { ! 54: return max; ! 55: } ! 56: ! 57: set { ! 58: if (value < min) { ! 59: min = value; ! 60: } ! 61: ! 62: max = value; ! 63: ! 64: if (val > max) { ! 65: val = max; ! 66: } ! 67: ! 68: Invalidate(); ! 69: } ! 70: } ! 71: ! 72: public int Value { ! 73: get { ! 74: return val; ! 75: } ! 76: ! 77: set { ! 78: int oldValue = val; ! 79: ! 80: // Make sure that the value does not stray outside the valid range. ! 81: if (value < min) { ! 82: val = min; ! 83: } else if (value > max) { ! 84: val = max; ! 85: } else { ! 86: val = value; ! 87: } ! 88: ! 89: // Invalidate only the changed area. ! 90: float percent; ! 91: ! 92: Rectangle newValueRect = ClientRectangle; ! 93: Rectangle oldValueRect = ClientRectangle; ! 94: ! 95: // Use a new value to calculate the rectangle for progress. ! 96: percent = (val - min) / (float)(max - min); ! 97: newValueRect.Width = (int)(newValueRect.Width * percent); ! 98: ! 99: // Use an old value to calculate the rectangle for progress. ! 100: percent = (oldValue - min) / (float)(max - min); ! 101: oldValueRect.Width = (int)(oldValueRect.Width * percent); ! 102: ! 103: Rectangle updateRect = new Rectangle(); ! 104: ! 105: // Find only the part of the screen that must be updated. ! 106: if (newValueRect.Width > oldValueRect.Width) { ! 107: updateRect.X = oldValueRect.Size.Width; ! 108: updateRect.Width = newValueRect.Width - oldValueRect.Width; ! 109: } else { ! 110: updateRect.X = newValueRect.Size.Width; ! 111: updateRect.Width = oldValueRect.Width - newValueRect.Width; ! 112: } ! 113: ! 114: updateRect.Height = Height; ! 115: ! 116: // Invalidate the intersection region only. ! 117: Invalidate(updateRect); ! 118: } ! 119: } ! 120: ! 121: public Color ProgressBarColor1 { ! 122: get { ! 123: return Color1; ! 124: } ! 125: ! 126: set { ! 127: Color1 = value; ! 128: ! 129: // Invalidate the control to get a repaint. ! 130: Invalidate(); ! 131: } ! 132: } ! 133: ! 134: public Color ProgressBarColor2 { ! 135: get { ! 136: return Color2; ! 137: } ! 138: ! 139: set { ! 140: Color2 = value; ! 141: ! 142: // Invalidate the control to get a repaint. ! 143: Invalidate(); ! 144: } ! 145: } ! 146: ! 147: private void Draw3DBorder(Graphics g) { ! 148: int PenWidth = (int)Pens.White.Width; ! 149: ! 150: g.DrawLine(Pens.DarkGray, ! 151: new Point(ClientRectangle.Left, ClientRectangle.Top), ! 152: new Point(ClientRectangle.Width - PenWidth, ClientRectangle.Top)); ! 153: g.DrawLine(Pens.DarkGray, ! 154: new Point(ClientRectangle.Left, ClientRectangle.Top), ! 155: new Point(ClientRectangle.Left, ClientRectangle.Height - PenWidth)); ! 156: g.DrawLine(Pens.White, ! 157: new Point(ClientRectangle.Left, ClientRectangle.Height - PenWidth), ! 158: new Point(ClientRectangle.Width - PenWidth, ClientRectangle.Height - PenWidth)); ! 159: g.DrawLine(Pens.White, ! 160: new Point(ClientRectangle.Width - PenWidth, ClientRectangle.Top), ! 161: new Point(ClientRectangle.Width - PenWidth, ClientRectangle.Height - PenWidth)); ! 162: } ! 163: } ! 164: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.