MeasureRangeControl.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. //added by songxk begin
  25. public enum MovingMethodEnum
  26. {
  27. Hor_Plus = 1,
  28. Hor_Minus = 2,
  29. Ver_Plus = 3,
  30. Ver_Minus = 4
  31. };
  32. public MovingMethodEnum MovingMothed { get; set; }
  33. public float MeasureHeight { get; set; }
  34. public event EventHandler ChangeEvent;
  35. //added by songxk end
  36. public MeasureRangeControl()
  37. {
  38. Paint += MeasureRangeControl_Paint;
  39. }
  40. public void InitGrid(SampleStageModel stageSettingNow)
  41. {
  42. _setting = stageSettingNow;
  43. _scal = Math.Min(Parent.Width / _width, Parent.Height / _height);
  44. _scal = Math.Min(Parent.Height / _dia, _scal);
  45. offxGrid = 0;
  46. offxC = 0;
  47. offyGrid = 0;
  48. offyC = 0;
  49. if (_dia > _width)
  50. {
  51. offxGrid = (float)((_dia - _width) * _scal) / 2;
  52. }
  53. else
  54. {
  55. offxC = (float)((_width - _dia) * _scal) / 2;
  56. }
  57. if (_dia > _height)
  58. {
  59. offyGrid = (float)((_dia - _height) * _scal) / 2;
  60. }
  61. else
  62. {
  63. offyC = (float)((_height - _dia) * _scal) / 2;
  64. }
  65. Invalidate();
  66. }
  67. public void Reset()
  68. {
  69. _setting = null;
  70. Invalidate();
  71. }
  72. private void MeasureRangeControl_Paint(object sender, PaintEventArgs e)
  73. {
  74. if (_setting == null) return;
  75. var pen = new Pen(Color.Red);
  76. pen.Width = 2;
  77. pen.DashStyle = DashStyle.Dash;
  78. var bursh = new SolidBrush(Color.White);
  79. var g = e.Graphics;
  80. g.FillEllipse(bursh, offxC, offyC, _dia * _scal, _dia * _scal);
  81. g.DrawRectangle(pen, offxGrid, offyGrid, _width * _scal, _height * _scal);
  82. }
  83. protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
  84. {
  85. switch (keyData)
  86. {
  87. case Keys.Left:
  88. MovingMothed = MovingMethodEnum.Hor_Minus;
  89. ChangeEvent(this, new EventArgs());
  90. break;
  91. case Keys.Up:
  92. MovingMothed = MovingMethodEnum.Ver_Plus;
  93. ChangeEvent(this, new EventArgs());
  94. break;
  95. case Keys.Right:
  96. MovingMothed = MovingMethodEnum.Hor_Plus;
  97. ChangeEvent(this, new EventArgs());
  98. break;
  99. case Keys.Down:
  100. MovingMothed = MovingMethodEnum.Ver_Minus;
  101. ChangeEvent(this, new EventArgs());
  102. break;
  103. default:
  104. break;
  105. }
  106. return base.ProcessCmdKey(ref msg, keyData);
  107. }
  108. }
  109. }