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; using System.Drawing.Drawing2D; namespace laserPTZ.Controls { public partial class CoordinateUc : UserControl { /// <summary> /// 定义该仪表盘画布的最大值为371 /// </summary> private int maxSize = 371; [Category("wyl")] [Description("角度值")] private float valueNow = 0; public float Value { get { return valueNow; } set { valueNow = value; } } /// <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 CoordinateUc() { 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> public 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> public void DrawPin(Graphics g) { Bitmap bit = new Bitmap(this.Width, this.Height); Graphics gp = Graphics.FromImage(bit); gp.SmoothingMode = SmoothingMode.HighQuality; gp.DrawLine(new Pen(Color.FromArgb(139, 129, 76)), new Point(16, this.Height / 2), new Point(this.Width - 26, this.Height / 2)); gp.DrawLine(new Pen(Color.FromArgb(139, 129, 76)), new Point(this.Width / 2 - 5, 16), new Point(this.Width / 2 - 5, this.Height - 11)); //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("180°", scaleFont, brush, new Point(this.Width - 28 , this.Height / 2 - 8)); gp.DrawString("270°", scaleFont, brush, new Point(this.Width/2 - 19, this.Height - 14)); int inter = 20;//圆圈之间的间隔 gp.DrawEllipse(new Pen(Color.FromArgb(139, 129, 76)), 16+5, 16+5,this.Width - 16 - 26 - 5*2, this.Width - 16 - 26 - 5 * 2);//画椭圆的方法,x坐标、y坐标、宽、高,如果是100,则半径为50 gp.DrawEllipse(new Pen(Color.FromArgb(139, 129, 76)), 16 + 5 + inter, 16 + 5+ inter, this.Width - 16 - 26 - 5 * 2 - 2* inter, this.Width - 16 - 26 - 5 * 2 - 2 * inter);//画椭圆的方法,x坐标、y坐标、宽、高,如果是100,则半径为50 gp.DrawEllipse(new Pen(Color.FromArgb(139, 129, 76)), 16 + 5 + inter * 2, 16 + 5 + inter*2, this.Width - 16 - 26 - 5 * 2 - 4 * inter, this.Width - 16 - 26 - 5 * 2 - 4 * inter);//画椭圆的方法,x坐标、y坐标、宽、高,如果是100,则半径为50 double fc = Math.Cos((valueNow * (Math.PI)) / 180); double fs = Math.Sin((valueNow * (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 CoordinateUc_Load(object sender, EventArgs e) { InitialCanvas(); } private void CoordinateUc_Resize(object sender, EventArgs e) { InitialCanvas(); } private void CoordinateUc_Paint(object sender, PaintEventArgs e) { DrawPin(e.Graphics); } } }