陌生的不再陌生

导航

DotSpatial 自定义MapFunction_mapMain中绘制线(未添加到图层中)

using DotSpatial.Controls;
using DotSpatial.Symbology;
using GeoAPI.Geometries;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DotSpatial20Test.LzqOP
{
    public class Lzq_FunctionClipPolygon : MapFunction
    {
        #region 定义变量

        private List<Coordinate> _coordinates;//折线段的集合
        private System.Drawing.Point _mousePosition;//鼠标位置
        private List<List<Coordinate>> _previousParts;//这次绘制前折线段点集合
        private bool _standBy;//判断是否注销当前工具       

        #endregion
/// <summary> /// 构造函数 /// </summary> /// <param name="map"></param> public Lzq_FunctionClipPolygon(IMap map) : base(map) { _previousParts = new List<List<Coordinate>>();//初始化 _coordinates = new List<Coordinate>(); YieldStyle = YieldStyles.LeftButton | YieldStyles.RightButton; if (map != null) { (map as Control).MouseLeave += MapMouseLeave; } Name = "绘制线"; } /// <summary> /// 鼠标离开控制区域 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void MapMouseLeave(object sender, EventArgs e) { Map.Invalidate(); } /// <summary> /// 激活Function /// </summary> protected override void OnActivate() { if(!_standBy) //非注销状态 { _previousParts = new List<List<Coordinate>>(); _coordinates = new List<Coordinate>(); } _standBy = false; base.OnActivate(); } /// <summary> /// 注销 /// </summary> protected override void OnDeactivate() { if(_standBy) { return; } _standBy = true; Map.Invalidate(); //base.OnDeactivate(); } /// <summary> /// 鼠标移动 /// </summary> /// <param name="e"></param> protected override void OnMouseMove(GeoMouseArgs e) { //注销后,跳过 if(_standBy) { return; } //无坐标时,跳过 if(_coordinates==null||_coordinates.Count==0) { return; } //鼠标位置点 Coordinate c1 = e.GeographicLocation; //当点数大于0时 if(_coordinates.Count>0) { //将地理坐标转换为屏幕坐标 List<Point> points = _coordinates.Select(coord => Map.ProjToPixel(coord)).ToList(); //获取鼠标上一个位置和最后一个点的矩形区域 Rectangle oldRect = SymbologyGlobal.GetRectangle(_mousePosition, points[points.Count - 1]); //获取鼠标和左后一个点矩形区域 Rectangle newRect = SymbologyGlobal.GetRectangle(e.Location, points[points.Count - 1]); //合并区域 Rectangle invalidRect = Rectangle.Union(newRect, oldRect); //刷新区域 invalidRect.Inflate(220, 20); Map.Invalidate(invalidRect); } //设置鼠标位置 _mousePosition = e.Location; base.OnMouseMove(e); } /// <summary> /// 鼠标弹起 /// </summary> /// <param name="e"></param> protected override void OnMouseUp(GeoMouseArgs e) { //注销,跳过 if (_standBy) { return; } //右键结束此次操作,开始下次操作 if(e.Button ==MouseButtons.Right) { _coordinates = new List<Coordinate>();//坐标点集清空 //Map.Invalidate(); //base.OnDeactivate(); } else { _coordinates.Add(e.GeographicLocation);//将当前鼠标点添加至坐标点集中 Map.Invalidate(); } base.OnMouseUp(e); } /// <summary> /// 绘制地图 /// </summary> /// <param name="e"></param> protected override void OnDraw(MapDrawArgs e) { //当前鼠标在屏幕坐标系下的坐标 Point mouseTest = Map.PointToClient(Control.MousePosition); //鼠标是否在地图内 bool hasMouseInMapCtrl = Map.ClientRectangle.Contains(mouseTest); #region 定义绘图工具 画笔等 //画笔——红色 Pen penRed = new Pen(Color.Red, 2F); //画笔——蓝色 Pen penBlue = new Pen(Color.Blue, 2F); //画刷——红色 Brush brushRed = new SolidBrush(Color.Red); //画刷——蓝色 Brush brushBlue = new SolidBrush(Color.FromArgb(60, 0, 0, 255)); //字体 Font drawFont = new Font("Arial", 12); //抗锯齿 e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; #endregion //点序列 List<Point> points = new List<Point>(); //绘制当前点集 if(_coordinates!=null) { //获取此次绘制的所有点 foreach(Coordinate coord in _coordinates) { points.Add(Map.ProjToPixel(coord));//地理坐标转屏幕坐标 } //点集数量>1 if(points.Count>1) { //绘制折线、节点 for(int i=0;i<points.Count-1;i++) { Point cp = points[i]; Point np = points[i + 1]; e.Graphics.DrawLine(penBlue, cp, np); e.Graphics.FillRectangle(brushRed, new Rectangle(cp.X - 2, cp.Y - 2, 4, 4)); } } //点击数量>0时,绘制鼠标移动所构成的那条线 if (points.Count > 0 && _standBy == false && hasMouseInMapCtrl) { e.Graphics.DrawLine(penRed, points[points.Count - 1], _mousePosition); } } //释放变量 penBlue.Dispose(); penRed.Dispose(); brushRed.Dispose(); brushBlue.Dispose(); drawFont.Dispose(); base.OnDraw(e); } } }

  

调用方法:

mapMain.Cursor = Cursors.Cross;
            var lzqFunctionClipPolygon = new Lzq_FunctionClipPolygon(mapMain);
            mapMain.ActivateMapFunction(lzqFunctionClipPolygon);

 

posted on 2020-02-19 12:03  陌生的不再陌生  阅读(861)  评论(1编辑  收藏  举报