첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
본 블로그는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 블로그 콘텐츠 향상을 위해 쓰여집니다.

728x90
반응형
728x170

TestProject.zip
다운로드

▶ BrushListBox.cs

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

namespace TestProject
{
    /// <summary>
    /// 브러시 리스트 박스
    /// </summary>
    public class BrushListBox : ListBox
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 브러시 타입
        /// </summary>
        private BrushType brushType;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Property
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 브러시 타입 - BrushType

        /// <summary>
        /// 브러시 타입
        /// </summary>
        public BrushType BrushType
        {
            get
            {
                return this.brushType;
            }
            set 
            {
                this.brushType = value;

                Initialize();
            }
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - BrushListBox()

        /// <summary>
        /// 생성자
        /// </summary>
        public BrushListBox()
        {
            DrawMode  = DrawMode.OwnerDrawFixed;
            BrushType = BrushType.HATCH;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Protected

        #region 항목 그리기시 처리하기 - OnDrawItem(e)

        /// <summary>
        /// 항목 그리기시 처리하기
        /// </summary>
        /// <param name="e">이벤트 인자</param>
        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            Graphics graphics = e.Graphics;

            e.DrawBackground();

            if(e.Index >= 0 && e.Index < Items.Count)
            {
                IBrushItem brushItem = (IBrushItem)Items[e.Index];
                Brush      brush     = brushItem.Brush;

                graphics.FillRectangle(brush, e.Bounds);

                graphics.DrawLine(Pens.Black, e.Bounds.X, e.Bounds.Bottom - 1, e.Bounds.Right, e.Bounds.Bottom - 1);

                string brushItemName = brushItem.Name;
                int    width         = (int)graphics.MeasureString(brushItemName, Font).Width;

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

                graphics.DrawRectangle(Pens.Black, 3, e.Bounds.Top + 3, width + 3, Font.Height + 5);

                graphics.DrawString(brushItem.Name, Font, Brushes.Black, 5, e.Bounds.Top + 5);

                brush.Dispose();
            }
        }

        #endregion

        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region 초기화하기 - Initialize()

        /// <summary>
        /// 초기화하기
        /// </summary>
        private void Initialize()
        {
            Items.Clear();

            if(this.brushType == BrushType.HATCH)
            {
                foreach(HatchStyle hatchStyle in Enum.GetValues(typeof(HatchStyle)))
                {
                    IBrushItem brushItem = new HatchBrushItem(hatchStyle, Color.Blue, Color.Transparent);

                    Items.Add(brushItem);
                }
            }
            else if(this.brushType == BrushType.SOLID_COLOR)
            {
                foreach(KnownColor knownColor in Enum.GetValues(typeof(KnownColor)))
                {
                    Color color = Color.FromKnownColor(knownColor);

                    if(!color.IsSystemColor)
                    {
                        IBrushItem brushItem = new ColorBrushItem(color);

                        Items.Add(brushItem);
                    }
                }
            }
            else if(this.brushType == BrushType.SYSTEM_COLOR)
            {
                foreach(KnownColor knownColor in Enum.GetValues(typeof(KnownColor)))
                {
                    Color color = Color.FromKnownColor(knownColor);

                    if(color.IsSystemColor)
                    {
                        IBrushItem brushItem = new ColorBrushItem(color);

                        Items.Add(brushItem);
                    }
                }
            }
        }

        #endregion
    }
}
728x90
반응형
그리드형
Posted by 사용자 icodebroker

댓글을 달아 주세요