첨부 소스 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
다운로드

▶ ComboBoxHelper.cs

using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Windows.Forms;

namespace TestProject
{
    /// <summary>
    /// 콤보 박스 헬퍼
    /// </summary>
    public static class ComboBoxHelper
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Instance
        //////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 색상 항목 마진 너비
        /// </summary>
        private const int COLOR_ITEM_MARGIN_WIDTH  = 4;

        /// <summary>
        /// 색상 항목 마진 높이
        /// </summary>
        private const int COLOR_ITEM_MARGIN_HEIGHT = 4;

        /// <summary>
        /// 이미지 항목 마진 너비
        /// </summary>
        private const int IMAGE_ITEM_MARGIN_WIDTH  = 2;

        /// <summary>
        /// 이미지 항목 마진 높이
        /// </summary>
        private const int IMAGE_ITEM_MARGIN_HEIGHT = 2;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Public
        ////////////////////////////////////////////////////////////////////// Function

        #region 색상 데이터 설정하기 - SetColorData(comboBox, colorArray)

        /// <summary>
        /// 색상 데이터 설정하기
        /// </summary>
        /// <param name="comboBox">콤보 박스</param>
        /// <param name="colorArray">색상 배열</param>
        public static void SetColorData(this ComboBox comboBox, Color[] colorArray)
        {
            comboBox.DrawMode = DrawMode.OwnerDrawFixed;

            comboBox.Items.Clear();

            foreach (Color color in colorArray)
            {
                comboBox.Items.Add(color);
            }

            comboBox.DrawItem += colorComboBox_DrawItem;
        }

        #endregion
        #region 이미지 데이터 설정하기 - SetImageData(comboBox, imageArray)

        /// <summary>
        /// 이미지 데이터 설정하기
        /// </summary>
        /// <param name="comboBox">콤보 박스</param>
        /// <param name="imageArray">이미지 배열</param>
        public static void SetImageData(this ComboBox comboBox, Image[] imageArray)
        {
            comboBox.DrawMode = DrawMode.OwnerDrawVariable;

            comboBox.Items.Clear();

            foreach(Image image in imageArray)
            {
                comboBox.Items.Add(image);
            }

            comboBox.MeasureItem += imageComboBox_MeasureItem;
            comboBox.DrawItem    += imageComboBox_DrawItem;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////// Private
        ////////////////////////////////////////////////////////////////////// Event

        #region 색상 콤보 박스 항목 그리기 - colorComboBox_DrawItem(sender, e)

        /// <summary>
        /// 색상 콤보 박스 항목 그리기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private static void colorComboBox_DrawItem(object sender, DrawItemEventArgs e)
        {
            if(e.Index < 0)
            {
                return;
            }

            e.DrawBackground();

            int height = e.Bounds.Height - 2 * COLOR_ITEM_MARGIN_HEIGHT;

            Rectangle rectangle = new Rectangle
            (
                e.Bounds.X + COLOR_ITEM_MARGIN_WIDTH,
                e.Bounds.Y + COLOR_ITEM_MARGIN_HEIGHT,
                height,
                height
            );

            ComboBox comboBox = sender as ComboBox;

            Color color = (Color)comboBox.Items[e.Index];

            using(SolidBrush brush = new SolidBrush(color))
            {
                e.Graphics.FillRectangle(brush, rectangle);
            }

            e.Graphics.DrawRectangle(Pens.Black, rectangle);

            using(Font font = new Font(comboBox.Font.FontFamily, comboBox.Font.Size * 0.75f, FontStyle.Bold))
            {
                using(StringFormat stringFormat = new StringFormat())
                {
                    stringFormat.Alignment     = StringAlignment.Near;
                    stringFormat.LineAlignment = StringAlignment.Center;

                    int x = height + 2 * COLOR_ITEM_MARGIN_WIDTH;
                    int y = e.Bounds.Y + e.Bounds.Height / 2;

                    e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;

                    e.Graphics.DrawString(color.Name, font, Brushes.Black, x, y, stringFormat);
                }
            }

            e.DrawFocusRectangle();
        }

        #endregion
        #region 이미지 콤보 박스 항목 측정하기 - imageComboBox_MeasureItem(sender, e)

        /// <summary>
        /// 이미지 콤보 박스 항목 측정하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private static void imageComboBox_MeasureItem(object sender, MeasureItemEventArgs e)
        {
            if(e.Index < 0)
            {
                return;
            }

            ComboBox comboBox = sender as ComboBox;

            Image image = (Image)comboBox.Items[e.Index];

            e.ItemHeight = image.Height + 2 * IMAGE_ITEM_MARGIN_HEIGHT;
            e.ItemWidth  = image.Width  + 2 * IMAGE_ITEM_MARGIN_WIDTH;
        }

        #endregion
        #region 이미지 콤보 박스 항목 그리기 - imageComboBox_DrawItem(sender, e)

        /// <summary>
        /// 이미지 콤보 박스 항목 그리기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private static void imageComboBox_DrawItem(object sender, DrawItemEventArgs e)
        {
            if(e.Index < 0)
            {
                return;
            }

            e.DrawBackground();

            ComboBox comboBox = sender as ComboBox;

            Image image = (Image)comboBox.Items[e.Index];

            float height = e.Bounds.Height - 2 * IMAGE_ITEM_MARGIN_HEIGHT;
            float scale  = height / image.Height;
            float width  = image.Width * scale;

            RectangleF rectangle = new RectangleF
            (
                e.Bounds.X + IMAGE_ITEM_MARGIN_WIDTH,
                e.Bounds.Y + IMAGE_ITEM_MARGIN_HEIGHT,
                width,
                height
            );

            e.Graphics.InterpolationMode = InterpolationMode.HighQualityBilinear;

            e.Graphics.DrawImage(image, rectangle);

            e.DrawFocusRectangle();
        }

        #endregion
    }
}

 

728x90

 

▶ MainForm.cs

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

namespace TestProject
{
    /// <summary>
    /// 메인 폼
    /// </summary>
    public partial class MainForm : Form
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainForm()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainForm()
        {
            InitializeComponent();

            #region 이벤트를 설정한다.

            Load += Form_Load;

            #endregion
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private
        //////////////////////////////////////////////////////////////////////////////// Event

        #region 폼 로드시 처리하기 - Form_Load(sender, e)

        /// <summary>
        /// 폼 로드시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void Form_Load(object sender, EventArgs e)
        {
            Color[] colorArray =
            {
                Color.Red,
                Color.Orange,
                Color.Yellow,
                Color.Green,
                Color.Blue,
                Color.Indigo,
                Color.Purple,
            };

            this.colorComboBox.SetColorData(colorArray);

            this.colorComboBox.SelectedIndex = 0;

            Image[] imageArray = 
            {
                Properties.Resources.face1,
                Properties.Resources.face2,
                Properties.Resources.face3,
                Properties.Resources.face4,
            };

            this.imageComboBox.SetImageData(imageArray);

            this.imageComboBox.SelectedIndex = 0;

            this.imageComboBox.DropDownHeight = 200;
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요