15

I'm dynamically adding Labels to panels in my code.

Something I want to do is be able to outline the font so that it can stand out from the background color of the panel.

The problem is I don't know how to create a outline for my font or even a shadow effect in C# using Winforms.

Anyone know what I should look at or can point me in the right direction? If you don't understand what I mean, the following picture is what I would like: (the Outer lining)

enter image description here

Kelsey Abreu
  • 1,094
  • 3
  • 17
  • 42

1 Answers1

34

I think you have to custom paint your own control. Here is an example for a Label. Note that it's just a demo, you should try finding out more on custom painting in winforms:

public class CustomLabel : Label
{
    public CustomLabel()
    {
        OutlineForeColor = Color.Green;
        OutlineWidth = 2;
    }
    public Color OutlineForeColor { get; set; }
    public float OutlineWidth { get; set; }
    protected override void OnPaint(PaintEventArgs e)
    {
        e.Graphics.FillRectangle(new SolidBrush(BackColor), ClientRectangle);
        using (GraphicsPath gp = new GraphicsPath())
        using (Pen outline = new Pen(OutlineForeColor, OutlineWidth)
            { LineJoin = LineJoin.Round})
        using(StringFormat sf = new StringFormat())
        using(Brush foreBrush = new SolidBrush(ForeColor))
        {
            gp.AddString(Text, Font.FontFamily, (int)Font.Style,
                Font.Size, ClientRectangle, sf);                                
            e.Graphics.ScaleTransform(1.3f, 1.35f);
            e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
            e.Graphics.DrawPath(outline, gp);                
            e.Graphics.FillPath(foreBrush, gp);                            
        }
    }
}

You can change the outline color via OutlineForeColor property, you can change the outline width via the OutlineWidth property. When you change these properties in the designer, the effect is not applied immediately (because there is not any code to do that, I want to keep it short and simple), the effect is applied only when the form is focused.

What you can add more is mapping the TextAlign to the Alignment of the StringFormat (named sf in the code), you can also override some event raising methods to add more control over the look and feel (such as to change the ForeColor when the mouse is over the label...). You can even create some shadow effect and glow effect (it requires a little much more code).

enter image description here

Ryan Lundy
  • 204,559
  • 37
  • 180
  • 211
King King
  • 61,710
  • 16
  • 105
  • 130
  • 1
    Will definitely have to work around my current solution. But that is my problem. Thanks alot @king-king. – Kelsey Abreu Nov 08 '13 at 04:06
  • I know this is about 7 years too late, but when I add ``sf.Alignment = StringAlignment.Center;`` it isn't centred, it's off to the right slightly, I can't make sense of it as before applying this custom class the text was centred... – Jay Croghan Nov 23 '20 at 06:45
  • I found a full solution here: https://www.codeproject.com/Articles/20464/A-Simple-Label-Control-with-Border-Effect – Jay Croghan Nov 23 '20 at 06:57