绘制 ComboBox 的 Item

为了让程序更具个性,有时候我们需要在ComboBox中绘制图形。效果如图所示。

combox2_1

combox2_2

combox2_3

代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;

partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
        this.comboBox1.Items.Add("Solid Brush");
        this.comboBox1.Items.Add("Horizontal");
        this.comboBox1.Items.Add("Min");
        this.comboBox1.Items.Add("Vertical");
        this.comboBox1.Items.Add("Forward Diagonal");
        this.comboBox1.Items.Add("Backward Diagonal");
        this.comboBox1.Items.Add("Cross");
        this.comboBox1.Items.Add("Large Grid");
        this.comboBox1.Items.Add("Max");
        this.comboBox1.Items.Add("Diagonal Cross");

        this.comboBox1.SelectedIndex = 0;
        this.comboBox1.DrawMode = DrawMode.OwnerDrawVariable;
        this.comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
        this.comboBox1.DropDownWidth = 190;
        this.comboBox1.DropDownHeight = 300;
        this.comboBox1.MeasureItem += new MeasureItemEventHandler(comboBox1_MeasureItem);
        this.comboBox1.DrawItem += new DrawItemEventHandler(comboBox1_DrawItem);
    }

    void comboBox1_MeasureItem(object sender, MeasureItemEventArgs e)
    {
        string displayText = this.comboBox1.Items[e.Index].ToString();
        SizeF stringSize = e.Graphics.MeasureString(displayText, this.Font);
        e.ItemHeight = (int)stringSize.Height;
        e.ItemWidth = (int)stringSize.Width;
    }

    private String RemoveSpaces(String str1)
    {
        int start;
        int at;
        int end;

        String str2 = String.Copy(str1);

        at = 0;
        end = str2.Length - 1;
        start = 0;

        while ((start <= end) && (at > -1))
        {
            // start+count must be a position within str2.
            at = str2.IndexOf(" ", start);
            if (at == -1) break;
            str2 = str2.Remove(at, 1);
            start = at + 1;
        }

        return str2;
    }

    private HatchStyle getHatchStyle(String s)
    {
        String str = RemoveSpaces(s);
        return (HatchStyle)Enum.Parse(typeof(HatchStyle), str, true);
    }

    void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
    {
        //throw new NotImplementedException();
        e.DrawBackground();

        Rectangle r = e.Bounds;

        if (e.Index != -1)
        {
            if (e.Index > 0)
            {
                Rectangle rd = r;
                rd.Width = rd.Left + 25;

                Rectangle rt = r;
                r.X = rd.Right;

                string displayText = this.comboBox1.Items[e.Index].ToString();
                HatchStyle hs = this.getHatchStyle(displayText);

                using (HatchBrush b = new HatchBrush(hs, e.ForeColor, e.BackColor))
                {
                    e.Graphics.FillRectangle(b, rd);
                }

                StringFormat sf = new StringFormat();
                sf.Alignment = StringAlignment.Near;

                using (SolidBrush sb = new SolidBrush(Color.White))
                {
                    if ((e.State & DrawItemState.Focus) == 0)
                    {
                        sb.Color = SystemColors.Window;
                        e.Graphics.FillRectangle(sb, r);
                        sb.Color = SystemColors.WindowText;
                        e.Graphics.DrawString(displayText, this.Font, sb, r, sf);
                    }
                    else
                    {
                        sb.Color = SystemColors.Highlight;
                        e.Graphics.FillRectangle(sb, r);
                        sb.Color = SystemColors.HighlightText;
                        e.Graphics.DrawString(displayText, this.Font, sb, r, sf);
                    }
                }
            }   // if (e.Index > 0)
            else
            {
                using (SolidBrush sb = new SolidBrush(Color.White))
                {
                    if ((e.State & DrawItemState.Focus) == 0)
                    {
                        sb.Color = SystemColors.Window;
                        e.Graphics.FillRectangle(sb, e.Bounds);
                        string displayText = this.comboBox1.Items[e.Index].ToString();
                        sb.Color = SystemColors.WindowText;
                        e.Graphics.DrawString(displayText, this.Font, sb, e.Bounds);
                    }
                    else
                    {
                        sb.Color = SystemColors.Highlight;
                        e.Graphics.FillRectangle(sb, e.Bounds);
                        string displayText = this.comboBox1.Items[e.Index].ToString();
                        sb.Color = SystemColors.HighlightText;
                        e.Graphics.DrawString(displayText, this.Font, sb, e.Bounds);
                    }
                }
            }
            e.DrawFocusRectangle();
        }
    }
Contributors: FHL