LineStyleControl.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System;
  2. using System.Windows.Forms;
  3. using System.Drawing;
  4. using System.Drawing.Drawing2D;
  5. using System.ComponentModel;
  6. using FastReport.Utils;
  7. namespace FastReport.Controls
  8. {
  9. #if !DEBUG
  10. [DesignTimeVisible(false)]
  11. #endif
  12. internal class LineStyleControl : Control
  13. {
  14. private LineStyle[] styles;
  15. private LineStyle style;
  16. private float lineWidth;
  17. private Color lineColor;
  18. private bool showBorder;
  19. public event EventHandler StyleSelected;
  20. public LineStyle Style
  21. {
  22. get { return style; }
  23. set
  24. {
  25. style = value;
  26. Refresh();
  27. }
  28. }
  29. public float LineWidth
  30. {
  31. get { return lineWidth; }
  32. set
  33. {
  34. lineWidth = value;
  35. Refresh();
  36. }
  37. }
  38. public Color LineColor
  39. {
  40. get { return lineColor; }
  41. set
  42. {
  43. lineColor = value;
  44. Refresh();
  45. }
  46. }
  47. public bool ShowBorder
  48. {
  49. get { return showBorder; }
  50. set
  51. {
  52. showBorder = value;
  53. Refresh();
  54. }
  55. }
  56. private void DrawHighlight(Graphics g, Rectangle rect)
  57. {
  58. using (Brush brush = new SolidBrush(Color.FromArgb(193, 210, 238)))
  59. g.FillRectangle(brush, rect);
  60. using (Pen pen = new Pen(Color.FromArgb(49, 106, 197)))
  61. g.DrawRectangle(pen, rect);
  62. }
  63. protected override void OnPaint(PaintEventArgs e)
  64. {
  65. Graphics g = e.Graphics;
  66. // draw control border
  67. if (showBorder)
  68. {
  69. using (Pen p = new Pen(Color.FromArgb(127, 157, 185)))
  70. g.DrawRectangle(p, 0, 0, Width - 1, Height - 1);
  71. }
  72. // draw items
  73. for (int i = 0; i < styles.Length; i++)
  74. {
  75. // highlight active style
  76. if (this.styles[i] == style)
  77. DrawHighlight(g, new Rectangle(
  78. this.LogicalToDevice(4),
  79. i * this.LogicalToDevice(15) + this.LogicalToDevice(4),
  80. Width - this.LogicalToDevice(9),
  81. this.LogicalToDevice(15)));
  82. using (Pen p = new Pen(lineColor, this.LogicalToDevice(lineWidth < 1.5f ? 1.5f : lineWidth)))
  83. {
  84. DashStyle[] dashStyles = new DashStyle[] {
  85. DashStyle.Solid, DashStyle.Dash, DashStyle.Dot, DashStyle.DashDot, DashStyle.DashDotDot, DashStyle.Solid };
  86. p.DashStyle = dashStyles[(int)this.styles[i]];
  87. if (this.styles[i] == LineStyle.Double)
  88. {
  89. p.Width *= 2.5f;
  90. p.CompoundArray = new float[] { 0, 0.4f, 0.6f, 1 };
  91. }
  92. g.DrawLine(p, this.LogicalToDevice(8), i * this.LogicalToDevice(15) + this.LogicalToDevice(12), Width - this.LogicalToDevice(8), i * this.LogicalToDevice(15) + this.LogicalToDevice(12));
  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 LineStyleControl()
  108. {
  109. styles = new LineStyle[] {
  110. LineStyle.Solid, LineStyle.Dash, LineStyle.Dot, LineStyle.DashDot, LineStyle.DashDotDot, LineStyle.Double };
  111. lineColor = Color.Black;
  112. lineWidth = 1;
  113. showBorder = true;
  114. BackColor = SystemColors.Window;
  115. SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true);
  116. Size = new Size(70, 100);
  117. }
  118. }
  119. }