Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add borders to label in Windows Forms?

Tags:

c#

winforms

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 TransparencyKey on Lime and when I click on something I changed the BackColor to Lime and set the FormBorderStyle to None. But the problem is with what I'm doing right now is that the white label has no borders, so You can't really see it. I know about the BorderStyle property and this is not what I want, I want the border to be exactly around the text so you can see the text above other things. Is there any way to add borders to a label?

Here's my code, by the way:

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);
    }
}
like image 360
Cokegod Avatar asked Dec 04 '22 10:12

Cokegod


2 Answers

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);
like image 176
Tim S Avatar answered Dec 21 '22 03:12

Tim S


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).

like image 28
KeithS Avatar answered Dec 21 '22 02:12

KeithS