ToolStripLineWidthButton.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using FastReport.Utils;
  2. using System;
  3. using System.Drawing;
  4. using System.Windows.Forms;
  5. namespace FastReport.Controls
  6. {
  7. internal class ToolStripLineWidthButton : ToolStripDropDownButton
  8. {
  9. private Control owner; // not used in Mono/WPF
  10. private LineWidthControl selector;
  11. public event EventHandler WidthSelected;
  12. public float LineWidth
  13. {
  14. get => selector.LineWidth;
  15. set => selector.LineWidth = value;
  16. }
  17. private void selector_ItemSelected(object sender, EventArgs e)
  18. {
  19. DropDown.Close();
  20. if (WidthSelected != null)
  21. WidthSelected(this, EventArgs.Empty);
  22. }
  23. private void UpdateDpiDependencies()
  24. {
  25. selector.Size = selector.LogicalToDevice(new Size(100, 166));
  26. selector.Font = selector.LogicalToDevice(DrawUtils.DefaultFont);
  27. }
  28. public ToolStripLineWidthButton(Control owner)
  29. {
  30. this.owner = owner;
  31. DisplayStyle = ToolStripItemDisplayStyle.Image;
  32. ImageAlign = ContentAlignment.MiddleLeft;
  33. if (!Config.IsRunningOnMono)
  34. {
  35. // better looking in Mono/Win. WPF will override this size
  36. AutoSize = false;
  37. Size = new Size(32, 22);
  38. }
  39. selector = new LineWidthControl(owner);
  40. selector.ItemSelected += selector_ItemSelected;
  41. DropDown = new FRToolStripDropDown(selector);
  42. DropDown.Opening += (s, e) => UpdateDpiDependencies();
  43. }
  44. private class LineWidthControl : ItemSelector
  45. {
  46. private float[] stdWidths;
  47. public float LineWidth
  48. {
  49. get => stdWidths[SelectedIndex];
  50. set
  51. {
  52. for (int i = 0; i < stdWidths.Length; i++)
  53. {
  54. if (Math.Abs(value - stdWidths[i]) < 1e-4)
  55. {
  56. SelectedIndex = i;
  57. break;
  58. }
  59. }
  60. }
  61. }
  62. protected override void DrawItem(Graphics g, Rectangle rect, int index)
  63. {
  64. using (Pen pen = new Pen(Color.Black, Owner.LogicalToDevice(stdWidths[index])))
  65. {
  66. int _3 = Owner.LogicalToDevice(3);
  67. int _6 = Owner.LogicalToDevice(6);
  68. int lineWidth = Owner.LogicalToDevice(60) + (index >= 4 ? 1 : 0);
  69. g.DrawLine(pen, rect.Left + _3, rect.Top + rect.Height / 2, rect.Left + lineWidth, rect.Top + rect.Height / 2);
  70. TextRenderer.DrawText(g, stdWidths[index].ToString(), Font, new Point(rect.Left + lineWidth + _6, rect.Top + _3), SystemColors.WindowText);
  71. }
  72. }
  73. public LineWidthControl(Control owner) : base(owner)
  74. {
  75. stdWidths = new float[] { 0.25f, 0.5f, 1, 1.5f, 2, 3, 4, 5 };
  76. ItemsCount = stdWidths.Length;
  77. ItemHeight = 20;
  78. }
  79. }
  80. }
  81. }