可以显示渐变背景色的Lable

类别:.NET开发 点击:0 评论:0 推荐:

///如未特别说明,本人所发表的技术文章都为原创, 任何人引用都请注明出处,并包含本声明
///作者: CSDN网名alias88,邮件:[email protected],QQ:63343

 

using System;

using System.Collections;

using System.ComponentModel;

using System.Drawing;

using System.Drawing.Drawing2D;

using System.Windows.Forms;

 

namespace upLibrary.upControls

{

  /// <summary>

  /// 可以显示渐变背景色的Lable

  /// </summary>

  public class upLabel : System.Windows.Forms.Label  

  {

    /// <summary>

    /// 必需的设计器变量。

    /// </summary>

    private System.ComponentModel.Container components = null;

 

    private Color startColor = Color.Wheat ;

    private Color endColor = Color.LimeGreen;

    private Brush baseBackground=null;

    private bool  showGradient=true;

    private float mAngle=75;

    private bool _ShowText;

 

 

    public upLabel()

    {

      InitializeComponent();

    }

 

    /// <summary>

    /// 清理所有正在使用的资源。

    /// </summary>

    protected override void Dispose( bool disposing )

    {

      if( disposing )

      {

        if(components != null)

        {

          components.Dispose();

        }

      }

      base.Dispose( disposing );

    }

 

    #region 组件设计器生成的代码

    /// <summary>

    /// 设计器支持所需的方法 - 不要使用代码编辑器

    /// 修改此方法的内容。

    /// </summary>

    private void InitializeComponent()

    {

      //

      // upLabel

      //

      this.Size = new System.Drawing.Size(144, 21);

 

    }

    #endregion

 

 

    protected override void OnPaintBackground(PaintEventArgs e)

    {

      LinearGradientBrush lbs;

      base.OnPaintBackground(e);

      if (this.Disposing ) return ;

 

      if (baseBackground == null)

      {

        if (ShowGradient)

        {

          

          lbs = new LinearGradientBrush(

            ClientRectangle ,

            StartColor  ,

            EndColor,

            Angle);

          baseBackground=lbs;

 

        }

        else if (BackgroundImage != null)

        {

          baseBackground = new TextureBrush(BackgroundImage);

        }

        else

        {

          baseBackground = new SolidBrush(BackColor);

        }

      }

      e.Graphics.FillRectangle(baseBackground, ClientRectangle);

      e.Graphics.Flush();

    }

 

 

    [

    Category("渐变"),

    DefaultValue(true),

    Description("指示是否显示文本。")

    ]

    public bool ShowText

    {

      get

      {

        return _ShowText;

      }

      set

      {

        if (value != _ShowText)

        {

          _ShowText = value;

          if (baseBackground != null)

          {

            baseBackground.Dispose();

            baseBackground = null;

          }

          Invalidate();

        }

      }

    }

 

 

 

    

    [

    Category("渐变"),

    DefaultValue(true),

    Description("指示背景是否应显示颜色渐变。 ")

    ]

    public bool ShowGradient

    {

      get

      {

        return showGradient;

      }

      set

      {

        if (value != showGradient)

        {

          showGradient = value;

          if (baseBackground != null)

          {

            baseBackground.Dispose();

            baseBackground = null;

          }

          Invalidate();

        }

      }

    }

 

 

    [

    Category("渐变"),

    Description("指定渐变的角度")

    ]

    public float Angle

    {

      get

      {

        return mAngle;

      }

      set

      {

        mAngle = value;

        if (baseBackground != null && ShowGradient)

        {

          baseBackground.Dispose();

          baseBackground = null;

        }

        Invalidate();

      }

    }

 

 

 

    [

    Category("渐变"),

    Description("指定渐变的开始颜色")

    ]

    public Color StartColor

    {

      get

      {

        return startColor;

      }

      set

      {

        startColor = value;

        if (baseBackground != null && ShowGradient)

        {

          baseBackground.Dispose();

          baseBackground = null;

        }

        Invalidate();

      }

    }

 

    [

    Category("渐变"),

    Description("指定渐变的结束颜色")

    ]

    public Color EndColor

    {

      get

      {

        return endColor;

      }

      set

      {

        endColor = value;

        if (baseBackground != null && ShowGradient)

        {

          baseBackground.Dispose();

          baseBackground = null;

        }

        Invalidate();

      }

    }

 

 

    [Description("当在设计器中更改属性时实现属性的序列化(持久性)")]

    public bool ShouldSerializeStartColor()

    {

      return !(startColor == Color.Wheat);

    }

 

        

    public bool ShouldSerializeEndColor()

    {

      return !(endColor == Color.LimeGreen);

    }

 

 

    

    

    

    protected override void OnBackColorChanged(EventArgs e)

    {

      base.OnBackColorChanged(e);

      if ((baseBackground != null) && (!ShowGradient))

      {

        baseBackground.Dispose();

        baseBackground = null;

      }

    }

 

    protected override void OnTextChanged(EventArgs e)

    {

      base.OnTextChanged(e);

      if ((baseBackground != null) && (!ShowGradient))

      {

        baseBackground.Dispose();

        baseBackground = null;

      }

    }

 

    protected override void OnBackgroundImageChanged(EventArgs e)

    {

      base.OnBackgroundImageChanged(e);

      if ((baseBackground != null) && (!ShowGradient))

      {

        baseBackground.Dispose();

        baseBackground = null;

      }

    }

 

    

    protected override void OnResize(EventArgs e)

    {

      base.OnResize(e);

      if (baseBackground != null)

      {

        baseBackground.Dispose();

        baseBackground = null;

      }

    }

 

    

    

 

 

  }

}

 

本文地址:http://com.8s8s.com/it/it42167.htm