可以显示渐变背景色的Lable

王朝other·作者佚名  2006-01-09
宽屏版  字体: |||超大  

///如未特别说明,本人所发表的技术文章都为原创, 任何人引用都请注明出处,并包含本声明

///作者: CSDN网名alias88,邮件:alias88@163.com,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;

}

}

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有