■ ListBox 클래스의 OnDrawItem 메소드를 사용해 리스트 박스 항목을 그리는 방법을 보여준다.
▶ BrushListBox.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
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 } } |