アットウィキロゴ

ImgComboBox

  • コンボリストに画像を表示する


using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class frmMain : Form
    {


        /// <summary>
        /// コンストラクタ
        /// </summary>
        public frmMain()
        {
            // コンポーネントの初期化
            InitializeComponent();
            Init();
        }

        /// <summary>
        /// 初期化処理
        /// </summary>
        private void Init()
        {
            Bitmap img;
            ExComboBox cmb = new ExComboBox();
            ImageList imgList = new ImageList();

            img = new Bitmap(15, 15);
            Graphics.FromImage(img).DrawString("あ", new Font("MS ゴシック", 9F, System.Drawing.FontStyle.Regular), Brushes.Green, 0, 0);
            imgList.Images.Add(img);

            img = new Bitmap(15, 15);
            Graphics.FromImage(img).DrawString("い", new Font("MS ゴシック", 9F, System.Drawing.FontStyle.Regular), Brushes.Blue, 0, 0);
            imgList.Images.Add(img);

            cmb.ImageList = imgList;
            cmb.Items.AddRange(new Object[] { "山", "川", "空" });
            this.Controls.Add(cmb);
        }

    }

    public class ExComboBox : ComboBox
    {
        private ImageList imgList = null;

        public ImageList ImageList
        {
            get
            {
                return imgList;
            }
            set
            {
                imgList = value;
            }
        }

        public ExComboBox()
        {
            this.DrawMode = DrawMode.OwnerDrawFixed;
        }

        /// <summary>
        /// リスト項目の描画
        /// </summary>
        /// <param name="e"></param>
        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            e.DrawBackground();
            e.DrawFocusRectangle();
            Rectangle bounds = new Rectangle();
            bounds = e.Bounds;
            string str = this.Items[e.Index].ToString();
            try
            {
                if (imgList != null)
                {
                    if (imgList.Images.Count > e.Index)
                    {
                        // imgListに設定された画像を描画
                        e.Graphics.DrawImage(imgList.Images[e.Index], bounds.Left, bounds.Top, bounds.Height, bounds.Height);
                        // itemsに設定された文字列を描画
                        e.Graphics.DrawString(str, e.Font, new SolidBrush(e.ForeColor), bounds.Left + bounds.Height, bounds.Top);
                    }
                    else
                    {
                        // itemsに設定された文字列を描画
                        e.Graphics.DrawString(str, e.Font, new SolidBrush(e.ForeColor), bounds.Left + bounds.Height, bounds.Top);
                    }
                }
                else
                {
                    // itemsに設定された文字列を描画
                    e.Graphics.DrawString(str, e.Font, new SolidBrush(e.ForeColor), bounds.Left, bounds.Top);
                }

            }
            catch
            {
                // itemsに設定された文字列を描画
                e.Graphics.DrawString(str, e.Font, new SolidBrush(e.ForeColor), bounds.Left, bounds.Top);
            }
        }
    }
}



最終更新:2011年06月22日 19:33
ツールボックス

下から選んでください:

新しいページを作成する
ヘルプ / FAQ もご覧ください。