本文主要是介绍自绘Lable的Text,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
private void Form1_Load(object sender, EventArgs e)
{
label1.Paint += new System.Windows.Forms.PaintEventHandler(PaintEvent);
}
//自绘 显示提示信息
private void PaintEvent(object sender, PaintEventArgs e)
{
Label label = ((Label)sender);
Graphics g = e.Graphics;
SolidBrush blueBrush = new SolidBrush(label.BackColor);
Font fnt = new Font("SimSun", 10, FontStyle.Bold);
Rectangle rc = new Rectangle(0, 0, ((Label)sender).Width, ((Label)sender).Height);
g.FillRectangle(blueBrush, rc);
System.Windows.Forms.TextRenderer.DrawText(
g, label.Text, label.Font, rc, label.ForeColor,
TextFormatFlags.WordEllipsis | ContentAlignmentToTextFormatFlags(label.TextAlign));
}
TextFormatFlags ContentAlignmentToTextFormatFlags(ContentAlignment contentAlignment)
{
TextFormatFlags textFormatFlags = new TextFormatFlags();
switch (contentAlignment)
{
case ContentAlignment.BottomCenter:
//上,左右居中
textFormatFlags = TextFormatFlags.Bottom | TextFormatFlags.HorizontalCenter;
break;
case ContentAlignment.BottomLeft:
textFormatFlags = TextFormatFlags.Bottom | TextFormatFlags.Left;
break;
case ContentAlignment.BottomRight:
textFormatFlags = TextFormatFlags.Bottom | TextFormatFlags.Right;
break;
case ContentAlignment.MiddleCenter:
textFormatFlags = TextFormatFlags.VerticalCenter | TextFormatFlags.HorizontalCenter;
break;
case ContentAlignment.MiddleLeft:
textFormatFlags = TextFormatFlags.VerticalCenter | TextFormatFlags.Left;
break;
case ContentAlignment.MiddleRight:
textFormatFlags = TextFormatFlags.VerticalCenter | TextFormatFlags.Right;
break;
case ContentAlignment.TopCenter:
textFormatFlags = TextFormatFlags.Top | TextFormatFlags.HorizontalCenter;
break;
case ContentAlignment.TopLeft:
textFormatFlags = TextFormatFlags.Top | TextFormatFlags.Left;
break;
case ContentAlignment.TopRight:
textFormatFlags = TextFormatFlags.Top | TextFormatFlags.Right;
break;
default:
break;
}
return textFormatFlags;
}
这篇关于自绘Lable的Text的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!