How can I add borders to label in Windows Forms?

后端 未结 5 1527
误落风尘
误落风尘 2021-01-19 04:32

I\'m trying to create a form with white label inside, that when I click on something the form will disappear and only show the label. So far I tried to put the TransparencyK

相关标签:
5条回答
  • 2021-01-19 04:51

    Well sure; there is a BorderStyle property on Label that can be set to FixedSingle or Fixed3D. FixedSingle is a single-pixel border in the ForeColor color, while Fixed3D is a beveled 3D border using greyscales of the label's background.

    EDIT: OK, a little more detail in what exactly is needed. As I see it you have a couple options.

    1. Put two labels, one on top of the other, with the same content and formatting EXCEPT the one in back is white and the one in front is black, and the label in back is offset from the one in front by one pixel in the X and/or Y dimensions. You'll get a white "shadow" behind the black text. You could even set up four labels, each offset 1 pixel in both X and y, for a complete "halo". You could set this up as a UserControl if you wanted to do this in multiple places; set the text of the control once and the control will populate all 5 labels. You could try playing with font size or weight, but I doubt you'd get something that lined up correctly and had a perfect 1-pixel border around the letters in all cases.

    2. Create an image of your text on a magenta background, ring it in white, and save it as a bitmap with the magenta keyed as the transparent color. Then, use the image in the label (or a PictureBox).

    0 讨论(0)
  • 2021-01-19 04:51

    Set the BorderStyle property of the label control to FixedSingle

    0 讨论(0)
  • 2021-01-19 05:00

    Set its Label.BorderStyle Property to one of the BorderStyle Enumeration.

    0 讨论(0)
  • 2021-01-19 05:07

    If anyone is still looking, here is what I did (mostly copied from this site)

    Create a new class, CustomLabel.cs for instance. Here's an example:

    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);
               } 
        }
    

    You can then use it like this:

                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);
    
    0 讨论(0)
  • 2021-01-19 05:12

    what about the borderstyle property? set it to FixedSingle in the properties windows.

    0 讨论(0)
提交回复
热议问题