Introduction
This code demonstrates the most efficient way to implement the gradient property in a winform Label control
public class derLabel : System.Windows.Forms.Label { // Declare these variables to specify the left and the right colors of the rectangle private Color cLeft; private Color cRight;
// Get/Set the left color public Color LeftColor { get { return cLeft; } set { cLeft = value; } }
// Get/Set the right color public Color RightColor { get { return cRight; } set { cRight = value; } }
public derLabel() { // Default left/right rectangle colors cLeft = Color.Red; cRight = Color.Yellow; }
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) { // Declare a rectangle with the user specified width and height. As of now, the Upper // left and top positions have been hardcoded. This can be replaced by creating // another property for the width and height and specifying the value in the form load Rectangle oRect = new Rectangle(100,100,this.Width,this.Height);
// Fill the background of label (Rectangle) LinearGradientBrush oLinearbrush = new LinearGradientBrush(oRect, cLeft, cRight, Lineargradientmode.horizontal);
// Very important // You can also use the oLinearBrush.RotateTransform function to change the left // color and right color (swap them). The input parameter to the above function module // is an angle which is a float variable. Specify 180 to swap the left and right // colors. Also, if you loop through the angles, and insert a sleep time, you can // gradually swap the colors.
// Fill the rectangle with gradient using GDI+ e.Graphics.FillRectangle(oLinearbrush, oRect); } }
In the form load,
this.derLbl = new derLabel();
this.derLbl.LeftColor = Color.Blue; this.derLbl.LeftColor = Color.Yellow; this.derLbl.Name = "derLbl"; this.derLbl.Text = "Subbu"; this.derLbl.width = unit.pixel(200); this.derLbl.height = unit.pixel(100);
this.Controls.Add(derLbl);
|
No responses found. Be the first to respond and make money from revenue sharing program.
|