andrew4582

GradientPanel [WinForm]

Nov 5th, 2011
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.50 KB | None | 0 0
  1. using System;
  2. using System.ComponentModel;
  3. using System.Drawing;
  4. using System.Drawing.Drawing2D;
  5. using System.Runtime.InteropServices;
  6.  
  7. namespace System.Windows.Forms {
  8.  
  9.     [ComVisible(false)]
  10.     public class GradientPanel:Panel {
  11.  
  12.         GradientPanelProperties _properties;
  13.          
  14.         public GradientPanel() {
  15.             _properties = new GradientPanelProperties();
  16.             _properties.GradientMode = LinearGradientMode.Horizontal;
  17.             _properties.LeftColor = Color.Black;
  18.             _properties.RightColor = Color.White;
  19.         }
  20.  
  21.         protected override void OnResize(EventArgs eventargs) {
  22.             if(base.DesignMode) {
  23.                 base.OnResize(eventargs);
  24.                 return;
  25.             }
  26.             this.Invalidate(true);
  27.         }
  28.         protected override void OnPaint(PaintEventArgs e) {
  29.             this.PaintGradient(e.Graphics,e.ClipRectangle);
  30.         }
  31.  
  32.         protected override void OnPaintBackground(PaintEventArgs pevent) {
  33.             if(base.DesignMode) {
  34.                 this.PaintPanel(pevent.Graphics,pevent.ClipRectangle);
  35.             }
  36.         }
  37.  
  38.         protected void PaintGradient(Graphics graphics,Rectangle clipRectangle) {
  39.             if(((graphics != null) && ((clipRectangle.Width != 0) && (clipRectangle.Height != 0))) && ((base.ClientRectangle.Width != 0) && (base.ClientRectangle.Height != 0))) {
  40.                 using(LinearGradientBrush brush = new LinearGradientBrush(base.ClientRectangle,this.LeftColor,this.RightColor,this.GradientMode)) {
  41.                     if(this.BellShape) {
  42.                         brush.SetSigmaBellShape(0.5f,1f);
  43.                     }
  44.                     else if(this.TriangleShape) {
  45.                         brush.SetBlendTriangularShape(0.5f,1f);
  46.                     }
  47.                     graphics.FillRectangle(brush,clipRectangle);
  48.                 }
  49.             }
  50.         }
  51.  
  52.         public virtual void PaintPanel(Graphics graphics,Rectangle clipRectangle) {
  53.             this.PaintGradient(graphics,clipRectangle);
  54.         }
  55.        
  56.         [Category("_GradientPanel")]
  57.         [Description("Gradient Panel Properties")]
  58.         public GradientPanelProperties Properties {
  59.             get {
  60.                 return _properties;
  61.             }
  62.             set {
  63.                 _properties = value ?? new GradientPanelProperties();
  64.             }
  65.         }
  66.  
  67.         [Category("_GradientPanel")]
  68.         [Description("Apply a bell-shaped distribution to the gradient?"),DefaultValue(false)]
  69.         public bool BellShape {
  70.             get {
  71.                 return _properties.BellShape;
  72.             }
  73.             set {
  74.                 _properties.BellShape = value;
  75.             }
  76.         }
  77.  
  78.         [Category("_GradientPanel")]
  79.         [Description("The LinearGradientMode enumeration describing the gradient."),DefaultValue(0)]
  80.         public LinearGradientMode GradientMode {
  81.             get {
  82.                 return _properties.GradientMode;
  83.             }
  84.             set {
  85.                 _properties.GradientMode = value;
  86.             }
  87.         }
  88.  
  89.         [Category("_GradientPanel")]
  90.         [Description("Starting color for the gradient"),DefaultValue(typeof(Color),"Black")]
  91.         public Color LeftColor {
  92.             get {
  93.                 return _properties.LeftColor;
  94.             }
  95.             set {
  96.                 _properties.LeftColor = value;
  97.                 base.Invalidate();
  98.             }
  99.         }
  100.  
  101.         [Category("_GradientPanel")]
  102.         [DefaultValue(typeof(Color),"White"),Description("Ending color for the gradient")]
  103.         public Color RightColor {
  104.             get {
  105.                 return _properties.RightColor;
  106.             }
  107.             set {
  108.                 _properties.RightColor = value;
  109.                 base.Invalidate();
  110.             }
  111.         }
  112.         [Category("_GradientPanel")]
  113.         [Description("Apply a triangle-shaped distribution to the gradient?"),DefaultValue(false)]
  114.         public bool TriangleShape {
  115.             get {
  116.                 return _properties.TriangleShape;
  117.             }
  118.             set {
  119.                 _properties.TriangleShape = value;
  120.             }
  121.         }
  122.  
  123.         public class GradientPanelProperties {
  124.             public bool BellShape { get; set; }
  125.             public LinearGradientMode GradientMode { get; set; }
  126.             public Color LeftColor { get; set; }
  127.             public Color RightColor { get; set; }
  128.             public bool TriangleShape { get; set; }
  129.         }
  130.     }
  131.  
  132. }
Advertisement
Add Comment
Please, Sign In to add comment