自定义label,实现了透明,文字靠左,中,右对齐,自动换行(仅针对汉字,有根据使用文字的方法,不过我比较懒就没写了),字体颜色,背景颜色,背景图,平铺背景图等。透明方法通过在 OnPaintBackground调用Parent重绘接口重绘背景并且本身不绘制背景来实现。
此DLL可在控件工具箱中使用,大部分修改即时显示。先贴一部分代码,留到有空再编辑咯。
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;
namespace labelDll
{
public enum Alignment
{
left, mid, right
};
///
/// 可以定义背景图片的Label
///
public partial class MyLabel : UserControl
{
#region 构造方法
public MyLabel()
{
backBuffer = new Bitmap(Width, Height);
}
protected override void OnPaint(PaintEventArgs e)
{
OnPaintBackground(e);
OnDrawText(e);
base.OnPaint(e);
}
#endregion
#region 私有字段
//背景图片
private Image image;
private string textString;
private Color textColor;
private Image fillImage;
private Image backBuffer;
private bool multiLine;
private Alignment textAlignment;
#endregion
#region 共有属性
///
/// 背景图片
///
[DefaultValue(1)]
public Alignment TextAlignment
{
get { return textAlignment; }
set { textAlignment = value; }
}
[DefaultValue(null)]
public Image Image
{
get { return image; }
set { image = value; }
}
[DefaultValue(false)]
public bool isMultiLine
{
get { return multiLine; }
set { multiLine = value; }
}
///
/// 显示的文字
///
[DefaultValue("")]
public string TextString
{
get { return textString; }
set
{
textString = value;
this.Refresh();
}
}
[DefaultValue(null)]
public Color TextColor
{
get { return textColor; }
set
{
textColor = value;
}
}
[DefaultValue(null)]
public Image FillImage
{
get { return fillImage; }
set { fillImage = value; }
}
public void ResetImage()
{
image = null;
}
public void ResetFillImage()
{
fillImage = null;
}
#endregion
#region 方法调用
///
/// 先在缓存内画好
///
public void OnDrawText(PaintEventArgs e)
{
using(SolidBrush tempSBrush = new SolidBrush(textColor))
{
//计算需要多少行来显示
int labelLength = ClientSize.Width;
SizeF sizeofText = e.Graphics.MeasureString(textString, this.Font);
int textWidth = (int)sizeofText.Width;
int iRowNum = textWidth / labelLength + 1;
//计算一个汉字的宽度以及高度
SizeF sizeofFont = e.Graphics.MeasureString("我", this.Font);
int fontWidth = (int)sizeofFont.Width;
int fontHeight = (int)sizeofFont.Height;
//计算一行最多能显示多少汉字
int maxFontPerLine = labelLength / fontWidth;
if (iRowNum > 1 && multiLine)
{
//for循环处理多行并且文字大于一行的情况下
for (int i = 0; i < iRowNum; i++)
{
//最后一行substring没有第二个参数,免得substring超出字符串长度
if (i < iRowNum - 1)
{
e.Graphics.DrawString(textString.Substring(i * maxFontPerLine, maxFontPerLine), this.Font, tempSBrush,
0, i * fontHeight);
}
else
{
string tempString = textString.Substring(i * maxFontPerLine);
SizeF sizeofTempString = e.Graphics.MeasureString(tempString, this.Font);
int tempStringWidth = (int)sizeofTempString.Width;
switch (textAlignment)
{
case Alignment.mid:
e.Graphics.DrawString(tempString, this.Font, tempSBrush, (labelLength - tempStringWidth) / 2, i * fontHeight);
break;
case Alignment.right:
e.Graphics.DrawString(tempString, this.Font, tempSBrush, (labelLength - tempStringWidth), i * fontHeight);
break;
default:
e.Graphics.DrawString(tempString, this.Font, tempSBrush, 0, i * fontHeight);
break;
}
}
}
}
//单行情况
else
{
switch (textAlignment)
{
case Alignment.mid:
e.Graphics.DrawString(textString, this.Font, tempSBrush, (labelLength - textWidth) / 2, 0);
break;
case Alignment.right:
e.Graphics.DrawString(textString, this.Font, tempSBrush, (labelLength - textWidth), 0);
break;
default:
e.Graphics.DrawString(textString, this.Font, tempSBrush, 0, 0);
break;
}
}
}
}
protected override void OnPaintBackground(PaintEventArgs e)
{
if (image != null)
{
using (Graphics gxBuffer = Graphics.FromImage(backBuffer))
{
gxBuffer.Clear((Color.Black));
gxBuffer.DrawImage(image, 0, 0);
}
e.Graphics.DrawImage(backBuffer, 0, 0);
return;
}
if (fillImage != null)
{
using (Graphics gxBuffer = Graphics.FromImage(backBuffer))
{
gxBuffer.Clear((Color.Black));
using (TextureBrush tempTBrush = new TextureBrush(fillImage))
{
gxBuffer.FillRectangle(tempTBrush, this.ClientRectangle);
}
e.Graphics.DrawImage(backBuffer, 0, 0);
}
return;
}
if (BackColor != Color.Transparent)
{
using (SolidBrush tempBrushBackColor = new SolidBrush(BackColor))
{
e.Graphics.FillRectangle(tempBrushBackColor, this.ClientRectangle);
}
}
else
{
IPaintControl parent = Parent as IPaintControl;
if (parent != null)
{
parent.InvokePaintBackground(e, this.Location);
}
}
}
#endregion
}
}