■ UserControl 클래스를 사용해 가상 리스트 박스를 만드는 방법을 보여준다.
▶ VirtualListBox.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 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 |
using System; using System.Collections.ObjectModel; using System.Collections.Specialized; using System.ComponentModel; using System.Diagnostics; using System.Drawing; using System.Windows.Forms; namespace TestProject { /// <summary> /// 가상 리스트 박스 /// </summary> public class VirtualListBox : UserControl { //////////////////////////////////////////////////////////////////////////////////////////////////// Event ////////////////////////////////////////////////////////////////////////////////////////// Public #region 마지막 렌더링 시간 변경시 - LastRenderTimeChanged /// <summary> /// 마지막 렌더링 시간 변경시 /// </summary> public event EventHandler LastRenderTimeChanged; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 현재 항목 인덱스 /// </summary> private int currentItemIndex; /// <summary> /// 버퍼 그래픽스 /// </summary> private BufferedGraphics bufferedGraphics; /// <summary> /// 버퍼 그래픽스 컨텍스트 /// </summary> private BufferedGraphicsContext bufferedGraphicsContext; /// <summary> /// 항목 컬렉션 /// </summary> private ObservableCollection<string> itemCollection; /// <summary> /// 항목 높이 /// </summary> private int itemHeight; /// <summary> /// 마지막 렌더링 시간 /// </summary> private long lastRenderTime; /// <summary> /// 페인팅 여부 /// </summary> private volatile bool painting; /// <summary> /// 보류 여부 /// </summary> private volatile bool suspended; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 항목 컬렉션 - ItemCollection /// <summary> /// 항목 컬렉션 /// </summary> [Browsable(false)] public ObservableCollection<string> ItemCollection { get { return this.itemCollection; } } #endregion #region 항목 높이 - ItemHeight /// <summary> /// 항목 높이 /// </summary> [Category("VirtualListBox")] [Browsable(true)] [DefaultValue(18)] [Description("목록에서 항목 높이 입니다.")] public int ItemHeight { get { return this.itemHeight; } set { if(this.itemHeight != value) { this.itemHeight = value; InvalidateBuffer(); } } } #endregion #region 마지막 렌더링 시간 - LastRenderTime /// <summary> /// 마지막 렌더링 시간 /// </summary> [Browsable(false)] public long LastRenderTime { get { return this.lastRenderTime; } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - VirtualListBox() /// <summary> /// 생성자 /// </summary> public VirtualListBox() { DoubleBuffered = true; SetStyle ( ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque | ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint, true ); this.currentItemIndex = -1; this.bufferedGraphics = null; this.bufferedGraphicsContext = BufferedGraphicsManager.Current; this.itemCollection = new ObservableCollection<string>(); this.itemHeight = 18; this.lastRenderTime = 0; this.painting = false; this.suspended = false; this.itemCollection.CollectionChanged += itemCollection_CollectionChanged; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 업데이트 시작하기 - BeginUpdate() /// <summary> /// 업데이트 시작하기 /// </summary> public void BeginUpdate() { this.suspended = true; } #endregion #region 업데이트 종료하기 - EndUpdate() /// <summary> /// 업데이트 종료하기 /// </summary> public void EndUpdate() { this.suspended = false; InvalidateBuffer(); } #endregion #region 항목 인덱스 구하기 - GetItemIndex(point) /// <summary> /// 항목 인덱스 구하기 /// </summary> /// <param name="point">위치</param> /// <returns>항목 인덱스</returns> public int GetItemIndex(Point point) { int start = itemHeight > 0 ? (int)Math.Floor((double)Math.Abs(AutoScrollPosition.Y) / itemHeight) : 0; int visibleItemCount = (int)Math.Ceiling((double)Height / (double)itemHeight); Rectangle boundRectangle = new Rectangle(0, -(int)(Math.Abs(AutoScrollPosition.Y) % itemHeight), Width, itemHeight); for(int i = 0; 0 <= start + i && start + i < itemCollection.Count && visibleItemCount > i; i++) { if(boundRectangle.Contains(point)) { return start + i; } boundRectangle.Y += itemHeight; } return -1; } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 크기 조정시 처리하기 - OnResize(e) /// <summary> /// 크기 조정시 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnResize(EventArgs e) { base.OnResize(e); InvalidateBuffer(); } #endregion #region 마우스 이동시 처리하기 - OnMouseMove(e) /// <summary> /// 마우스 이동시 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnMouseMove(MouseEventArgs e) { base.OnMouseMove(e); int index = GetItemIndex(e.Location); if(index != currentItemIndex) { currentItemIndex = index; InvalidateBuffer(); } } #endregion #region 마우스 휠 처리하기 - OnMouseWheel(e) /// <summary> /// 마우스 휠 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnMouseWheel(MouseEventArgs e) { base.OnMouseWheel(e); InvalidateBuffer(); } #endregion #region 스크롤시 처리하기 - OnScroll(e) /// <summary> /// 스크롤시 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnScroll(ScrollEventArgs e) { base.OnScroll(e); InvalidateBuffer(); } #endregion #region 페인트시 처리하기 - OnPaint(e) /// <summary> /// 페인트시 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnPaint(PaintEventArgs e) { Stopwatch stopwatch = Stopwatch.StartNew(); if(this.bufferedGraphics == null) { Rectangle boundRectangle = new Rectangle(0, 0, Width, Height); this.bufferedGraphics = this.bufferedGraphicsContext.Allocate(e.Graphics, boundRectangle); using(Brush brush = new SolidBrush(BackColor)) { this.bufferedGraphics.Graphics.FillRectangle(brush, boundRectangle); brush.Dispose(); } if(itemCollection.Count > 0 && itemHeight > 0) { int start = 0; if(itemHeight > 0) { start = (int)Math.Floor((double)Math.Abs(AutoScrollPosition.Y) / itemHeight); } StringFormat stringFormat = new StringFormat() { Alignment = StringAlignment.Near, LineAlignment = StringAlignment.Center }; int visibleItemCount = (int)Math.Ceiling((double)Height / (double)itemHeight); int offset = -(int)(Math.Abs(AutoScrollPosition.Y) % itemHeight); for(int i = 0; visibleItemCount > i && itemCollection.Count > start + i && 0 <= start + i; i++) { Brush foregroundBrush = (currentItemIndex == start + i) ? Brushes.White : Brushes.Black; Rectangle itemBoundRectangle = new Rectangle(0, i * itemHeight + offset, Width, itemHeight); if(currentItemIndex == start + i) { using(Brush brush = new SolidBrush(Color.FromArgb(50, 142, 254))) { bufferedGraphics.Graphics.FillRectangle(brush, itemBoundRectangle); brush.Dispose(); } } itemBoundRectangle.X += 10; itemBoundRectangle.Width -= 20; bufferedGraphics.Graphics.DrawString ( itemCollection[start + i], Font, foregroundBrush, itemBoundRectangle, stringFormat ); } } } this.bufferedGraphics.Render(e.Graphics); stopwatch.Stop(); this.lastRenderTime = stopwatch.ElapsedMilliseconds; if(LastRenderTimeChanged != null) { LastRenderTimeChanged.Invoke(this, EventArgs.Empty); } } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private #region 항목 컬렉션 컬렉션 변경시 처리하기 - itemCollection_CollectionChanged(sender, e) /// <summary> /// 항목 컬렉션 컬렉션 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void itemCollection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { InvalidateBuffer(); } #endregion #region 버퍼 무효화 하기 - InvalidateBuffer() /// <summary> /// 버퍼 무효화 하기 /// </summary> private void InvalidateBuffer() { if(this.painting || this.suspended) { return; } this.painting = true; if(this.bufferedGraphics != null) { this.bufferedGraphics.Dispose(); this.bufferedGraphics = null; } AutoScrollMinSize = new Size(0, ItemHeight * itemCollection.Count); Invalidate(); this.painting = false; } #endregion } } |
▶ 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 |
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 가상 리스트 박스를 설정한다. this.virtualListBox.BackColor = Color.White; this.virtualListBox.LastRenderTimeChanged += (s, e) => this.timeLabel.Invoke ( (MethodInvoker)delegate { this.timeLabel.Text = "렌더링 시간 : " + this.virtualListBox.LastRenderTime + "밀리초"; } ); this.virtualListBox.BeginUpdate(); for(int i = 0; 1000000 > i; i++) { this.virtualListBox.ItemCollection.Add(string.Format("가상 리스트 박스 항목 {0}", i + 1)); } this.virtualListBox.EndUpdate(); #endregion this.addButton.Click += addButton_Click; this.itemHeightNumericUpDown.ValueChanged += itemHeightNumericUpDown_ValueChanged; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 추가 버튼 클릭시 처리하기 - addButton_Click(sender, e) /// <summary> /// 추가 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void addButton_Click(object sender, EventArgs e) { Random random = new Random(); this.virtualListBox.BeginUpdate(); for(int i = 0; 100000 > i; i++) { this.virtualListBox.ItemCollection.Add(string.Format("추가 가상 리스트 박스 항목 {0}", random.Next(10000000, 99999999))); } this.virtualListBox.EndUpdate(); } #endregion #region 항목 높이 숫자 UP/DOWN 값 변경시 처리하기 - itemHeightNumericUpDown_ValueChanged(sender, e) /// <summary> /// 항목 높이 숫자 UP/DOWN 값 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void itemHeightNumericUpDown_ValueChanged(object sender, EventArgs e) { this.virtualListBox.ItemHeight = (int)this.itemHeightNumericUpDown.Value; } #endregion } } |