如何在Windows窗体中为标签添加边框?

我正在尝试创建一个内部带有白色标签的表单,当我点击某个表单时,表单将消失并仅显示标签。 到目前为止,我试图将TransparencyKey放在Lime上,当我点击某些内容时,我将BackColor更改为Lime并将FormBorderStyle设置为None。 但问题是我现在正在做的是白色标签没有边框,所以你不能真正看到它。 我知道BorderStyle属性,这不是我想要的,我希望边框正好在文本周围,所以你可以看到其他东西上面的文字。 有没有办法为标签添加边框?

顺便说一句,这是我的代码:

private void label1_Click(object sender, EventArgs e) { if (BackColor == Color.Lime) { FormBorderStyle = FormBorderStyle.Sizable; BackColor = Color.Black; Location = new Point(Left - 8, Top - 30); } else { FormBorderStyle = FormBorderStyle.None; BackColor = Color.Lime; Location = new Point(Left + 8, Top + 30); } } 

当然可以 Label上有一个BorderStyle属性,可以设置为FixedSingle或Fixed3D。 FixedSingle是ForeColor颜色的单像素边框,而Fixed3D是使用标签背景灰度的斜面3D边框。

编辑:好的,更确切地说明需要什么。 在我看来,你有几个选择。

  1. 放置两个标签,一个在另一个的顶部,具有相同的内容和格式除了后面的一个是白色而前面的一个是黑色,后面的标签偏离前面的标签X中的一个像素和/或Y尺寸。 你会在黑色文字后面看到一个白色的“阴影”。 您甚至可以设置四个标签,每个标签在X和y中都偏移1个像素,以获得完整的“光环”。 如果你想在多个地方做这个,你可以把它设置为UserControl; 设置控件的文本一次,控件将填充所有5个标签。 您可以尝试使用字体大小或重量,但我怀疑你会得到正确排列的东西,并且在所有情况下在字母周围都有一个完美的1像素边框。

  2. 在洋红色背景上创建文本图像,将其环绕为白色,并将其保存为位图,并将洋红色键入为透明色。 然后,使用标签(或PictureBox)中的图像。

如果有人还在寻找,这就是我所做的(主要是从这个网站复制)

例如,创建一个新类CustomLabel.cs。 这是一个例子:

 public class CustomLabel : Label { protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); ControlPaint.DrawBorder(e.Graphics, ClientRectangle, Color.Red, 5, ButtonBorderStyle.Solid, Color.Red, 5, ButtonBorderStyle.Solid, Color.Red, 5, ButtonBorderStyle.Solid, Color.Red, 5, ButtonBorderStyle.Solid); } } 

然后你可以像这样使用它:

  Form newForm = new Form(); CustomLabel newLabel = new CustomLabel(); newForm.Controls.Add(newLabel); newLabel.BackColor = Color.Black; newLabel.Font = new System.Drawing.Font("Microsoft Arial", 18F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(0))); newLabel.ForeColor = Color.Crimson; newLabel.Text = "Some text on a topmost transparent form window"; newForm.Show(); newForm.TopMost = true; newLabel.AutoSize = true; newLabel.Location = new Point(230, 375); 

将Label控件的BorderStyle属性设置为FixedSingle

将其Label.BorderStyle Property设置为BorderStyle Enumeration

那么的bordertyle属性呢? 在属性窗口中将其设置为FixedSingle。