我创建了一个继承自Label的标签控件,只要边框样式是固定的,就可以在属性中设置应用颜色和大小的边框。这样工作正常,除了尽管在调用base之后绘制新边框仍然绘制原始边框:
显然,我想只显示新边框,而不是原始边框。这是控件中的代码:
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (BorderStyle == BorderStyle.FixedSingle)
{
ControlPaint.DrawBorder(e.Graphics, DisplayRectangle, _borderColor, BorderWidth,
ButtonBorderStyle.Solid, _borderColor, BorderWidth, ButtonBorderStyle.Solid, _borderColor,
BorderWidth, ButtonBorderStyle.Solid, _borderColor, BorderWidth, ButtonBorderStyle.Solid);
}
}
这里是生成两个标签进行比较的代码:
private void labelButton_Click(object sender, EventArgs e)
{
var label = new AdvancedLabel
{
BorderColor = Color.Yellow,
BorderStyle = BorderStyle.FixedSingle,
BorderWidth = 3,
Location = new Point(10, 10),
Text = @"Advanced Label",
TextAlign = ContentAlignment.MiddleCenter
};
Controls.Add(label);
var label2 = new Label
{
BorderStyle = BorderStyle.FixedSingle,
Location = new Point(10, 35),
Text = @"Standard Label",
TextAlign = ContentAlignment.MiddleCenter
};
Controls.Add(label2);
}
那么,谁能告诉我这里缺少什么?我已经尝试了所有我能想到的东西!!