Control_Ruler.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. namespace MyControls
  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. /// <summary>
  82. /// 设置当前标尺显示的值,自动计算其显示的文字及宽度
  83. /// </summary>
  84. public void SetValue(double in_value)
  85. {
  86. this.m_f_value = in_value;
  87. this.RulerWidth = Convert.ToInt32(in_value);
  88. //写死缩放逻辑
  89. if (this.Width > 100)
  90. {
  91. m_bs = (float)in_value / 100;
  92. for (int i = 0; i < 100; i++)
  93. {
  94. if (m_bs > i && m_bs <= (i + 1))
  95. {
  96. m_bs = i + 1;
  97. }
  98. }
  99. this.Width = Convert.ToInt32(in_value / m_bs) + 20; //有20的宽度是边,所以补上
  100. //修改显示文字
  101. m_showstring = Convert.ToInt32(100 / m_bs).ToString() + "μm";
  102. }
  103. }
  104. /// <summary>
  105. /// 传入标尺要显示的宽度,及该宽度代表的1像素um单位级实际宽度
  106. /// </summary>
  107. /// <param name="in_width"></param>
  108. /// <param name="in_onepixel_size"></param>
  109. public void SetValue(double in_width, double in_onepixel_size)
  110. {
  111. this.m_f_value = in_width;
  112. this.RulerWidth = Convert.ToInt32(in_width);
  113. //显示信息
  114. m_showstring = Convert.ToInt32(in_width* in_onepixel_size).ToString() + "μm";
  115. }
  116. /// <summary>
  117. /// 设置当前标尺显示的值,自动计算其显示的文字及宽度
  118. /// </summary>
  119. public void SetValueInspector(double in_value, double in_xs)
  120. {
  121. double rulerWidth = 100;
  122. double bs = rulerWidth / in_value;
  123. if (bs > 1)
  124. {
  125. m_bs = (float)Math.Round(bs);
  126. }
  127. else if (bs < 1)
  128. {
  129. m_bs = (float)Math.Round(bs, 1);
  130. }
  131. this.Width = (int)(m_bs * in_value);
  132. //修改显示文字
  133. m_showstring = Convert.ToInt32(100 * m_bs * in_xs).ToString() + "um";
  134. Invalidate();
  135. }
  136. }
  137. }