MeasureRangeControl.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Drawing.Drawing2D;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows.Forms;
  9. namespace Metis.AutoAnalysis
  10. {
  11. internal class MeasureRangeControl : UserControl
  12. {
  13. float _scal;
  14. float w;
  15. float h;
  16. private SampleStageModel _setting;
  17. private float offxGrid;
  18. private float offxC;
  19. private float offyGrid;
  20. private float offyC;
  21. float _dia => _setting.Diameter;
  22. private float _width => _setting.Width;
  23. private float _height => _setting.Height;
  24. public MeasureRangeControl()
  25. {
  26. Paint += MeasureRangeControl_Paint;
  27. }
  28. public void InitGrid(SampleStageModel stageSettingNow)
  29. {
  30. _setting = stageSettingNow;
  31. _scal = Math.Min(Parent.Width / _width, Parent.Height / _height);
  32. _scal = Math.Min(Parent.Height / _dia, _scal);
  33. offxGrid = 0;
  34. offxC = 0;
  35. offyGrid = 0;
  36. offyC = 0;
  37. if (_dia > _width)
  38. {
  39. offxGrid = (float)((_dia - _width) * _scal) / 2;
  40. }
  41. else
  42. {
  43. offxC = (float)((_width - _dia) * _scal) / 2;
  44. }
  45. if (_dia > _height)
  46. {
  47. offyGrid = (float)((_dia - _height) * _scal) / 2;
  48. }
  49. else
  50. {
  51. offyC = (float)((_height - _dia) * _scal) / 2;
  52. }
  53. Invalidate();
  54. }
  55. public void Reset()
  56. {
  57. _setting = null;
  58. Invalidate();
  59. }
  60. private void MeasureRangeControl_Paint(object sender, PaintEventArgs e)
  61. {
  62. if (_setting == null) return;
  63. var pen = new Pen(Color.Red);
  64. pen.Width = 2;
  65. pen.DashStyle = DashStyle.Dash;
  66. var bursh = new SolidBrush(Color.White);
  67. var g = e.Graphics;
  68. g.FillEllipse(bursh, offxC, offyC, _dia * _scal, _dia * _scal);
  69. g.DrawRectangle(pen, offxGrid, offyGrid, _width * _scal, _height * _scal);
  70. }
  71. }
  72. }