LineStyleControl.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using FastReport.Utils;
  2. using System;
  3. using System.Drawing;
  4. using System.Drawing.Drawing2D;
  5. using System.Windows.Forms;
  6. namespace FastReport.Controls
  7. {
  8. internal class LineStyleControl : Control
  9. {
  10. private LineStyle[] styles;
  11. private LineStyle style;
  12. private float lineWidth;
  13. private Color lineColor;
  14. private bool showBorder;
  15. public event EventHandler StyleSelected;
  16. public LineStyle Style
  17. {
  18. get { return style; }
  19. set
  20. {
  21. style = value;
  22. Refresh();
  23. }
  24. }
  25. public float LineWidth
  26. {
  27. get { return lineWidth; }
  28. set
  29. {
  30. lineWidth = value;
  31. Refresh();
  32. }
  33. }
  34. public Color LineColor
  35. {
  36. get { return lineColor; }
  37. set
  38. {
  39. lineColor = value;
  40. Refresh();
  41. }
  42. }
  43. public bool ShowBorder
  44. {
  45. get { return showBorder; }
  46. set
  47. {
  48. showBorder = value;
  49. Refresh();
  50. }
  51. }
  52. private void DrawHighlight(Graphics g, Rectangle rect)
  53. {
  54. using (Brush brush = new SolidBrush(Color.FromArgb(193, 210, 238)))
  55. g.FillRectangle(brush, rect);
  56. using (Pen pen = new Pen(Color.FromArgb(49, 106, 197)))
  57. g.DrawRectangle(pen, rect);
  58. }
  59. protected override void OnPaint(PaintEventArgs e)
  60. {
  61. Graphics g = e.Graphics;
  62. // draw control border
  63. if (showBorder)
  64. {
  65. this.DrawVisualStyleBorder(g, new Rectangle(0, 0, Width - 1, Height - 1));
  66. }
  67. // draw items
  68. int _2 = this.LogicalToDevice(2);
  69. int _4 = this.LogicalToDevice(4);
  70. int _8 = this.LogicalToDevice(8);
  71. int _9 = this.LogicalToDevice(9);
  72. int _12 = this.LogicalToDevice(12);
  73. int _15 = this.LogicalToDevice(15);
  74. for (int i = 0; i < styles.Length; i++)
  75. {
  76. // highlight active style
  77. if (this.styles[i] == style)
  78. DrawHighlight(g, new Rectangle(_4, i * _15 + _4, Width - _9, _15));
  79. using (Pen p = new Pen(lineColor, this.LogicalToDevice(lineWidth < 1.5f ? 1.5f : lineWidth)))
  80. {
  81. int y = i * _15 + _12;
  82. if (this.styles[i] == LineStyle.Double)
  83. {
  84. y -= _2;
  85. g.DrawLine(p, _8, y, Width - _9, y);
  86. y += _2 + _2;
  87. }
  88. else
  89. {
  90. p.DashStyle = (DashStyle)this.styles[i];
  91. }
  92. g.DrawLine(p, _8, y, Width - _9, y);
  93. };
  94. }
  95. }
  96. protected override void OnMouseUp(MouseEventArgs e)
  97. {
  98. int i = (e.Y - this.LogicalToDevice(4)) / this.LogicalToDevice(15);
  99. if (i < 0)
  100. i = 0;
  101. if (i > styles.Length - 1)
  102. i = styles.Length - 1;
  103. Style = styles[i];
  104. if (StyleSelected != null)
  105. StyleSelected(this, EventArgs.Empty);
  106. }
  107. public void UpdateDpiDependencies(Control owner)
  108. {
  109. #if MONO
  110. // WPF: fix issues with multi-monitor dpi. Dropdown will be scaled automatically
  111. owner = this;
  112. #endif
  113. if (owner == null)
  114. return;
  115. Size = owner.LogicalToDevice(new Size(70, 100));
  116. }
  117. public LineStyleControl()
  118. {
  119. styles = new LineStyle[] {
  120. LineStyle.Solid, LineStyle.Dash, LineStyle.Dot, LineStyle.DashDot, LineStyle.DashDotDot, LineStyle.Double };
  121. lineColor = Color.Black;
  122. lineWidth = 1;
  123. showBorder = true;
  124. BackColor = SystemColors.Window;
  125. SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true);
  126. Size = new Size(70, 100);
  127. }
  128. }
  129. }