You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
2.9 KiB

  1. #if NETFX || NETCORE
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.Text;
  6. using System.Windows.Forms;
  7. namespace Apewer.Surface
  8. {
  9. /// <summary></summary>
  10. internal class BlockList : ListBox
  11. {
  12. private List<Color> _color = new List<Color>();
  13. /// <summary></summary>
  14. public BlockList()
  15. {
  16. Font = FormsUtility.DefaultFont;
  17. BorderStyle = BorderStyle.None;
  18. DrawMode = DrawMode.OwnerDrawVariable;
  19. SelectionMode = SelectionMode.One;
  20. MeasureItem += Event_MeasureItem;
  21. DrawItem += Event_DrawItem;
  22. }
  23. /// <summary></summary>
  24. private void Event_DrawItem(object sender, DrawItemEventArgs e)
  25. {
  26. if (_color.Count > 0)
  27. {
  28. RectangleF vrf = new RectangleF(8, e.Bounds.Top + 4, e.Bounds.Width - 16, 16);
  29. Brush vb = new SolidBrush(GetColor(e.Index));
  30. string vs = Items[e.Index].ToString();
  31. e.DrawBackground();
  32. e.DrawFocusRectangle();
  33. e.Graphics.DrawString(vs, e.Font, vb, vrf);
  34. }
  35. }
  36. /// <summary></summary>
  37. private void Event_MeasureItem(object sender, MeasureItemEventArgs e)
  38. {
  39. e.ItemHeight = 25;
  40. }
  41. /// <summary></summary>
  42. private Color GetColor(int index)
  43. {
  44. if (index >= _color.Count) return ForeColor;
  45. return _color[index];
  46. }
  47. /// <summary></summary>
  48. public void Append(string text) => Write(text, ForeColor);
  49. /// <summary></summary>
  50. public void Write(string text) => Write(text, ForeColor);
  51. /// <summary></summary>
  52. public void Black(string text) => Write(text, FormsUtility.Black);
  53. /// <summary></summary>
  54. public void Green(string text) => Write(text, FormsUtility.Green);
  55. /// <summary></summary>
  56. public void Red(string text) => Write(text, FormsUtility.Red);
  57. /// <summary></summary>
  58. public void Gray(string text) => Write(text, FormsUtility.GraceLocked);
  59. /// <summary></summary>
  60. public void Error(string text) => Red(text);
  61. /// <summary></summary>
  62. public void Write(string text, Color color)
  63. {
  64. var t = string.IsNullOrEmpty(text) ? "" : text.Trim();
  65. while (_color.Count < Items.Count)
  66. {
  67. _color.Add(ForeColor);
  68. }
  69. while (_color.Count > Items.Count)
  70. {
  71. _color.RemoveAt(_color.Count - 1);
  72. }
  73. _color.Add((color == null) ? FormsUtility.Black : color);
  74. Items.Add(t);
  75. }
  76. /// <summary></summary>
  77. public void SelectLast()
  78. {
  79. if (Items.Count > 0) SelectedIndex = Items.Count - 1;
  80. }
  81. }
  82. }
  83. #endif