Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

.NET System Brushes Painter

0.00/5 (No votes)
9 Mar 2004 2  
Shows how to use different brushes and write owner drawn listbox

Sample Image - SystemPainter.jpg

Introduction

System painter is a little utility for viewing .NET framework system defined hatches from System.Drawing.Drawing2D.HatchStyle and windows system colors and named colors from Color structure.

The example shows how to use different brushes and how to extend ListBox with owner drawn code.

Painters

The code uses 2 different painters (with easy ability to create new different ones). All painters implement IPainter interface. All the interface does it to get the name that will show in list box and Brush used for filling.

    public interface IPainter
    {
        Brush PaintBrush
        {
            get;
        }

        string Name
        {
            get;
        }
    }

We also have utility enumeration for available painters:

    public enum PaintType
    {
        Hatches,        // draw hatches from HatchStyle

        SystemColors,    // draw system colors 

        Colors            // draw NamedColors (except system colors)

    }

First painter is the color painter. All it does is filling the listbox item with solid color:

    public class ColorPainter : IPainter
    {
        Color color;
        
        public ColorPainter(Color color)
        {
            this.color = color;
        }

        public string Name
        {
            get { return color.ToKnownColor().ToString(); }
        }

        public Brush PaintBrush
        {
            get { return new SolidBrush(color); }
        }
    }

And another one is Hatch painter that uses hatch brush:

    public class HatchPainter : IPainter
    {
        HatchStyle style;    
        Color color1;    // foreground color for hatch

        Color color2;    // background color for hatch


        public HatchPainter(HatchStyle style, Color color1, Color color2)
        {
            this.style = style;
            this.color1 = color1;
            this.color2 = color2;            
        }

        /// <SUMMARY>

        /// for name just return hatch enum name

        /// </SUMMARY>

        public string Name
        {
            get { return style.ToString();    }
        }

        /// <SUMMARY>

        /// return HatchBrush with current style and colors

        /// </SUMMARY>

        public Brush PaintBrush
        {
            get { 
                return new HatchBrush(style, color1, color2);
            }
        }
    ....

That's it for painters. We have 3 different types and only 2 painters because SystemColors and Colors use the same painter.

ListBox

The implementation of the listbox is an owner drawn listbox. To do that all we have to do is:

    this.DrawMode = DrawMode.OwnerDrawFixed;
in constructor. We also have custom PaintType property on the listbox that decides what type of painter to use:
    public PaintType PaintType
    {
        get { return this.paintType; }
        set 
        {
            paintType = value;
            InitValues();
        }
    }

when we assign this property we initialize the items in the list:

void InitValues()
{
    this.Items.Clear();

    if (paintType == PaintType.Hatches)
    {
        // for hatches we get all values from HatchStyle enumeration

        foreach (HatchStyle style in Enum.GetValues(typeof(HatchStyle)))
        {
            IPainter painter = new HatchPainter(style, 
                 Color.Blue, Color.Transparent);
            Items.Add(painter);
        }
    }
    else if (paintType == PaintType.Colors)
    {
        // for colors we get all color values from KnownColor

        // and skip system colors

        foreach (KnownColor kcolor in Enum.GetValues(typeof(KnownColor)))
        {
            Color color = Color.FromKnownColor(kcolor);
            if (!color.IsSystemColor)
            {
                IPainter painter = new ColorPainter(color);
                Items.Add(painter);
            }
        }

    }
    else if (paintType == PaintType.SystemColors)
    {
        // for systemcolors we get all known colors

        // and only use system colors

        foreach (KnownColor kcolor in Enum.GetValues(typeof(KnownColor)))
        {
            Color color = Color.FromKnownColor(kcolor);
            if (color.IsSystemColor)
            {
                IPainter painter = new ColorPainter(color);
                Items.Add(painter);
            }
        }

    }

}

Depending on the type of painter that we want to use, we populate the list with IPainter values. Later when we draw the items we don't care about what type of list it is, because IPainter will tell us how to paint it. And finally the draw item code:

// each item in the list IPainter

IPainter painter = (IPainter)Items[e.Index];

// get brush from painter

Brush brush = painter.PaintBrush;

// fill the item with painters brush

g.FillRectangle(brush, e.Bounds);
g.DrawLine(Pens.Black, e.Bounds.X, e.Bounds.Bottom-1, 
  e.Bounds.Right, e.Bounds.Bottom-1);

// draw box with painter name

string name = painter.Name;
int width = (int)g.MeasureString(name, Font).Width;

g.FillRectangle((e.State & DrawItemState.Selected) == 
  DrawItemState.Selected ? Brushes.Yellow : Brushes.White,
  3, e.Bounds.Top+3, width+3, Font.Height+5);

g.DrawRectangle(Pens.Black, 3, e.Bounds.Top+3, width+3, Font.Height+5);
g.DrawString(painter.Name, Font, Brushes.Black, 5, e.Bounds.Top+5);

brush.Dispose();

We fill the area with painters brush, draw line below each item to separate them, and draw a box with painters name.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here