| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 | using System;using System.Drawing;using System.Windows.Forms;namespace MyControls{    /// <summary>    /// 在图上显示的标尺类    /// </summary>    public partial class Control_Ruler : UserControl    {        #region 变量定义        private string m_showstring = "1mm";        private double m_f_value = 1;  //实际表示的长度值        //private float m_bs = 0;//倍数,计算使用        /// <summary>        /// 显示在标尺上的文字        /// </summary>        public string ShowString        {            get { return m_showstring; }            set { m_showstring = value; }        }        /// <summary>        /// 设置标尺的宽度也就是控件的宽度,里面实际参与计算的标尺宽度为,        /// 左右边各减10的宽度,也就是整宽-20的宽度,这里传入实际宽度,不参与计算        /// </summary>        public int RulerWidth        {            get { return this.Width - 20; }            set { this.Width = value + 20; }        }        /// <summary>        /// 用来存储该标尺,实际的值是多少,用来与显示的宽度进行换算        /// </summary>        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 设置双缓冲        /// <summary>        /// 设置双缓冲        /// </summary>        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        /// <summary>        /// 设置当前标尺长度        /// </summary>        public void SetValue(int in_value)        {            this.Width = in_value + 20; //有20的宽度是边,所以补上        }        /// <summary>        ///         /// </summary>        /// <param name="in_value"></param>        /// <param name="showstring"></param>        public void SetValue(int in_value,string showstring)        {            this.Width = in_value + 20; //有20的宽度是边,所以补上            m_showstring = showstring + "mm";        }    }}
 |