using System; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; using System.Drawing.Drawing2D; using Casic.Birmm.RbFreqStandMeasure.Properties; namespace Casic.Birmm.RbFreqStandMeasure { public partial class Coordinate : UserControl { private int x = 0; private int y = 0; [Description("角度值")] private int valueNow = 0; public int Value { get { return valueNow; } set { valueNow = value; } } /// <summary> /// 定义轴线长度 /// </summary> private float diameter; /// <summary> /// 每个间隔值 /// </summary> private int intervalValue; /// <summary> /// 仪表盘显示的最小值,默认为0 /// </summary> private float minValue = 0; /// <summary> /// 仪表盘显示的最小值 /// </summary> [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> [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 int X { get => x; set => x = value; } public int Y { get => y; set => y = value; } public Coordinate() { 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("W", scaleFont, brush, new Point(0, this.Height / 2 - 8)); gp.DrawString("N", scaleFont, brush, new Point(this.Width/2 - 13, 0)); gp.DrawString("E", scaleFont, brush, new Point(this.Width - 28 , this.Height / 2 - 8)); gp.DrawString("S", scaleFont, brush, new Point(this.Width/2 - 13, this.Height - 14)); int inter = 50; // 圆圈之间的间隔 Pen dashPen = new Pen(Color.Red, 2) { DashStyle = DashStyle.Custom, DashPattern = new float[] { 3f, 1f } }; gp.DrawEllipse(dashPen, 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 // 画点 X = 100; Y = 100; gp.DrawImage((Bitmap)Resources.ResourceManager.GetObject("USA"), X, Y); X = 200; Y = 300; gp.DrawImage((Bitmap)Resources.ResourceManager.GetObject("CHN"), 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); } } }