用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);
            }
        }
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章