CVisualFieldGDIObject.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System.Drawing;
  2. using System.Drawing.Drawing2D;
  3. using System.Windows.Forms;
  4. namespace OTSMeasureApp._4_OTSSamplespaceGraphicsPanel
  5. {
  6. public class CVisualFieldGDIObject : CDisplayGDIObject
  7. {
  8. private int m_sequenceNum;
  9. private bool m_enable;
  10. public CVisualFieldGDIObject(RectangleF rectField, int OTSx, int OTSy, string name, string sampleName, Color selColor)
  11. {
  12. m_OrigineRegionF = rectField;
  13. SetInitRegionF(m_OrigineRegionF);
  14. HoleNo = name;
  15. SelColor = selColor;
  16. SampleName = sampleName;
  17. ID = System.Guid.NewGuid().ToString();
  18. OTSX = OTSx;
  19. OTSY = OTSy;
  20. }
  21. public int SequenceNum { get => m_sequenceNum; set => m_sequenceNum = value; }
  22. public bool Enable { get => m_enable; set => m_enable = value; }
  23. public override void OnPaint(PaintEventArgs e)
  24. {
  25. Color myColor = SelColor;
  26. System.Drawing.SolidBrush sampleBrush = new System.Drawing.SolidBrush(myColor);
  27. Pen pen;
  28. if (m_enable)
  29. {
  30. pen = new Pen(sampleBrush, 0.01f);
  31. }
  32. else
  33. {
  34. sampleBrush = new System.Drawing.SolidBrush(Color.Gray);
  35. pen = new Pen(new SolidBrush(Color.Gray), 0.01f);
  36. }
  37. e.Graphics.SmoothingMode = SmoothingMode.Default;
  38. //设置直线位置与尺寸
  39. PointF startPointF = new PointF(m_RegionF.Left, m_RegionF.Top);
  40. PointF endPointF = new PointF(m_RegionF.Right, m_RegionF.Bottom);
  41. if (m_RegionF.Height > 20)//won't display when it's too small,to speed up
  42. {
  43. float fontSize = m_RegionF.Width / 4;
  44. Font font;
  45. if (fontSize == 0)
  46. {
  47. font = new Font("宋体", 5, FontStyle.Regular);
  48. }
  49. else
  50. {
  51. font = new Font("宋体", fontSize, FontStyle.Regular);
  52. }
  53. StringFormat sf = new StringFormat();
  54. sf.Alignment = StringAlignment.Center;
  55. sf.LineAlignment = StringAlignment.Center;
  56. e.Graphics.DrawString(m_sequenceNum.ToString(), font, sampleBrush, m_RegionF, sf);
  57. }
  58. //绘制帧图
  59. e.Graphics.DrawRectangle(pen, startPointF.X, startPointF.Y, endPointF.X - startPointF.X, endPointF.Y - startPointF.Y);
  60. }
  61. }
  62. }