using System;
using System.Drawing;
using System.Windows.Forms;
namespace MyControls
{
///
/// 在图上显示的标尺类
///
public partial class Control_Ruler : UserControl
{
#region 变量定义
private string m_showstring = "1mm";
private double m_f_value = 1; //实际表示的长度值
//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
///
/// 设置当前标尺长度
///
public void SetValue(int in_value)
{
this.Width = in_value + 20; //有20的宽度是边,所以补上
}
///
///
///
///
///
public void SetValue(int in_value,string showstring)
{
this.Width = in_value + 20; //有20的宽度是边,所以补上
m_showstring = showstring + "mm";
}
}
}