LinkLabel边框颜色
问题描述:
我正在使用LinkLabel
,并且想要将边框设置为红色。 我将BorderStyle
设置为FixedSingle
,并将ForeColor
设为红色,但只有文本为红色,边框仍为黑色。LinkLabel边框颜色
如果我对Label
控件做同样的操作,我会得到边框和文字为红色,有什么建议吗?
答
设置BorderStyle
为None
并绘制自己的。
private void linkLabel1_Paint(object sender, PaintEventArgs e)
{
if (linkLabel1.ForeColor == Color.Red)
ControlPaint.DrawBorder(e.Graphics, linkLabel1.DisplayRectangle, linkLabel1.ForeColor, ButtonBorderStyle.Solid);
else
ControlPaint.DrawBorder(e.Graphics, linkLabel1.DisplayRectangle, Color.Black, ButtonBorderStyle.Solid);
}
答
LinkLabels实际上没有边框颜色属性,但可以挂钩到OnPaint事件并绘制自己的边框。有代码显示如何在windows-tech.info处执行此操作。
正如他们在那里提到的那样,如果您打算重新使用LinkLabel控件,您可以从中获得新的控件。在这一点上,你甚至可以添加一个“CustomBorderColor”属性并使用它。一个未设置的颜色会变成Empty,所以我会选择默认为透明的。像这样:
using System.Drawing.Color;
private Color m_CustomBorderColor = Color.Transparent;
public Color CustomBorderColor
{
get { return m_CustomBorderColor; }
set { m_CustomBorderColor = value; }
}