StyleListBox.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System;
  2. using System.Windows.Forms;
  3. using System.Drawing;
  4. using System.ComponentModel;
  5. using FastReport.Utils;
  6. namespace FastReport.Controls
  7. {
  8. #if !DEBUG
  9. [DesignTimeVisible(false)]
  10. #endif
  11. internal class StyleListBox : ListBox
  12. {
  13. private bool updating;
  14. private StyleCollection styles;
  15. public event EventHandler StyleSelected;
  16. public string Style
  17. {
  18. get
  19. {
  20. if (SelectedIndex < 1)
  21. return "";
  22. return (string)Items[SelectedIndex];
  23. }
  24. set
  25. {
  26. updating = true;
  27. int i = Items.IndexOf(value);
  28. SelectedIndex = i != -1 ? i : 0;
  29. updating = false;
  30. }
  31. }
  32. public StyleCollection Styles
  33. {
  34. get { return styles; }
  35. set
  36. {
  37. styles = value;
  38. if (value != null)
  39. UpdateItems();
  40. }
  41. }
  42. protected override void OnDrawItem(DrawItemEventArgs e)
  43. {
  44. e.DrawBackground();
  45. Graphics g = e.Graphics;
  46. if (e.Index >= 0)
  47. {
  48. string name = (string)Items[e.Index];
  49. using (TextObject sample = new TextObject())
  50. {
  51. sample.Bounds = new RectangleF(e.Bounds.Left + 2, e.Bounds.Top + 2, e.Bounds.Width - 4, e.Bounds.Height - 4);
  52. sample.Text = name;
  53. sample.HorzAlign = HorzAlign.Center;
  54. sample.VertAlign = VertAlign.Center;
  55. if (styles != null)
  56. {
  57. int index = styles.IndexOf(name);
  58. if (index != -1)
  59. sample.ApplyStyle(styles[index]);
  60. }
  61. using (GraphicCache cache = new GraphicCache())
  62. {
  63. sample.Draw(new FRPaintEventArgs(g, 1, 1, cache));
  64. }
  65. }
  66. }
  67. }
  68. protected override void OnSelectedIndexChanged(EventArgs e)
  69. {
  70. base.OnSelectedIndexChanged(e);
  71. if (updating)
  72. return;
  73. if (StyleSelected != null)
  74. StyleSelected(this, EventArgs.Empty);
  75. }
  76. private void UpdateItems()
  77. {
  78. Items.Clear();
  79. Items.Add(Res.Get("Designer,Toolbar,Style,NoStyle"));
  80. foreach (Style s in styles)
  81. {
  82. Items.Add(s.Name);
  83. }
  84. }
  85. public StyleListBox()
  86. {
  87. DrawMode = DrawMode.OwnerDrawFixed;
  88. ItemHeight = 32;
  89. IntegralHeight = false;
  90. Size = new Size(150, 300);
  91. }
  92. }
  93. }