mapx使用经验(C#版)(未完成...)

本文介绍如何使用AxMapXLib.AxMap组件动态生成地图,并深入探讨了不同颜色模式之间的转换方法,包括从Argb到Bgr、Rgb到Bgr及Bgr到Rgb的转换。同时,文章还提及了在MapX和MapObjects组件中使用IFont表示字体的细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


一。可以动态生成Map
1None.gifAxMapXLib.AxMap map = new AxMapXLib.AxMap();
2None.gifmap.Layers.RemoveAll();
3None.gifmap.Parent = this;
4None.gifmap.Layers.Add(strFileName);


必须设置map.Parent = this;

否则不能显示。

二。很多组件使用标准动态链接库处理字体,颜色。
程序集为stdole.dll

stdole中使用BGR表示颜色。
而组件mapx,maplbjects很多地方使用RGB表示颜色
dotnet使用Argb表示颜色。

 1None.gifusing stdole;
 2None.gifuint ArgbToBgr(System.Drawing.Color argb)
 3ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 4InBlock.gifuint r = argb.R;
 5InBlock.gifuint g = argb.G;
 6InBlock.gifuint b = argb.B;
 7InBlock.gifuint color = r|(g<<8)|(b<<16);
 8InBlock.gifreturn color;
 9ExpandedBlockEnd.gif}

10None.gif
11None.gifuint RgbToBgr(uint rgb)
12ExpandedBlockStart.gifContractedBlock.gifdot.gif{
13InBlock.gifuint r = (rgb>>16)|0xFF;
14InBlock.gifuint g = (rgb>>8)|0xFF;
15InBlock.gifuint b = rgb|0xFF;
16InBlock.gifuint color = r|(g<<8)|(b<<16);
17InBlock.gifreturn color;
18ExpandedBlockEnd.gif}

19None.gifuint BgrToRgb(uint bgr)
20ExpandedBlockStart.gifContractedBlock.gifdot.gif{
21InBlock.gifreturn RgbToBgr(bgr);
22ExpandedBlockEnd.gif}

23None.gif


