■ ListView 클래스에서 커스텀 아이콘을 사용하는 방법을 보여준다.
▶ MainForm.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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D; 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; this.viewComboBox.SelectedIndexChanged += viewComboBox_SelectedIndexChanged; #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) { this.viewComboBox.SelectedIndex = 0; this.listView.SmallImageList = this.smallImageList; this.listView.LargeImageList = this.largeImageList; this.listView.MakeColumnHeaders ( "제목" , 230, HorizontalAlignment.Left, "URL" , 220, HorizontalAlignment.Left, "ISBN" , 130, HorizontalAlignment.Left, "이미지", 230, HorizontalAlignment.Left, "페이지", 50 , HorizontalAlignment.Right, "연도" , 60 , HorizontalAlignment.Right ); RectangleF smallRectangle = new RectangleF(0, 0, 32, 32); RectangleF largeRectangle = new RectangleF(3, 3, 58, 58); this.smallImageList.Images.Clear(); this.largeImageList.Images.Clear(); using(Pen smallPen = new Pen(Color.Blue, 2)) { using(Pen largePen = new Pen(Color.Blue, 3)) { largePen.LineJoin = LineJoin.Round; for(int i = 1; i <= 7; i++) { Bitmap smallBitmap = new Bitmap(32, 32); using(Graphics smallGraphics = Graphics.FromImage(smallBitmap)) { smallGraphics.SmoothingMode = SmoothingMode.AntiAlias; smallGraphics.Clear(Color.Transparent); DrawStar(smallGraphics, smallRectangle, i + 3, smallPen, Brushes.Yellow); this.smallImageList.Images.Add(i.ToString(), smallBitmap); } Bitmap largeBitmap = new Bitmap(64, 64); using(Graphics largeGraphics = Graphics.FromImage(largeBitmap)) { largeGraphics.Clear(Color.Transparent); largeGraphics.SmoothingMode = SmoothingMode.AntiAlias; DrawStar(largeGraphics, largeRectangle, i + 3, largePen, Brushes.Yellow); this.largeImageList.Images.Add(i.ToString(), largeBitmap); } } } } this.listView.AddRow ( "1", "WPF 3d", "http://www.csharphelper.com/wpf3d.html", "978-1983905964", "http://www.csharphelper.com/wpf3d_350_429.jpg", "430", "2018" ); this.listView.AddRow ( "2", "The C# Helper Top 100", "http://www.csharphelper.com/top100.htm", "978-1546886716", "http://www.csharphelper.com/top100_350_433.jpg", "380", "2017" ); this.listView.AddRow ( "3", "Interview Puzzles Dissected", "http://www.csharphelper.com/puzzles.htm", "978-1539504887", "http://www.csharphelper.com/interview_puzzles_350_433.jpg", "300", "2016" ); this.listView.AddRow ( "4", "C# 24-Hour Trainer, 2e", "http://tinyurl.com/n2a5797", "978-1-119-06566-1", "http://www.csharphelper.com/csharp24hr_2e_79x100.jpg", "600", "2015" ); this.listView.AddRow ( "5", "Beginning Software Engineering", "http://tinyurl.com/pz7bavo", "978-1-118-96914-4", "http://tinyurl.com/y7zusrct", "480", "2015" ); this.listView.AddRow ( "6", "Essential Algorithms", "http://tinyurl.com/y9uqajqv", "978-1-118-61210-1", "http://tinyurl.com/y84d2jgp", "624", "2013" ); this.listView.AddRow ( "7", "Beginning Database Design Solutions", "http://www.vb-helper.com/db_design.htm", "978-0-470-38549-4", "http://www.vb-helper.com/db_design.jpg", "522", "2008" ); } #endregion #region 뷰 콤보 박스 선택 인덱스 변경시 처리하기 - viewComboBox_SelectedIndexChanged(sender, e) /// <summary> /// 뷰 콤보 박스 선택 인덱스 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void viewComboBox_SelectedIndexChanged(object sender, EventArgs e) { switch(this.viewComboBox.Text) { case "Large Icons" : this.listView.View = View.LargeIcon; break; case "Small Icons" : this.listView.View = View.SmallIcon; break; case "List" : this.listView.View = View.List; break; case "Tile" : this.listView.View = View.Tile; break; case "Details" : this.listView.View = View.Details; break; } } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 별 그리기 - DrawStar(graphics, rectangle, pointCount, pen, brush) /// <summary> /// 별 그리기 /// </summary> /// <param name="graphics">그래픽스</param> /// <param name="rectangle">사각형</param> /// <param name="pointCount">포인트 카운트</param> /// <param name="pen">펜</param> /// <param name="brush">브러시</param> private void DrawStar(Graphics graphics, RectangleF rectangle, int pointCount, Pen pen, Brush brush) { float cx = (rectangle.Left + rectangle.Right ) / 2f; float cy = (rectangle.Top + rectangle.Bottom) / 2f; float rx1 = rectangle.Width / 2f - 1; float ry1 = rectangle.Height / 2f - 1; float rx2 = rx1 / 3f; float ry2 = ry1 / 3f; double theta = -Math.PI / 2; double dtheta = Math.PI / pointCount; List<PointF> pointList = new List<PointF>(); for(int i = 0; i < pointCount; i++) { pointList.Add ( new PointF ( (float)(cx + rx1 * Math.Cos(theta)), (float)(cy + ry1 * Math.Sin(theta)) ) ); theta += dtheta; pointList.Add ( new PointF ( (float)(cx + rx2 * Math.Cos(theta)), (float)(cy + ry2 * Math.Sin(theta)) ) ); theta += dtheta; } PointF[] pointArray = pointList.ToArray(); graphics.FillPolygon(brush, pointArray); graphics.DrawPolygon(pen, pointArray); } #endregion } } |