C#绘制GPS星空图

搜了一下,网上关于C#绘制星空图的代码比较少,且把我自己做的一个简单的C#星空图控件代码贴出来,供大家一起交流……

首先是SatelliteInfo类,这个是用来存放卫星信息数据的。完整代码如下:

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace DBF.Controls
{
    /// <summary>
    /// 卫星信息
    /// </summary>
    public class SatelliteInfo
    {
        /// <summary>
        /// 名称
        /// </summary>
        public string Name = "";
        /// <summary>
        /// 仰角
        /// </summary>
        public double Elevation = 0d;
        /// <summary>
        /// 方位角
        /// </summary>
        public double Azimuth = 0d;
        /// <summary>
        /// 符号颜色
        /// </summary>
        public Color Color = Color.Red;
    }
}

接下来就是要创建自定义控件了,创建一个名为SkyChartBox的自定义控件,代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DBF.Controls
{
    /// <summary>
    /// 星空图控件
    /// </summary>
    public partial class SkyChartBox : Control
    {
        SatelliteInfo[] stlArray = new SatelliteInfo[0];    //卫星信息数组

        public SkyChartBox()
        {
            InitializeComponent();
            base.DoubleBuffered = true;
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
            //取正方形
            Rectangle myRect = new Rectangle();
            myRect.Width = Math.Min(pe.ClipRectangle.Width, pe.ClipRectangle.Height);
            myRect.Height = myRect.Width;
            myRect.X = (pe.ClipRectangle.Width - myRect.Width) / 2;
            myRect.Y = (pe.ClipRectangle.Height - myRect.Height) / 2;
            pe.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            //绘制圆圈
            int pad = 20;
            Pen myPen = new Pen(base.ForeColor, 1f);
            pe.Graphics.DrawEllipse(myPen, pad + myRect.X, pad + myRect.Y, myRect.Width - pad * 2, myRect.Height - pad * 2);
            pe.Graphics.DrawEllipse(myPen, pad + myRect.X + (myRect.Width - pad * 2) / 6, pad + myRect.Y + (myRect.Height - pad * 2) / 6, (myRect.Width - pad * 2) / 3 * 2, (myRect.Height - pad * 2) / 3 * 2);
            pe.Graphics.DrawEllipse(myPen, pad + myRect.X + (myRect.Width - pad * 2) / 6 * 2, pad + myRect.Y + (myRect.Height - pad * 2) / 6 * 2, (myRect.Width - pad * 2) / 3, (myRect.Height - pad * 2) / 3);
            //绘制线条
            pe.Graphics.DrawLine(myPen, myRect.X + pad, myRect.Y + myRect.Height / 2, myRect.Right - pad, myRect.Y + myRect.Height / 2);
            pe.Graphics.DrawLine(myPen, myRect.X + myRect.Width / 2, myRect.Y + pad, myRect.X + myRect.Width / 2, myRect.Bottom - pad);
            //绘制注记
            SolidBrush myBrush = new SolidBrush(base.ForeColor);
            pe.Graphics.DrawString("0°", base.Font, myBrush, myRect.X + myRect.Width / 2 + myPen.Width, myRect.Y + pad + myPen.Width);
            pe.Graphics.DrawString("30°", base.Font, myBrush, myRect.X + myRect.Width / 2 + myPen.Width, myRect.Y + pad + (myRect.Height - pad * 2) / 6 + myPen.Width);
            pe.Graphics.DrawString("60°", base.Font, myBrush, myRect.X + myRect.Width / 2 + myPen.Width, myRect.Y + pad + (myRect.Height - pad * 2) / 6 * 2 + myPen.Width);
            pe.Graphics.DrawString("N", base.Font, myBrush, myRect.X + myRect.Width / 2 - base.Font.SizeInPoints / 2, myRect.Y + pad - base.Font.Height);
            pe.Graphics.DrawString("S", base.Font, myBrush, myRect.X + myRect.Width / 2 - base.Font.SizeInPoints / 2, myRect.Bottom - pad);
            pe.Graphics.DrawString("W", base.Font, myBrush, myRect.Left + pad - base.Font.SizeInPoints, myRect.Y + myRect.Height / 2 - base.Font.Height / 2);
            pe.Graphics.DrawString("E", base.Font, myBrush, myRect.Right - pad, myRect.Y + myRect.Height / 2 - base.Font.Height / 2);
            //绘制卫星
            int stlSize = 4;
            for (int i = 0; i < stlArray.Length; i++)
            {
                Point stlPoint = new Point();
                stlPoint.X = (int)(Math.Sin(stlArray[i].Azimuth) * stlArray[i].Elevation / Math.PI * 2 * (myRect.Width / 2 - pad) + myRect.X + myRect.Width / 2);
                stlPoint.Y = (int)(myRect.Y + myRect.Height / 2 - Math.Cos(stlArray[i].Azimuth) * stlArray[i].Elevation / Math.PI * 2 * (myRect.Width / 2 - pad));
                pe.Graphics.FillEllipse(new SolidBrush(stlArray[i].Color), stlPoint.X - stlSize / 2, stlPoint.Y - stlSize / 2, stlSize, stlSize);
                pe.Graphics.DrawString(stlArray[i].Name, base.Font, new SolidBrush(stlArray[i].Color), stlPoint.X + stlSize / 2, stlPoint.Y - base.Font.Height / 2);
            }
            myPen.Dispose();
            myBrush.Dispose();
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            base.Refresh();
        }

        /// <summary>
        /// 获取和设置卫星信息
        /// </summary>
        public SatelliteInfo[] StlInfo
        {
            get { return this.stlArray; }
            set
            {
                this.stlArray = value;
                this.Refresh();
            }
        }
    }
}

GPS模块数据读取的部分就暂时不贴了,我自己的写得比较复杂,网上有很多简单的代码。

控件最终的运行效果如下:

评论 21
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

八两

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值