Control_Ruler.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. /// 传入标尺要显示的宽度,及该宽度代表的1像素um单位级实际宽度
  84. /// </summary>
  85. /// <param name="in_width">100um标尺长度</param>
  86. public void SetValue(double in_width)
  87. {
  88. this.m_f_value = in_width;
  89. this.RulerWidth = Convert.ToInt32(in_width);
  90. }
  91. /// <summary>
  92. /// 设置当前标尺显示的值,自动计算其显示的文字及宽度
  93. /// </summary>
  94. public void SetValueInspector(double in_value, double in_xs)
  95. {
  96. double rulerWidth = 100;
  97. double bs = rulerWidth / in_value;
  98. if (bs > 1)
  99. {
  100. m_bs = (float)Math.Round(bs);
  101. }
  102. else if (bs < 1)
  103. {
  104. m_bs = (float)Math.Round(bs, 1);
  105. }
  106. this.Width = (int)(m_bs * in_value);
  107. //修改显示文字
  108. m_showstring = Convert.ToInt32(100 * m_bs * in_xs).ToString() + "um";
  109. Invalidate();
  110. }
  111. #endregion
  112. }
  113. }