|
|
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 GradientButton : Button { ! 8: public Color Color1 { get; set; } ! 9: public Color Color2 { get; set; } ! 10: public double HoverAlpha { get; set; } ! 11: public double DisableAlpha { get; set; } ! 12: ! 13: private bool Hovered = false; ! 14: ! 15: public GradientButton() { ! 16: Color1 = Color.Black; ! 17: Color2 = Color.White; ! 18: HoverAlpha = 0.875; ! 19: DisableAlpha = 0.644; ! 20: } ! 21: ! 22: protected override void OnMouseDown(MouseEventArgs mevent) { ! 23: Hovered = false; ! 24: base.OnMouseDown(mevent); ! 25: Refresh(); ! 26: } ! 27: ! 28: protected override void OnMouseUp(MouseEventArgs mevent) { ! 29: Hovered = true; ! 30: base.OnMouseUp(mevent); ! 31: Refresh(); ! 32: } ! 33: ! 34: protected override void OnMouseEnter(EventArgs e) { ! 35: Hovered = true; ! 36: base.OnMouseEnter(e); ! 37: Refresh(); ! 38: } ! 39: ! 40: protected override void OnMouseLeave(EventArgs e) { ! 41: Hovered = false; ! 42: base.OnMouseLeave(e); ! 43: Refresh(); ! 44: } ! 45: ! 46: protected override void OnPaint(PaintEventArgs e) { ! 47: Color c1 = Color1; ! 48: Color c2 = Color2; ! 49: Color c3 = BackColor; ! 50: Color c4 = ForeColor; ! 51: ! 52: if (!Enabled) { ! 53: c1 = Color.FromArgb(255, (int)(c1.R * DisableAlpha), (int)(c1.G * DisableAlpha), (int)(c1.B * DisableAlpha)); ! 54: c2 = Color.FromArgb(255, (int)(c2.R * DisableAlpha), (int)(c2.G * DisableAlpha), (int)(c2.B * DisableAlpha)); ! 55: c3 = Color.FromArgb(255, (int)(c3.R * DisableAlpha), (int)(c3.G * DisableAlpha), (int)(c3.B * DisableAlpha)); ! 56: c4 = Color.FromArgb(255, (int)(c4.R * DisableAlpha), (int)(c4.G * DisableAlpha), (int)(c4.B * DisableAlpha)); ! 57: } else if (!Hovered) { ! 58: c1 = Color.FromArgb(255, (int)(c1.R * HoverAlpha), (int)(c1.G * HoverAlpha), (int)(c1.B * HoverAlpha)); ! 59: c2 = Color.FromArgb(255, (int)(c2.R * HoverAlpha), (int)(c2.G * HoverAlpha), (int)(c2.B * HoverAlpha)); ! 60: } ! 61: ! 62: SizeF size = e.Graphics.MeasureString(Text, Font); ! 63: ! 64: using (Brush brush = new LinearGradientBrush(ClientRectangle, c1, c2, 90.0F)) { ! 65: e.Graphics.FillRectangle(brush, ClientRectangle); ! 66: } ! 67: ! 68: int thickness = 1; ! 69: int halfThickness = thickness / 2; ! 70: ! 71: using (Pen pen = new Pen(c3, thickness)) { ! 72: e.Graphics.DrawRectangle( ! 73: pen, new Rectangle( ! 74: halfThickness, halfThickness, ! 75: ClientRectangle.Width - thickness, ClientRectangle.Height - thickness ! 76: ) ! 77: ); ! 78: } ! 79: ! 80: Point point = new Point( ! 81: (ClientRectangle.Width - (int)size.Width) / 2, ! 82: (ClientRectangle.Height - (int)size.Height) / 2 ! 83: ); ! 84: ! 85: using (Brush brush = new SolidBrush(c4)) { ! 86: e.Graphics.DrawString(Text, Font, new SolidBrush(c3), new Point(point.X + 1, point.Y + 1)); ! 87: e.Graphics.DrawString(Text, Font, brush, point); ! 88: } ! 89: } ! 90: } ! 91: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.