Newer
Older
TestStandRepository / Software / Arduino / libraries / Arduino-Libraries / CmdMessenger / CSharp / TemperatureControl / LoggingView.cs
  1. using System.Drawing;
  2. using System.Windows.Forms;
  3.  
  4. namespace Tools
  5. {
  6. public partial class LoggingView : ListBox
  7. {
  8. public LoggingView()
  9. {
  10. InitializeComponent();
  11. SetStyle(
  12. ControlStyles.OptimizedDoubleBuffer |
  13. ControlStyles.ResizeRedraw |
  14. ControlStyles.UserPaint,
  15. true);
  16. DrawMode = DrawMode.OwnerDrawFixed;
  17. FollowLastItem = true;
  18. MaxEntriesInListBox = 3000;
  19. }
  20.  
  21. public bool FollowLastItem{ get; set; }
  22. public int MaxEntriesInListBox { get; set; }
  23.  
  24.  
  25. public void AddEntry(object item)
  26. {
  27. BeginUpdate();
  28. Items.Add(item);
  29.  
  30. if (Items.Count > MaxEntriesInListBox)
  31. {
  32. Items.RemoveAt(0);
  33. }
  34.  
  35. if (FollowLastItem) TopIndex = Items.Count - 1;
  36. EndUpdate();
  37. }
  38.  
  39. protected override void OnDrawItem(DrawItemEventArgs e)
  40. {
  41. if (Items.Count > 0)
  42. {
  43. e.DrawBackground();
  44. e.Graphics.DrawString(Items[e.Index].ToString(), e.Font, new SolidBrush(ForeColor), new PointF(e.Bounds.X, e.Bounds.Y));
  45. }
  46. base.OnDrawItem(e);
  47. }
  48. protected override void OnPaint(PaintEventArgs e)
  49. {
  50. var iRegion = new Region(e.ClipRectangle);
  51. e.Graphics.FillRegion(new SolidBrush(BackColor), iRegion);
  52. if (Items.Count > 0)
  53. {
  54. for (int i = 0; i < Items.Count; ++i)
  55. {
  56. var irect = GetItemRectangle(i);
  57. if (e.ClipRectangle.IntersectsWith(irect))
  58. {
  59. if ((SelectionMode == SelectionMode.One && SelectedIndex == i)
  60. || (SelectionMode == SelectionMode.MultiSimple && SelectedIndices.Contains(i))
  61. || (SelectionMode == SelectionMode.MultiExtended && SelectedIndices.Contains(i)))
  62. {
  63. OnDrawItem(new DrawItemEventArgs(e.Graphics, Font,
  64. irect, i,
  65. DrawItemState.Selected, ForeColor,
  66. BackColor));
  67. }
  68. else
  69. {
  70. OnDrawItem(new DrawItemEventArgs(e.Graphics, Font,
  71. irect, i,
  72. DrawItemState.Default, ForeColor,
  73. BackColor));
  74. }
  75. iRegion.Complement(irect);
  76. }
  77. }
  78. }
  79. base.OnPaint(e);
  80. }
  81. }
  82. }