using System; using System.Drawing; using System.Windows.Forms; namespace OTSIncAReportGraph.Controls { /// /// 在图上显示的标尺类 /// public partial class Control_Ruler : UserControl { #region 变量定义 private string m_showstring = "100um"; private double m_f_value = 0; //实际表示的长度值 private float m_bs = 0;//倍数,计算使用 /// /// 显示在标尺上的文字 /// public string ShowString { get { return m_showstring; } set { m_showstring = value; } } /// /// 设置标尺的宽度也就是控件的宽度,里面实际参与计算的标尺宽度为, /// 左右边各减10的宽度,也就是整宽-20的宽度,这里传入实际宽度,不参与计算 /// public int RulerWidth { get { return this.Width - 20; } set { this.Width = value + 20; } } /// /// 用来存储该标尺,实际的值是多少,用来与显示的宽度进行换算 /// public double Value { get { return m_f_value; } set { this.m_f_value = value; } } #endregion #region 构造函数及窗体加载 public Control_Ruler() { InitializeComponent(); } private void Control_Ruler_Load(object sender, EventArgs e) { SetDoubleBufferByIsDraw(); //设置Style支持透明背景色 this.SetStyle(ControlStyles.SupportsTransparentBackColor, true); this.BackColor = Color.FromArgb(200, 255, 255, 255); } #endregion #region 设置双缓冲 /// /// 设置双缓冲 /// public void SetDoubleBufferByIsDraw() { SetStyle(ControlStyles.UserPaint, true); SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景. this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true); // 双缓冲 } #endregion #region 绘制事件 protected override void OnPaint(PaintEventArgs e) { Graphics g = e.Graphics; Brush b = new SolidBrush(Color.Black); Pen p = new Pen(b); p.Width = 1; //直线 g.DrawLine(p, 10, 20, this.Width - 10, 20); //两个竖线 g.DrawLine(p, 10, 10, 10, 30); g.DrawLine(p, this.Width - 10, 10, Width - 10, 30); //写上要输出的文字 g.DrawString(m_showstring, new Font("宋体", 9), b, this.Width / 2 - 10, 25); } #endregion #region 设置控制显示部份 /// /// 传入标尺要显示的宽度,及该宽度代表的1像素um单位级实际宽度 /// /// 100um标尺长度 public void SetValue(double in_width) { this.m_f_value = in_width; this.RulerWidth = Convert.ToInt32(in_width); } /// /// 设置当前标尺显示的值,自动计算其显示的文字及宽度 /// public void SetValueInspector(double in_value, double in_xs) { double rulerWidth = 100; double bs = rulerWidth / in_value; if (bs > 1) { m_bs = (float)Math.Round(bs); } else if (bs < 1) { m_bs = (float)Math.Round(bs, 1); } this.Width = (int)(m_bs * in_value); //修改显示文字 m_showstring = Convert.ToInt32(100 * m_bs * in_xs).ToString() + "um"; Invalidate(); } #endregion } }