还有在mapx和mapobjects 中都用stdole.IFont 表示字体。
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 ESRI.ArcGIS.Carto; using ESRI.ArcGIS.Controls; using ESRI.ArcGIS.esriSystem; using ESRI.ArcGIS.Geometry; using ESRI.ArcGIS.Geodatabase; using ESRI.ArcGIS.Display; namespace WaterPipelineGIS2 { public partial class Form1 : Form { private IFeatureLayer _selectedFeatureLayer; private System.Drawing.Point _lastRightClickPosition; private enum SelectionMode { None, Rectangle, Circle, Polygon, Polyline } private SelectionMode currentMode = SelectionMode.None; public Form1() { InitializeComponent(); axMapControl1.OnMouseMove += new IMapControlEvents2_Ax_OnMouseMoveEventHandler(axMapControl1_OnMouseMove); } private void axMapControl1_OnMouseMove(object sender, IMapControlEvents2_OnMouseMoveEvent e) { // 获取地图坐标 double mapX = e.mapX; double mapY = e.mapY; // 格式化坐标显示(保留3位小数) lblCoordinate.Text = string.Format("X: {0:F3} Y: {1:F3}", mapX, mapY); // 立即刷新状态栏 statusStrip1.Refresh(); } private void toolStripMenuItem2_Click(object sender, EventArgs e) { using (var rotateForm = new RotateForm()) { if (rotateForm.ShowDialog() == DialogResult.OK) { try { axMapControl1.Rotation = rotateForm.RotationAngle; axMapControl1.ActiveView.Refresh(); // 可选:更新状态栏 lblCoordinate.Text += " 旋转角度:{rotateForm.RotationAngle}°"; } catch (Exception ex) { MessageBox.Show("旋转失败:{ex.Message}"); } } } } private void axTOCControl1_OnMouseDown(object sender, ITOCControlEvents_OnMouseDownEvent e) { if (e.button == 2) // 右键 { // 保存点击位置(控件坐标系) _lastRightClickPosition = new System.Drawing.Point(e.x, e.y); ITOCControl2 tocControl = (ITOCControl2)axTOCControl1.Object; // 修改点1:声明为接口类型并初始化为null IBasicMap basicMap = null; ILayer layer = null; // 修改点2:使用Type.Missing代替new object() object other = Type.Missing; object index = Type.Missing; esriTOCControlItem itemType = esriTOCControlItem.esriTOCControlItemNone; // 修改点3:正确传递ref参数 tocControl.HitTest(e.x, e.y, ref itemType, ref basicMap, ref layer, ref other, ref index); if (itemType == esriTOCControlItem.esriTOCControlItemLayer && layer != null) { contextMenuStripTOC.Show(axTOCControl1, e.x, e.y); } } } // 修改后(使用 MouseEventArgs) private void openAttributeTableToolStripMenuItem_Click(object sender, EventArgs e) { ITOCControl2 tocControl = (ITOCControl2)axTOCControl1.Object; IBasicMap basicMap = null; ILayer layer = null; object other = Type.Missing; object index = Type.Missing; esriTOCControlItem itemType = esriTOCControlItem.esriTOCControlItemNone; // 使用保存的控件坐标系位置 tocControl.HitTest( _lastRightClickPosition.X, _lastRightClickPosition.Y, ref itemType, ref basicMap, ref layer, ref other, ref index ); IFeatureLayer featureLayer = layer as IFeatureLayer; if (featureLayer != null) { _selectedFeatureLayer = featureLayer; // 确保使用正确的构造函数 AttributeTableForm attrForm = new AttributeTableForm( _selectedFeatureLayer, axMapControl1.Object as IMapControl2 ); attrForm.Show(); } else { MessageBox.Show("请选择有效的要素图层!"); } } private void SetSelectionSymbol() { // 使用接口创建符号 ISimpleFillSymbol fillSymbol = new SimpleFillSymbol() as ISimpleFillSymbol; fillSymbol.Color = GetRgbColor(255, 0, 0); fillSymbol.Style = esriSimpleFillStyle.esriSFSSolid; ISimpleLineSymbol lineSymbol = new SimpleLineSymbol() as ISimpleLineSymbol; lineSymbol.Color = GetRgbColor(255, 255, 0); lineSymbol.Width = 2; fillSymbol.Outline = lineSymbol; // 设置渲染器 if (_selectedFeatureLayer != null) { IGeoFeatureLayer geoLayer = (IGeoFeatureLayer)_selectedFeatureLayer; ISimpleRenderer renderer = new SimpleRenderer() as ISimpleRenderer; renderer.Symbol = (ISymbol)fillSymbol; geoLayer.Renderer = (IFeatureRenderer)renderer; axMapControl1.ActiveView.Refresh(); } } private IRgbColor GetRgbColor(int r, int g, int b) { IRgbColor color = new RgbColor() as IRgbColor; // 正确方式 color.Red = r; color.Green = g; color.Blue = b; return color; } public object _featureLayer { get; set; } // 矩形选择 private void btnRectSelect_Click(object sender, EventArgs e) { currentMode = SelectionMode.Rectangle; axMapControl1.CurrentTool = null; } // 圆形选择 private void btnCircleSelect_Click(object sender, EventArgs e) { currentMode = SelectionMode.Circle; axMapControl1.CurrentTool = null; } // 多边形选择 private void btnPolygonSelect_Click(object sender, EventArgs e) { currentMode = SelectionMode.Polygon; axMapControl1.CurrentTool = null; } // 折线选择 private void btnLineSelect_Click(object sender, EventArgs e) { currentMode = SelectionMode.Polyline; axMapControl1.CurrentTool = null; } // 地图鼠标按下事件 private void axMapControl1_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e) { try { IGeometry geometry = null; switch (currentMode) { case SelectionMode.Rectangle: // 绘制矩形 geometry = axMapControl1.TrackRectangle(); break; case SelectionMode.Circle: // 获取用户拖拽的矩形范围 IEnvelope envelope = axMapControl1.TrackRectangle(); // 创建圆心点 IPoint center = new PointClass(); center.PutCoords(envelope.XMin + envelope.Width / 2, envelope.YMin + envelope.Height / 2); // 正确创建圆形几何(修复部分) ICircularArc circularArc = new CircularArcClass(); IConstructCircularArc constructArc = (IConstructCircularArc)circularArc; double radius = Math.Min(envelope.Width, envelope.Height) / 2; constructArc.ConstructCircle(center, radius, true); // 创建多边形表示圆形 IGeometryCollection geomColl = new PolygonClass(); geomColl.AddGeometry((IGeometry)circularArc); geometry = (IGeometry)geomColl; geometry.SpatialReference = axMapControl1.SpatialReference; break; case SelectionMode.Polygon: // 绘制多边形 geometry = axMapControl1.TrackPolygon(); break; case SelectionMode.Polyline: // 绘制折线 geometry = axMapControl1.TrackLine(); break; } if (geometry != null) { PerformSpatialSelection(geometry); HighlightGeometry(geometry); } } catch (Exception ex) { MessageBox.Show("操作失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } // 执行空间选择 private void PerformSpatialSelection(IGeometry geometry) { IMap map = axMapControl1.Map; // 创建空间过滤器 ISpatialFilter spatialFilter = new SpatialFilterClass(); spatialFilter.Geometry = geometry; spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects; // 清除之前的选择 map.ClearSelection(); // 遍历所有图层执行选择 for (int i = 0; i < map.LayerCount; i++) { IFeatureLayer featureLayer = map.get_Layer(i) as IFeatureLayer; if (featureLayer == null) continue; spatialFilter.GeometryField = featureLayer.FeatureClass.ShapeFieldName; IFeatureSelection featureSelection = (IFeatureSelection)featureLayer; featureSelection.SelectFeatures(spatialFilter, esriSelectionResultEnum.esriSelectionResultNew, false); } axMapControl1.Refresh(esriViewDrawPhase.esriViewGeoSelection, null, null); } // 高亮显示选择图形 private void HighlightGeometry(IGeometry geometry) { IMap map = axMapControl1.Map; IActiveView activeView = map as IActiveView; IGraphicsContainer graphicsContainer = map as IGraphicsContainer; // 清除之前的高亮 graphicsContainer.DeleteAllElements(); // 创建高亮符号 ISimpleFillSymbol fillSymbol = new SimpleFillSymbolClass(); fillSymbol.Color = GetRGBColor(255, 0, 0, 50); // 半透明红色 fillSymbol.Outline.Color = GetRGBColor(0, 0, 255); ISimpleLineSymbol lineSymbol = new SimpleLineSymbolClass(); lineSymbol.Color = GetRGBColor(0, 0, 255); lineSymbol.Width = 2; // 创建元素 IElement element = null; if (geometry.GeometryType == esriGeometryType.esriGeometryPolygon) { IFillShapeElement fillElement = new PolygonElementClass(); fillElement.Symbol = fillSymbol; element = (IElement)fillElement; } else if (geometry.GeometryType == esriGeometryType.esriGeometryPolyline) { ILineElement lineElement = new LineElementClass(); lineElement.Symbol = lineSymbol; element = (IElement)lineElement; } else if (geometry.GeometryType == esriGeometryType.esriGeometryPoint) { // 圆形选择使用点符号 IMarkerElement markerElement = new MarkerElementClass(); ISimpleMarkerSymbol markerSymbol = new SimpleMarkerSymbolClass(); markerSymbol.Color = fillSymbol.Color; markerSymbol.Size = 10; markerElement.Symbol = markerSymbol; element = (IElement)markerElement; } if (element != null) { element.Geometry = geometry; graphicsContainer.AddElement(element, 0); activeView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null); } } // 创建颜色对象 private IRgbColor GetRGBColor(int r, int g, int b, int alpha = 255) { IRgbColor color = new RgbColorClass(); color.Red = r; color.Green = g; color.Blue = b; color.Transparency = (byte)(255 - alpha); return color; } // 清除选择 private void btnClearSelection_Click(object sender, EventArgs e) { axMapControl1.Map.ClearSelection(); IGraphicsContainer graphicsContainer = axMapControl1.Map as IGraphicsContainer; graphicsContainer.DeleteAllElements(); axMapControl1.Refresh(); } } }还是点击空间选择中的多边形选择、圆形选择等无法进行选择呢,如何结局
07-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值