Control_Ruler.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. namespace OTSIncAReportGraph.Controls
  5. {
  6. /// <summary>
  7. /// 在图上显示的标尺类
  8. /// </summary>
  9. public partial class Control_Ruler : UserControl
  10. {
  11. #region 变量定义
  12. private string m_showstring = "100um";
  13. private double m_f_value = 0; //实际表示的长度值
  14. private float m_bs = 0;//倍数,计算使用
  15. /// <summary>
  16. /// 显示在标尺上的文字
  17. /// </summary>
  18. public string ShowString
  19. {
  20. get { return m_showstring; }
  21. set { m_showstring = value; }
  22. }
  23. /// <summary>
  24. /// 设置标尺的宽度也就是控件的宽度,里面实际参与计算的标尺宽度为,
  25. /// 左右边各减10的宽度,也就是整宽-20的宽度,这里传入实际宽度,不参与计算
  26. /// </summary>
  27. public int RulerWidth
  28. {
  29. get { return this.Width - 20; }
  30. set { this.Width = value + 20; }
  31. }
  32. /// <summary>
  33. /// 用来存储该标尺,实际的值是多少,用来与显示的宽度进行换算
  34. /// </summary>
  35. public double Value
  36. {
  37. get { return m_f_value; }
  38. set { this.m_f_value = value; }
  39. }
  40. #endregion
  41. #region 构造函数及窗体加载
  42. public Control_Ruler()
  43. {
  44. InitializeComponent();
  45. }
  46. private void Control_Ruler_Load(object sender, EventArgs e)
  47. {
  48. SetDoubleBufferByIsDraw();
  49. //设置Style支持透明背景色
  50. this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
  51. this.BackColor = Color.FromArgb(200, 255, 255, 255);
  52. }
  53. #endregion
  54. #region 设置双缓冲
  55. /// <summary>
  56. /// 设置双缓冲
  57. /// </summary>
  58. public void SetDoubleBufferByIsDraw()
  59. {
  60. SetStyle(ControlStyles.UserPaint, true);
  61. SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
  62. this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true); // 双缓冲
  63. }
  64. #endregion
  65. #region 绘制事件
  66. protected override void OnPaint(PaintEventArgs e)
  67. {
  68. Graphics g = e.Graphics;
  69. Brush b = new SolidBrush(Color.Black);
  70. Pen p = new Pen(b);
  71. p.Width = 1;
  72. //直线
  73. g.DrawLine(p, 10, 20, this.Width - 10, 20);
  74. //两个竖线
  75. g.DrawLine(p, 10, 10, 10, 30);
  76. g.DrawLine(p, this.Width - 10, 10, Width - 10, 30);
  77. //写上要输出的文字
  78. g.DrawString(m_showstring, new Font("宋体", 9), b, this.Width / 2 - 10, 25);
  79. }
  80. #endregion
  81. #region 设置控制显示部份
  82. /// <summary>
  83. /// 设置当前标尺显示的值,自动计算其显示的文字及宽度
  84. /// </summary>
  85. public void SetValue(double in_value)
  86. {
  87. this.m_f_value = in_value;
  88. this.RulerWidth = Convert.ToInt32(in_value);
  89. //写死缩放逻辑
  90. if (this.Width > 100)
  91. {
  92. m_bs = (float)in_value / 100;
  93. for (int i = 0; i < 100; i++)
  94. {
  95. if (m_bs > i && m_bs <= (i + 1))
  96. {
  97. m_bs = i + 1;
  98. }
  99. }
  100. this.Width = Convert.ToInt32(in_value / m_bs) + 20; //有20的宽度是边,所以补上
  101. //修改显示文字
  102. m_showstring = Convert.ToInt32(100 / m_bs).ToString() + "μm";
  103. }
  104. }
  105. /// <summary>
  106. /// 传入标尺要显示的宽度,及该宽度代表的1像素um单位级实际宽度
  107. /// </summary>
  108. /// <param name="in_width"></param>
  109. /// <param name="in_onepixel_size"></param>
  110. public void SetValue(double in_width, double in_onepixel_size)
  111. {
  112. this.m_f_value = in_width;
  113. this.RulerWidth = Convert.ToInt32(in_width);
  114. //显示信息
  115. m_showstring = Convert.ToInt32(in_width * in_onepixel_size).ToString() + "μm";
  116. }
  117. /// <summary>
  118. /// 设置当前标尺显示的值,自动计算其显示的文字及宽度
  119. /// </summary>
  120. public void SetValueInspector(double in_value, double in_xs)
  121. {
  122. double rulerWidth = 100;
  123. double bs = rulerWidth / in_value;
  124. if (bs > 1)
  125. {
  126. m_bs = (float)Math.Round(bs);
  127. }
  128. else if (bs < 1)
  129. {
  130. m_bs = (float)Math.Round(bs, 1);
  131. }
  132. this.Width = (int)(m_bs * in_value);
  133. //修改显示文字
  134. m_showstring = Convert.ToInt32(100 * m_bs * in_xs).ToString() + "um";
  135. Invalidate();
  136. }
  137. #endregion
  138. }
  139. }