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

Cok*_*god 4 c# winforms

我正在尝试创建一个内部带有白色标签的表单,当我点击某个表单时,表单将消失并仅显示标签.到目前为止,我尝试将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);
    }
}
Run Code Online (Sandbox Code Playgroud)

Tim*_*m S 7

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

例如,创建一个新类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);
           } 
    }
Run Code Online (Sandbox Code Playgroud)

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

            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);
Run Code Online (Sandbox Code Playgroud)