using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; namespace laserPTZ.Controls { public partial class HalfCoordinateUc : UserControl { [Category("wyl")] [Description("角度值")] public float value = 0; /// <summary> /// 定义该仪表盘画布的最大值为371 /// </summary> private int maxSize = 371; /// <summary> /// 仪表盘画布的放大倍数,默认1 /// </summary> private float multiple = 1; /// <summary> /// 定义轴线长度 /// </summary> private float diameter; /// <summary> /// 每个间隔值 /// </summary> private int intervalValue; /// <summary> /// 仪表盘显示的最小值,默认为0 /// </summary> private float minValue = 0; /// <summary> /// 仪表盘显示的最小值 /// </summary> [Category("wyl")] [Description("仪表盘显示的最小值")] public float MinValue { get { return minValue; } set { if (value >= MaxValue) { MessageBox.Show("最小值不能超过最大值!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning); minValue = 0; } else { minValue = value; //drawBackImage(); } } } /// <summary> /// 仪表盘上显示的最大值,默认123。 /// </summary> private float maxValue = 123; /// <summary> /// 仪表盘上显示的最大值 /// </summary> [Category("wyl")] [Description("仪表盘上显示的最大值")] public float MaxValue { get { return maxValue; } set { if (value <= MinValue) { MessageBox.Show("最大值不能低于最小值!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning); maxValue = 123; } else { maxValue = value; //drawBackImage(); } } } // <summary> /// 仪表盘变换的值,默认为0; /// </summary> private float changeValue = 0; /// <summary> /// 仪表盘变换的值 /// </summary> public float ChangeValue { get { return changeValue; } set { changeValue = value; } } /// <summary> /// 指针颜色 /// </summary> private Color pinColor = Color.FromArgb(191, 148, 28); public Color PinColor { get { return pinColor; } set { pinColor = value; } } public HalfCoordinateUc() { InitializeComponent(); //双缓存防止屏幕抖动 this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer, true); this.SetStyle(ControlStyles.UserPaint, true); this.UpdateStyles(); //设置背景颜色为透明 this.BackColor = Color.Transparent; InitialCanvas(); } //private int uintfontsize = 40; /// <summary> /// 初始化仪表盘画布 /// </summary> private void InitialCanvas() { //对比控件的长高,以最小值为轴线长度 if (this.Width > this.Height) { diameter = this.Height - 30; } else { diameter = this.Width - 30; } intervalValue = (int)(diameter / 3);//计算每个间隔之间的值 } /// <summary> /// 画图 /// </summary> /// <param name="g"></param> private void DrawPin(Graphics g) { Bitmap bit = new Bitmap(this.Width, this.Height); Graphics gp = Graphics.FromImage(bit); gp.DrawLine(new Pen(Color.FromArgb(139, 129, 76)), new Point(16, this.Height / 2), new Point(this.Width/2, this.Height / 2)); gp.DrawLine(new Pen(Color.FromArgb(139, 129, 76)), new Point(this.Width / 2, 16), new Point(this.Width / 2, this.Height - 16)); //Color color = Color.FromArgb(139, 129, 76); Color color = Color.RoyalBlue; SolidBrush brush = new SolidBrush(color); Font scaleFont = new Font(new FontFamily("微软雅黑"), 9, FontStyle.Regular); gp.DrawString("0°", scaleFont, brush, new Point(0, this.Height / 2 - 8)); gp.DrawString("90°", scaleFont, brush, new Point(this.Width / 2 - 13, 0)); gp.DrawString("-90°", scaleFont, brush, new Point(this.Width / 2 - 19, this.Height - 14)); int inter = 20;//圆圈之间的间隔 Rectangle Rec = new Rectangle(16 + 5, 16 + 5, this.Width - 16 - 16 - 5 * 2, this.Height - 16 - 16 - 5 * 2); //使用DrawArc方法绘制圆弧,起点角度45度,结束角度260度 gp.DrawArc(new Pen(Color.FromArgb(139, 129, 76)), Rec, 90, 180); Rec = new Rectangle(16 + 5 + inter, 16 + 5 + inter, this.Width - 16 - 16 - 5 * 2 - inter * 2, this.Height - 16 - 16 - 5 * 2 - inter * 2); gp.DrawArc(new Pen(Color.FromArgb(139, 129, 76)), Rec, 90, 180); Rec = new Rectangle(16 + 5 + inter *2, 16 + 5 + inter * 2, this.Width - 16 - 16 - 5 * 2 - inter * 4, this.Height - 16 - 16 - 5 * 2 - inter * 4); gp.DrawArc(new Pen(Color.FromArgb(139, 129, 76)), Rec, 90, 180); double fc = Math.Cos((value * (Math.PI)) / 180); double fs = Math.Sin((value * (Math.PI)) / 180); int r = (this.Width - 16 - 26 - 5 * 2) / 2; int x = this.Width / 2 - 5 - (int)(fc * r); int y = this.Height / 2 - (int)(fs * r); gp.DrawLine(new Pen(Color.LawnGreen, 1), new Point(this.Width / 2 - 5, this.Height / 2), new Point(x, y)); g.DrawImage(bit, 0, 0); gp.Dispose(); } private void HalfCoordinateUc_Load(object sender, EventArgs e) { InitialCanvas(); } private void HalfCoordinateUc_Paint(object sender, PaintEventArgs e) { DrawPin(e.Graphics); } private void HalfCoordinateUc_Resize(object sender, EventArgs e) { InitialCanvas(); } } }