Search code examples
c#winformscontrolsdrop-shadow

Drop shadow in Winforms Controls?


is there a way to add a drop shadow to controls?

are there any controls out there with this feature?


Solution

  • This question has been around for 6 years and needs an answer. I hope that anyone who needs to do this can extrapolate an answer for any control set from my solution. I had a panel and wanted to draw a drop shadow underneath every child control - in this instance one or more panels (but the solution should hold good for other control types with some minor code changes).

    As the drop shadow for a control has to be drawn on the surface of that control's container we start by adding a function to the container's Paint() event.

    Container.Paint += dropShadow;
    

    dropShadow() looks like this:

        private void dropShadow(object sender, PaintEventArgs e)
        {
            Panel panel = (Panel)sender;
            Color[] shadow = new Color[3];
            shadow[0] = Color.FromArgb(181, 181, 181);
            shadow[1] = Color.FromArgb(195, 195, 195);
            shadow[2] = Color.FromArgb(211, 211, 211);
            Pen pen = new Pen(shadow[0]);
            using (pen)
            {
                foreach (Panel p in panel.Controls.OfType<Panel>())
                {
                    Point pt = p.Location;
                    pt.Y += p.Height;
                    for (var sp = 0; sp < 3; sp++)
                    {
                        pen.Color = shadow[sp];
                        e.Graphics.DrawLine(pen, pt.X, pt.Y, pt.X + p.Width - 1, pt.Y);
                        pt.Y++;
                    }
                }
            }
        }
    

    Clearly you can pick a different control type from the container's collection and you can vary the colour and depth of the shadow with some minor tweaks.