LineStyleButtonItem.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Drawing2D;
  4. using System.Windows.Forms;
  5. using FastReport.DevComponents.DotNetBar;
  6. using FastReport.Utils;
  7. namespace FastReport.Controls
  8. {
  9. internal class LineStyleButtonItem : ButtonItem
  10. {
  11. private LineStyle lineStyle;
  12. private ItemContainer container;
  13. public event EventHandler StyleSelected;
  14. bool isOpen = false;
  15. public LineStyle LineStyle
  16. {
  17. get { return lineStyle; }
  18. set
  19. {
  20. lineStyle = value;
  21. foreach (BaseItem item in container.SubItems)
  22. {
  23. (item as ButtonItem).Checked = (LineStyle)(item as ButtonItem).Tag == value;
  24. }
  25. }
  26. }
  27. private void AddSubItem(LineStyle style)
  28. {
  29. ButtonItem item = new ButtonItem();
  30. Control owner = ContainerControl as Control;
  31. if (owner == null)
  32. return;
  33. Bitmap bmp = new Bitmap(owner.LogicalToDevice(88), owner.LogicalToDevice(14));
  34. using (Graphics g = Graphics.FromImage(bmp))
  35. {
  36. using (Pen pen = new Pen(Color.Black, owner.LogicalToDevice(2)))
  37. {
  38. DashStyle[] styles = new DashStyle[] {
  39. DashStyle.Solid, DashStyle.Dash, DashStyle.Dot, DashStyle.DashDot, DashStyle.DashDotDot, DashStyle.Solid };
  40. pen.DashStyle = styles[(int)style];
  41. if (style == LineStyle.Double)
  42. {
  43. pen.Width = owner.LogicalToDevice(5);
  44. pen.CompoundArray = new float[] { 0, 0.4f, 0.6f, 1 };
  45. }
  46. g.DrawLine(pen, owner.LogicalToDevice(4), owner.LogicalToDevice(7),
  47. owner.LogicalToDevice(84), owner.LogicalToDevice(7));
  48. }
  49. }
  50. item.Image = bmp;
  51. item.Tag = style;
  52. item.Checked = style == lineStyle;
  53. item.Click += item_Click;
  54. container.SubItems.Add(item);
  55. }
  56. private void item_Click(object sender, EventArgs e)
  57. {
  58. lineStyle = (LineStyle)(sender as ButtonItem).Tag;
  59. (sender as ButtonItem).Checked = true;
  60. if (StyleSelected != null)
  61. StyleSelected(this, EventArgs.Empty);
  62. }
  63. protected override void OnPopupClose(EventArgs e)
  64. {
  65. base.OnPopupClose(e);
  66. isOpen = false;
  67. }
  68. public override void InternalMouseDown(MouseEventArgs objArg)
  69. {
  70. isOpen = !isOpen;
  71. container.SubItems.Clear();
  72. UpdateItems();
  73. Expanded = isOpen;
  74. }
  75. private void UpdateItems()
  76. {
  77. AddSubItem(LineStyle.Solid);
  78. AddSubItem(LineStyle.Dash);
  79. AddSubItem(LineStyle.Dot);
  80. AddSubItem(LineStyle.DashDot);
  81. AddSubItem(LineStyle.DashDotDot);
  82. AddSubItem(LineStyle.Double);
  83. }
  84. public override void UpdateDpiDependencies()
  85. {
  86. base.UpdateDpiDependencies();
  87. container.SubItems.Clear();
  88. UpdateItems();
  89. }
  90. public LineStyleButtonItem()
  91. {
  92. container = new ItemContainer();
  93. container.LayoutOrientation = eOrientation.Vertical;
  94. SubItems.Add(container);
  95. PopupType = ePopupType.ToolBar;
  96. UpdateItems();
  97. }
  98. }
  99. }