刚刚接触GIS方面的东西,学习的是mapx的东西,经理叫写一下鹰眼,算是检查学习的情况,网上看来看去,用C#+Mapx开发的好像真的不多,只能找到VB的代码,然后看完再写,大的思路没有问题,可是小地方的问题真的很多啊,把自己的代码贴一下,思路是别人,不好意思,还有一些其他的小功能,还希望各位高手们能指点一二。
首先要加几个类属性:
private double m_distance = 0.0;
private double m_totalDis = 0.0;
private bool m_toolBarFlag = true;
MapXLib.Layer m_lyrEngle;
MapXLib.Feature m_feaEagle;
bool m_bLeftButtonControl = false;
代码如下:思路应该很容易看清了。
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using MapXLib;
- namespace firstMapX
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- // ToolStrip上各个button的鼠标响应事件
- private void toolStripBtnZoomIn_Click(object sender, EventArgs e)
- {
- axMapControl.CurrentTool = MapXLib.ToolConstants.miZoomInTool;
- }
- private void toolStripBtnZoomOut_Click(object sender, EventArgs e)
- {
- axMapControl.CurrentTool = MapXLib.ToolConstants.miZoomOutTool;
- }
- private void toolStripBtnPan_Click(object sender, EventArgs e)
- {
- axMapControl.CurrentTool = MapXLib.ToolConstants.miPanTool;
- }
- private void toolStripBtnDistance_Click(object sender, EventArgs e)
- {
- axMapControl.CreateCustomTool(100, MapXLib.ToolTypeConstants.miToolTypePoly,
- MapXLib.CursorConstants.miCrossCursor, null, null, null);
- axMapControl.CurrentTool = (MapXLib.ToolConstants)100;
- }
- private void toolStripBtnCenter_Click(object sender, EventArgs e)
- {
- axMapControl.CurrentTool = MapXLib.ToolConstants.miCenterTool;
- }
- // 测距的方法
- private void axMapControl_PolyToolUsed(object sender, AxMapXLib.CMapXEvents_PolyToolUsedEvent e)
- {
- MapXLib.Points pts = new MapXLib.PointsClass();
- switch (e.flags)
- {
- case (int) MapXLib.ToolFlagConstants.miToolInProgress :
- pts = (MapXLib.Points)e.points;
- m_distance = axMapControl.Distance(pts._Item(pts.Count - 1).X,
- pts._Item(pts.Count - 1).Y,
- pts._Item(pts.Count).X,
- pts._Item(pts.Count).Y);
- m_totalDis += m_distance;
- break;
- default:
- m_distance = 0;
- m_totalDis = 0;
- break;
- }
- labelDis.Text = "Distance:"
- + m_distance.ToString("#.00 kilometer");
- labelTotalDis.Text = "TotalDistance:"
- + m_totalDis.ToString("#.00kilometer");
- }
- // open GeoSet file
- private void openGSTToolStripMenuItem_Click(object sender, EventArgs e)
- {
- openFileDialog1.InitialDirectory = "C://Program Files//MapInfo//MapX 5.0//Maps";
- openFileDialog1.Filter = "gst files (*.gst)|*.gst|All files (*.*)|*.*";
- openFileDialog1.FilterIndex = 0;
- openFileDialog1.RestoreDirectory = true;
- if (openFileDialog1.ShowDialog() == DialogResult.OK)
- if ((openFileDialog1.OpenFile()) != null)
- {
- axMapControl.GeoSet = openFileDialog1.FileName;
- axMapEagle.GeoSet = openFileDialog1.FileName;
- axMapControl.Title.Visible = false;
- axMapEagle.Title.Visible = false;
- axMapEagle.Zoom = 150;
- }
- axMapEagle_CreateTempLayer();
- }
- // close GeoSet file
- private void closeGSTToolStripMenuItem_Click(object sender, EventArgs e)
- {
- axMapControl.Layers.RemoveAll();
- axMapEagle.Layers.RemoveAll();
- }
- // exit
- private void exitToolStripMenuItem_Click(object sender, EventArgs e)
- {
- Dispose();
- }
- // Zoom In
- private void zoomInToolStripMenuItem_Click(object sender, EventArgs e)
- {
- axMapControl.CurrentTool = MapXLib.ToolConstants.miZoomInTool;
- }
- // Zoom Out
- private void zoomOutToolStripMenuItem_Click(object sender, EventArgs e)
- {
- axMapControl.CurrentTool = MapXLib.ToolConstants.miZoomOutTool;
- }
- // Pan
- private void panToolStripMenuItem_Click(object sender, EventArgs e)
- {
- axMapControl.CurrentTool = MapXLib.ToolConstants.miPanTool;
- }
- // Center
- private void centerToolStripMenuItem_Click(object sender, EventArgs e)
- {
- axMapControl.CurrentTool = MapXLib.ToolConstants.miCenterTool;
- }
- // toolBar View
- private void toolBarToolStripMenuItem_Click(object sender, EventArgs e)
- {
- if (m_toolBarFlag)
- {
- toolStrip1.Visible = false;
- m_toolBarFlag = false;
- toolBarToolStripMenuItem.Checked = false;
- }
- else
- {
- toolStrip1.Visible = true;
- m_toolBarFlag = true;
- toolBarToolStripMenuItem.Checked = true;
- }
- }
- // Layer Control
- private void layerControlToolStripMenuItem_Click(object sender, EventArgs e)
- {
- axMapControl.Layers.LayersDlg("", "");
- }
- // Create the temp layer on eagle map
- private void axMapEagle_CreateTempLayer()
- {
- try
- {
- MapXLib.LayerInfo tempLayerInfo = new MapXLib.LayerInfoClass();
- MapXLib.Fields flds = new MapXLib.FieldsClass();
- flds.AddStringField("Name", 100, false);
- flds.AddStringField("Id", 20, false);
- tempLayerInfo.Type = MapXLib.LayerInfoTypeConstants.miLayerInfoTypeTemp;
- tempLayerInfo.AddParameter("Name", "Eagle");
- tempLayerInfo.AddParameter("Fields", flds);
- m_lyrEngle = axMapEagle.Layers.Add(tempLayerInfo, 1);
- //axMapEagle.Layers.LayersDlg("", "");
- m_lyrEngle = null;
- }
- catch (System.Exception e)
- {
- MessageBox.Show(e.ToString());
- }
- }
- // Handle Eagle Map MouseMove Event
- private void axMapEagle_MouseMoveEvent(object sender, AxMapXLib.CMapXEvents_MouseMoveEvent e)
- {
- double dbMapX = 0.0;
- double dbMapY = 0.0;
- Single sngScreenX = 0;
- Single sngScreenY = 0;
- if (m_bLeftButtonControl)
- {
- sngScreenX = e.x;
- sngScreenY = e.y;
- axMapEagle.ConvertCoord(ref sngScreenX, ref sngScreenY,
- ref dbMapX, ref dbMapY, MapXLib.ConversionConstants.miScreenToMap);
- axMapControl.CenterX = dbMapX;
- axMapControl.CenterY = dbMapY;
- }
- }
- // Handle Eagle Map MouseUp Event
- private void axMapEagle_MouseUpEvent(object sender, AxMapXLib.CMapXEvents_MouseUpEvent e)
- {
- m_bLeftButtonControl = false;
- }
- // Handle Eagle Map MouseDown Event
- private void axMapEagle_MouseDownEvent(object sender, AxMapXLib.CMapXEvents_MouseDownEvent e)
- {
- double dbMapX = 0.0;
- double dbMapY = 0.0;
- Single sngScreenX = 0;
- Single sngScreenY = 0;
- m_bLeftButtonControl = true;
- sngScreenX = e.x;
- sngScreenY = e.y;
- axMapEagle.ConvertCoord(ref sngScreenX, ref sngScreenY,
- ref dbMapX, ref dbMapY, MapXLib.ConversionConstants.miScreenToMap);
- axMapControl.CenterX = dbMapX;
- axMapControl.CenterY = dbMapY;
- }
- // Handle the Control Map's MapViewChanged Event
- private void axMapControl_MapViewChanged(object sender, EventArgs e)
- {
- MapXLib.Feature tempFea = new MapXLib.FeatureClass();
- MapXLib.Style tempStyle = new MapXLib.StyleClass();
- MapXLib.Points tempPoints = new MapXLib.PointsClass();
- MapXLib.Point ptRectMap1 = new MapXLib.PointClass();
- MapXLib.Point ptRectMap2 = new MapXLib.PointClass();
- MapXLib.Point ptRectMap3 = new MapXLib.PointClass();
- MapXLib.Point ptRectMap4 = new MapXLib.PointClass();
- // 设置矩形框的四角
- ptRectMap1.Set(axMapControl.CtlBounds.XMin, axMapControl.CtlBounds.YMin);
- ptRectMap2.Set(axMapControl.CtlBounds.XMax, axMapControl.CtlBounds.YMin);
- ptRectMap3.Set(axMapControl.CtlBounds.XMax, axMapControl.CtlBounds.YMax);
- ptRectMap4.Set(axMapControl.CtlBounds.XMin, axMapControl.CtlBounds.YMax);
- tempPoints.Add(ptRectMap1, 1);
- tempPoints.Add(ptRectMap2, 2);
- tempPoints.Add(ptRectMap3, 3);
- tempPoints.Add(ptRectMap4, 4);
- try
- {
- // 矩形边框不存在时
- if (axMapEagle.Layers._Item(1).AllFeatures.Count == 0)
- {
- // 设置边框样式
- tempStyle.RegionPattern = MapXLib.FillPatternConstants.miPatternNoFill;
- tempStyle.RegionBorderColor = (uint)MapXLib.ColorConstants.miColorBlue;
- tempStyle.RegionBorderWidth = 2;
- // 在临时图层添加大小为axMapxControl的边界的Rectangle对象
- try
- {
- tempFea = axMapEagle.FeatureFactory.CreateRegion(tempPoints, tempStyle);
- m_feaEagle = axMapEagle.Layers._Item("Eagle").AddFeature(tempFea, new MapXLib.RowValuesClass());
- tempStyle = null;
- }
- catch (System.Exception ex)
- {
- MessageBox.Show(ex.ToString());
- }
- }
- // 矩形框存在,根据axMapXControl的视野变化改变矩形边框的大小和位置
- else
- {
- try
- {
- m_feaEagle.Parts._Item(1).RemoveAll();
- m_feaEagle.Parts._Item(1).AddXY(axMapControl.CtlBounds.XMin, axMapControl.CtlBounds.YMin, 1);
- m_feaEagle.Parts._Item(1).AddXY(axMapControl.CtlBounds.XMax, axMapControl.CtlBounds.YMin, 2);
- m_feaEagle.Parts._Item(1).AddXY(axMapControl.CtlBounds.XMax, axMapControl.CtlBounds.YMax, 3);
- m_feaEagle.Parts._Item(1).AddXY(axMapControl.CtlBounds.XMin, axMapControl.CtlBounds.YMax, 4);
- m_feaEagle.Update(m_feaEagle, new MapXLib.RowValuesClass());
- }
- catch (System.Exception ex)
- {
- MessageBox.Show(ex.ToString());
- }
- }
- }
- catch (System.Exception exx)
- {
- MessageBox.Show(exx.ToString());
- }
- }
- }
- }