必威体育Betway必威体育官网
当前位置:首页 > IT技术

C#绘制GPS星空图

时间:2019-06-12 10:43:10来源:IT技术作者:seo实验室小编阅读:63次「手机版」
 

星空图

搜了一下,网上关于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模块数据读取的部分就暂时不贴了,我自己的写得比较复杂,网上有很多简单的代码。

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

相关阅读

豆瓣时间:仰望星空与柴米油盐之间的妥协和博弈

三、运营思路分析豆瓣时间沿袭了豆瓣的一贯运营风格,在整体的营销推广上没有过多的动作,从内容运营本身入手,扎实的靠内容吸引用户,下

分享到:

栏目导航

推荐阅读

热门阅读