ItemSelector.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using FastReport.Utils;
  2. using System;
  3. using System.Drawing;
  4. using System.Windows.Forms;
  5. namespace FastReport.Controls
  6. {
  7. internal class ItemSelector : Control
  8. {
  9. public int ItemsCount { get; set; }
  10. public int ItemHeight { get; set; }
  11. public int SelectedIndex { get; set; }
  12. public Control Owner { get; }
  13. public event EventHandler ItemSelected;
  14. private void DrawHighlight(Graphics g, Rectangle rect)
  15. {
  16. using (Brush brush = new SolidBrush(Color.FromArgb(193, 210, 238)))
  17. using (Pen pen = new Pen(Color.FromArgb(49, 106, 197)))
  18. {
  19. g.FillRectangle(brush, rect);
  20. g.DrawRectangle(pen, rect);
  21. }
  22. }
  23. protected virtual void DrawItem(Graphics g, Rectangle rect, int index)
  24. {
  25. }
  26. protected override void OnPaint(PaintEventArgs e)
  27. {
  28. Graphics g = e.Graphics;
  29. int _3 = Owner.LogicalToDevice(3);
  30. int _6 = Owner.LogicalToDevice(6);
  31. int h = Owner.LogicalToDevice(ItemHeight);
  32. for (int i = 0; i < ItemsCount; i++)
  33. {
  34. Rectangle rect = new Rectangle(_3, i * h + _3, Width - _6, h);
  35. if (i == SelectedIndex)
  36. DrawHighlight(g, rect);
  37. DrawItem(g, rect, i);
  38. }
  39. }
  40. protected override void OnMouseMove(MouseEventArgs e)
  41. {
  42. SelectedIndex = e.Y / Owner.LogicalToDevice(ItemHeight);
  43. Refresh();
  44. }
  45. protected override void OnMouseUp(MouseEventArgs e)
  46. {
  47. if (ItemSelected != null)
  48. ItemSelected(this, EventArgs.Empty);
  49. }
  50. public ItemSelector(Control owner)
  51. {
  52. Owner = owner;
  53. BackColor = Color.White;
  54. SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true);
  55. }
  56. }
  57. }