FormsPlot.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Drawing;
  5. using System.Windows.Forms;
  6. using Uni_Trend.MSO7000X.Touch;
  7. #pragma warning disable IDE1006 // lowercase public properties
  8. #pragma warning disable CS0067 // unused events
  9. namespace ScottPlot
  10. {
  11. public partial class FormsPlot : UserControl,ITouchAble
  12. {
  13. /// <summary>
  14. /// This is the plot displayed by the user control.
  15. /// After modifying it you may need to call Render() to request the plot be redrawn on the screen.
  16. /// </summary>
  17. public Plot Plot => Backend.Plot;
  18. /// <summary>
  19. /// This object can be used to modify advanced behaior and customization of this user control.
  20. /// </summary>
  21. public Control.Configuration Configuration => Backend.Configuration;
  22. /// <summary>
  23. /// This event is invoked any time the axis limits are modified.
  24. /// </summary>
  25. public event EventHandler AxesChanged;
  26. /// <summary>
  27. /// 创建扩展通道事件
  28. /// </summary>
  29. public event EventHandler ZoomCreated;
  30. /// <summary>
  31. /// This event is invoked any time the plot is right-clicked.
  32. /// By default it contains DefaultRightClickEvent(), but you can remove this and add your own method.
  33. /// </summary>
  34. public event EventHandler RightClicked;
  35. /// <summary>
  36. /// This event is invoked after the mouse moves while dragging a draggable plottable.
  37. /// The object passed is the plottable being dragged.
  38. /// </summary>
  39. public event EventHandler PlottableDragged;
  40. [Obsolete("use 'PlottableDragged' instead", error: true)]
  41. public event EventHandler MouseDragPlottable;
  42. /// <summary>
  43. /// This event is invoked right after a draggable plottable was dropped.
  44. /// The object passed is the plottable that was just dropped.
  45. /// </summary>
  46. public event EventHandler PlottableDropped;
  47. [Obsolete("use 'PlottableDropped' instead", error: true)]
  48. public event EventHandler MouseDropPlottable;
  49. //定义ITouchAble的接口相关的事件
  50. public event Action<int, int> TouchDown;
  51. public event Action<int, int, int, int> TouchMove;
  52. public event Action<int, int> TouchUp;
  53. public event Action<int, int> TouchTranslation;
  54. public event Action<float> TouchRotation;
  55. public event Action<float, ScaleDirection> TouchScale;
  56. private readonly Control.ControlBackEnd Backend = new(1, 1);
  57. private readonly Dictionary<Cursor, System.Windows.Forms.Cursor> Cursors;
  58. private readonly bool IsDesignerMode = Process.GetCurrentProcess().ProcessName == "devenv";
  59. [Obsolete("Reference 'Plot' instead of 'plt'")]
  60. public Plot plt => Plot;
  61. public FormsPlot()
  62. {
  63. if (IsDesignerMode)
  64. {
  65. try
  66. {
  67. Plot.Title($"ScottPlot {Plot.Version}");
  68. Plot.Render();
  69. }
  70. catch (Exception e)
  71. {
  72. InitializeComponent();
  73. pictureBox1.Visible = false;
  74. rtbErrorMessage.Visible = true;
  75. rtbErrorMessage.Dock = DockStyle.Fill;
  76. rtbErrorMessage.Text = "ERROR: ScottPlot failed to render in design mode.\n\n" +
  77. "This may be due to incompatible System.Drawing.Common versions or a 32-bit/64-bit mismatch.\n\n" +
  78. "Although rendering failed at design time, it may still function normally at runtime.\n\n" +
  79. $"Exception details:\n{e}";
  80. return;
  81. }
  82. }
  83. Backend.Resize(Width, Height, useDelayedRendering: false);
  84. Backend.BitmapChanged += new EventHandler(OnBitmapChanged);
  85. Backend.BitmapUpdated += new EventHandler(OnBitmapUpdated);
  86. Backend.CursorChanged += new EventHandler(OnCursorChanged);
  87. Backend.RightClicked += new EventHandler(OnRightClicked);
  88. Backend.AxesChanged += new EventHandler(OnAxesChanged);
  89. Backend.ZoomCreated += new EventHandler(OnZoomCreated);
  90. Backend.PlottableDragged += new EventHandler(OnPlottableDragged);
  91. Backend.PlottableDropped += new EventHandler(OnPlottableDropped);
  92. Cursors = new Dictionary<Cursor, System.Windows.Forms.Cursor>()
  93. {
  94. [ScottPlot.Cursor.Arrow] = System.Windows.Forms.Cursors.Arrow,
  95. [ScottPlot.Cursor.WE] = System.Windows.Forms.Cursors.SizeWE,
  96. [ScottPlot.Cursor.NS] = System.Windows.Forms.Cursors.SizeNS,
  97. [ScottPlot.Cursor.All] = System.Windows.Forms.Cursors.SizeAll,
  98. [ScottPlot.Cursor.Crosshair] = System.Windows.Forms.Cursors.Cross,
  99. [ScottPlot.Cursor.Hand] = System.Windows.Forms.Cursors.Hand,
  100. [ScottPlot.Cursor.Question] = System.Windows.Forms.Cursors.Help,
  101. };
  102. InitializeComponent();
  103. rtbErrorMessage.Visible = false;
  104. pictureBox1.BackColor = System.Drawing.Color.Transparent;
  105. BackColor = System.Drawing.Color.Transparent;
  106. Plot.Style(figureBackground: BackColor);
  107. pictureBox1.MouseWheel += PictureBox1_MouseWheel;
  108. RightClicked += DefaultRightClickEvent;
  109. Backend.StartProcessingEvents();
  110. SetStyle(ControlStyles.UserPaint |
  111. ControlStyles.AllPaintingInWmPaint |
  112. ControlStyles.OptimizedDoubleBuffer |
  113. ControlStyles.ResizeRedraw |
  114. ControlStyles.SupportsTransparentBackColor, true);
  115. }
  116. /// <summary>
  117. /// Return the mouse position on the plot (in coordinate space) for the latest X and Y coordinates
  118. /// </summary>
  119. public (double x, double y) GetMouseCoordinates() => Backend.GetMouseCoordinates();
  120. /// <summary>
  121. /// Return the mouse position (in pixel space) for the last observed mouse position
  122. /// </summary>
  123. public (float x, float y) GetMousePixel() => Backend.GetMousePixel();
  124. /// <summary>
  125. /// Reset this control by replacing the current plot with a new empty plot
  126. /// </summary>
  127. public void Reset() => Backend.Reset(Width, Height);
  128. /// <summary>
  129. /// Reset this control by replacing the current plot with an existing plot
  130. /// </summary>
  131. public void Reset(Plot newPlot) => Backend.Reset(Width, Height, newPlot);
  132. /// <summary>
  133. /// Re-render the plot and update the image displayed by this control.
  134. /// </summary>
  135. /// <param name="lowQuality">disable anti-aliasing to produce faster (but lower quality) plots</param>
  136. /// <param name="skipIfCurrentlyRendering"></param>
  137. public void Render(bool lowQuality = false, bool skipIfCurrentlyRendering = false)
  138. {
  139. Application.DoEvents();
  140. Backend.Render(lowQuality, skipIfCurrentlyRendering);
  141. this.Invalidate();
  142. }
  143. /// <summary>
  144. /// 余晖类型
  145. /// </summary>
  146. public WaveFormPersist WfmPersist
  147. {
  148. get
  149. {
  150. return Backend.WfmPersist;
  151. }
  152. set
  153. {
  154. Backend.WfmPersist = value;
  155. }
  156. }
  157. /// <summary>
  158. /// Request the control re-render the next time it is available.
  159. /// This method does not block the calling thread.
  160. /// </summary>
  161. public void RenderRequest(RenderType renderType = RenderType.LowQualityThenHighQualityDelayed) => Backend.RenderRequest(renderType);
  162. public void SetZoomingState(Boolean state)
  163. {
  164. Backend.IsZooming = state;
  165. }
  166. /// <summary>
  167. /// 清除无限余晖时的波形缓存
  168. /// </summary>
  169. public void ClearWfmCache()
  170. {
  171. Backend.ClearWfmCache();
  172. }
  173. private void PlottableCountTimer_Tick(object sender, EventArgs e) => Backend.RenderIfPlottableListChanged();
  174. private void FormsPlot_Load(object sender, EventArgs e) { OnSizeChanged(null, null); }
  175. private void OnBitmapUpdated(object sender, EventArgs e) { Application.DoEvents(); /*pictureBox1.Invalidate();*/ }
  176. private void OnBitmapChanged(object sender, EventArgs e) {/* pictureBox1.Image = Backend.GetLatestBitmap();*/ }
  177. private void OnCursorChanged(object sender, EventArgs e) => Cursor = Cursors[Backend.Cursor];
  178. private void OnSizeChanged(object sender, EventArgs e) => Backend.Resize(Width, Height, useDelayedRendering: true);//创建BMP
  179. private void OnAxesChanged(object sender, EventArgs e) => AxesChanged?.Invoke(this, e);
  180. private void OnZoomCreated(object sender, EventArgs e) => ZoomCreated?.Invoke(this, e);
  181. private void OnRightClicked(object sender, EventArgs e) => RightClicked?.Invoke(this, e);
  182. private void OnPlottableDragged(object sender, EventArgs e) => PlottableDragged?.Invoke(sender, e);
  183. private void OnPlottableDropped(object sender, EventArgs e) => PlottableDropped?.Invoke(sender, e);
  184. private void PictureBox1_MouseDown(object sender, MouseEventArgs e) { Backend.MouseDown(GetInputState(e)); base.OnMouseDown(e); }
  185. private void PictureBox1_MouseUp(object sender, MouseEventArgs e) { Backend.MouseUp(GetInputState(e)); base.OnMouseUp(e); }
  186. private void PictureBox1_DoubleClick(object sender, EventArgs e) { Backend.DoubleClick(); base.OnDoubleClick(e); }
  187. private void PictureBox1_MouseWheel(object sender, MouseEventArgs e) { Backend.MouseWheel(GetInputState(e)); base.OnMouseWheel(e); }
  188. private void PictureBox1_MouseMove(object sender, MouseEventArgs e) { Backend.MouseMove(GetInputState(e)); base.OnMouseMove(e); }
  189. private void PictureBox1_MouseEnter(object sender, EventArgs e) => base.OnMouseEnter(e);
  190. private void PictureBox1_MouseLeave(object sender, EventArgs e) => base.OnMouseLeave(e);
  191. //pictureBox1上的触摸的相关事件的处理
  192. private void pictureBox1_TouchDown(int x, int y)
  193. {
  194. Backend.MouseDown(GetInputState(new MouseEventArgs(MouseButtons.Left, 1, x, y, 0)));
  195. TouchDown?.Invoke(x, y);
  196. }
  197. private void pictureBox1_TouchUp(int x, int y)
  198. {
  199. Backend.MouseUp(GetInputState(new MouseEventArgs(MouseButtons.Left, 1, x, y, 0)));
  200. TouchUp?.Invoke(x, y);
  201. }
  202. private void pictureBox1_TouchScale(float scale, Uni_Trend.MSO7000X.Touch.ScaleDirection direction)
  203. {
  204. TouchScale?.Invoke(scale, direction);
  205. }
  206. private void pictureBox1_TouchMove(int x, int y, int transX,int transY)
  207. {
  208. Backend.MouseMove(GetInputState(new MouseEventArgs(MouseButtons.Left, 1, x, y, 0)));
  209. TouchMove?.Invoke(x, y,transX, transY);
  210. }
  211. private void pictureBox1_TouchRotation(float rotate)
  212. {
  213. TouchRotation?.Invoke(rotate);
  214. }
  215. private Control.InputState GetInputState(MouseEventArgs e) =>
  216. new()
  217. {
  218. X = e.X,
  219. Y = e.Y,
  220. LeftWasJustPressed = e.Button == MouseButtons.Left,
  221. RightWasJustPressed = e.Button == MouseButtons.Right,
  222. MiddleWasJustPressed = e.Button == MouseButtons.Middle,
  223. ShiftDown = ModifierKeys.HasFlag(Keys.Shift),
  224. CtrlDown = ModifierKeys.HasFlag(Keys.Control),
  225. AltDown = ModifierKeys.HasFlag(Keys.Alt),
  226. WheelScrolledUp = e.Delta > 0,
  227. WheelScrolledDown = e.Delta < 0,
  228. };
  229. protected override void OnPaint(PaintEventArgs e)
  230. {
  231. e.Graphics.DrawImage(Backend.GetLatestBitmap(), 0, 0);
  232. base.OnPaint(e);
  233. }
  234. /// <summary>
  235. /// Launch the default right-click menu.
  236. /// </summary>
  237. public void DefaultRightClickEvent(object sender, EventArgs e) => DefaultRightClickMenu.Show(System.Windows.Forms.Cursor.Position);
  238. private void RightClickMenu_Copy_Click(object sender, EventArgs e) => Clipboard.SetImage(Plot.Render());
  239. private void RightClickMenu_Help_Click(object sender, EventArgs e) => new FormHelp().Show();
  240. private void RightClickMenu_AutoAxis_Click(object sender, EventArgs e) { Plot.AxisAuto(); Render(); }
  241. private void RightClickMenu_OpenInNewWindow_Click(object sender, EventArgs e) => new FormsPlotViewer(Plot).Show();
  242. private void RightClickMenu_SaveImage_Click(object sender, EventArgs e)
  243. {
  244. var sfd = new SaveFileDialog
  245. {
  246. FileName = "ScottPlot.png",
  247. Filter = "PNG Files (*.png)|*.png;*.png" +
  248. "|JPG Files (*.jpg, *.jpeg)|*.jpg;*.jpeg" +
  249. "|BMP Files (*.bmp)|*.bmp;*.bmp" +
  250. "|All files (*.*)|*.*"
  251. };
  252. if (sfd.ShowDialog() == DialogResult.OK)
  253. Plot.SaveFig(sfd.FileName);
  254. }
  255. }
  256. }