用C#实现桌面GIS系列--实现点的绘制

实现点的绘制

  • 不妨先来看看效果
    在这里插入图片描述
  • 基本思路
    • 在绘制按钮的点击事件里获取三个TextBox的内容,然后新建相应的点类,进行绘制
    private void button1_Click(object sender, EventArgs e)
         {
             double x = Convert.ToDouble(textBox1.Text); //
             double y = Convert.ToDouble(textBox2.Text);
             GISVertext location = new GISVertext(x, y);
             string attribute = textBox3.Text;
             GISPoint point = new GISPoint(location, attribute);
             Graphics graphics = this.CreateGraphics();
             point.drawAttribute(graphics);
             point.drawLocation(graphics);
         }
    
    • 下面是用到的两个类,放在BasicClassices.cs下
namespace CalfGIS
{
    class GISVertext //节点
    {
        public double x;
        public double y;

        public GISVertext(double x, double y)
        {
            this.x = x;
            this.y = y;
        }
    }
    class GISPoint
    {
        public GISVertext location;
        public string attribute;

        public GISPoint(GISVertext location, string attribute)
        {
            this.location = location;
            this.attribute = attribute;
        }

        public void drawLocation(Graphics graphics)
        {
            graphics.FillEllipse(new SolidBrush(Color.Red),
                (int)location.x - 3, (int)location.y - 3, 6, 6);
        }

        public void drawAttribute(Graphics graphics)
        {
            graphics.DrawString(attribute, new Font("宋体", 20),
                new SolidBrush(Color.Green), new Point((int)location.x, (int)location.y));
        }
    }
}

  • 上述代码相对简单,不详讲,读者认真看应该问题不大,有问题可以留言

下面添加捕捉功能

  • 思路很简单,在Form1里新建一个List points, 来存储所有绘制的点,然后监听鼠标的点击事件,获取点击的x,y值,与points里每个点计算距离,距离最小且距离不超过5个像素的点被捕捉
  • 以下是全部代码
//BasicClasses.cs
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CalfGIS
{
    class GISVertext //节点
    {
        public double x;
        public double y;

        public GISVertext(double x, double y)
        {
            this.x = x;
            this.y = y;
        }

        public double distance(GISVertext anotherVertex) //节点
        {
            return Math.Sqrt((x - anotherVertex.x) * (x - anotherVertex.x) +
                (y - anotherVertex.y) * (y- anotherVertex.y));
        }
    }
    class GISPoint
    {
        public GISVertext location;
        public string attribute;

        public GISPoint(GISVertext location, string attribute)
        {
            this.location = location;
            this.attribute = attribute;
        }

        public void drawLocation(Graphics graphics)
        {
            graphics.FillEllipse(new SolidBrush(Color.Red),
                (int)location.x - 3, (int)location.y - 3, 6, 6);
        }

        public void drawAttribute(Graphics graphics)
        {
            graphics.DrawString(attribute, new Font("宋体", 20),
                new SolidBrush(Color.Green), new Point((int)location.x, (int)location.y));
        }
    }
}

using CalfGIS;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace lesson1
{
    public partial class Form1 : Form
    {
        List<GISPoint> points = new List<GISPoint>();
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            double x = Convert.ToDouble(textBox1.Text);  //获取x
            double y = Convert.ToDouble(textBox2.Text);  //获取y
            GISVertext location = new GISVertext(x, y);  
            string attribute = textBox3.Text; //属性
            GISPoint point = new GISPoint(location, attribute);

            Graphics graphics = this.CreateGraphics(); //获取绘图对象
            point.drawAttribute(graphics); //绘制点属性
            point.drawLocation(graphics);  //绘制点的位置

            points.Add(point);
        }

        private void Form1_MouseClick(object sender, MouseEventArgs e)
        {
            GISVertext clickVertex = new GISVertext(e.X, e.Y);
            double minDistance = double.MaxValue; //最小距离
            int minIndex = -1; //记录距离最小点所在下标
            for(int i = 0; i < points.Count; i++)
            {
                double distance = points[i].location.distance(clickVertex);
                if(distance < minDistance)
                {
                    minDistance = distance;
                    minIndex = i;
                }
            }
            if(minIndex == -1)
            {
                MessageBox.Show("屏幕上没有点!");
            }
            else
            {
                MessageBox.Show("被选择的点是:" + points[minIndex].location.x.ToString()
                    + points[minIndex].location.y.ToString() + points[minIndex].attribute);
            }
        }
    }
}

  • 5
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值