以下总结了ae开发中常用的控件加载方法,包括地图控件AxMapControl,鹰眼,内容列表控件TOCControl以及打印视图AxPageLayoutControl。
需要强调的是,目前网上的开发教程中,内容列表移除图层几乎都没有包含图层的解锁,本文对此进行了优化补充。
另外,在日常开发过程中,为了降低耦合,建议各个控件分为几个页面写,不要全部堆在一个页面。
1.wpf界面配置
在wpf中配置以上控件都是插入WindowsFormsHost的子控件,界面配置如下所示:
<Grid>
<WindowsFormsHost x:Name="mapHost" Background="White"/>
</Grid>
若是winform开发,可直接如普通控件那般插入
2.地图视图
#region 地图视图AxMapControl
public AxMapControl MapControl = new AxMapControl();
public AxTOCControl TocControl = new AxTOCControl();
public AxMapControl OverviewMapControl = new AxMapControl();
//鹰眼轮廓线
public IEnvelope m_pEnvelope = null;
//选择按钮
private readonly ControlsSelectFeaturesToolClass m_selectTool = new ControlsSelectFeaturesToolClass();
/// <summary>
/// 比例尺变化通知
/// </summary>
public event EventHandler<dynamic> OnMapRefresh;
public MapControlPage()
{
InitializeComponent();
mapHost.Child = MapControl;
MapControl.OnMouseDown += MainMapControl1_OnMouseDown;
MapControl.OnDoubleClick += MainMapControl1_OnDoubleClick;
MapControl.OnMouseMove += MainMapControl_OnMouseMove;
MapControl.OnExtentUpdated += MainMapControl_OnExtentUpdated;
MapControl.OnAfterDraw += MapControl_OnOnAfterDraw;
//修改选中颜色
ISelectionEnvironment environment = new SelectionEnvironmentClass();
environment.DefaultColor = new RgbColorClass { Red = 255, Green = 255, Blue = 0 };
//mapcontrol右键菜单
var mapContextMenuStrip = new ContextMenuStrip();
mapContextMenuStrip.Items.Add("放大地图",
Image.FromFile(PathHelper.GetCurrentRootPath() + "images\\工具\\放大地图.png"), AxMapControl_ZoomIn);
mapContextMenuStrip.Items.Add("缩小地图",
Image.FromFile(PathHelper.GetCurrentRootPath() + "images\\工具\\缩小地图.png"), AxMapControl_ZoomOut);
MapControl.ContextMenuStrip = mapContextMenuStrip;
}
private void MapControl_OnOnAfterDraw(object sender, IMapControlEvents2_OnAfterDrawEvent e)
{
OnMapRefresh?.Invoke(sender, MapControl.MapScale);
}
private void MapControlPage_OnLoaded(object sender, RoutedEventArgs e)
{
((ISupportInitialize)MapControl).BeginInit();
//隐藏地图的滚动条
MapControl.ShowScrollbars = false;
ControlsSynchronizer.MapPage = this;
((ISupportInitialize)MapControl).EndInit();
InitializeControls();
}
/// <summary>
/// 地图中点击鼠标事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MainMapControl1_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e)
{
//左键
if (e.button == 1)
{
}
//右键
else if (e.button == 2)
{
}
//中键
else if (e.button == 4)
{
}
}
private void MainMapControl1_OnDoubleClick(object sender, IMapControlEvents2_OnDoubleClickEvent e)
{
}
private IElement element;
/// <summary>
/// 地图鼠标移动
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MainMapControl_OnMouseMove(object sender, IMapControlEvents2_OnMouseMoveEvent e)
{
}
/// <summary>
/// 主窗体视图范围变化,对鹰眼视图进行画框
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MainMapControl_OnExtentUpdated(object sender, IMapControlEvents2_OnExtentUpdatedEvent e)
{
DrawRectangle(MapControl.Extent);
}
#endregion
3. 鹰眼
#region 鹰眼
/// <summary>
/// 在鹰眼视图中绘画矩形框
/// </summary>
/// <param name="pEnvelop"></param>
private void DrawRectangle(IEnvelope pEnvelop)
{
if (pEnvelop == null)
return;
if (OverviewMapControl.Map.LayerCount > 0)
{
var pGraphicsContainer = OverviewMapControl.Map as IGraphicsContainer;
var pActiveView = pGraphicsContainer as IActiveView;
//在绘制前。清除之前的图形元素
pGraphicsContainer.Reset();
var oldElement = pGraphicsContainer.Next();
IEnvelope oldEnvelope = null;
if (oldElement != null)
{
oldEnvelope = oldElement.Geometry.Envelope;
pGraphicsContainer.DeleteAllElements();
OverviewMapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewForeground, null, OverviewMapControl.ActiveView.Extent);
}
IRectangleElement pRectangleEle = new RectangleElementClass();
var pElement = pRectangleEle as IElement;
pElement.Geometry = pEnvelop;
//实例化符号(Symbol)对象
IRgbColor rgbColor = new RgbColorClass();
rgbColor.Red = 255;
IColor color = rgbColor;
ISimpleFillSymbol pSimpleFillSymbol = new SimpleFillSymbolClass();
ILineSymbol pOutLine = new SimpleLineSymbolClass();
pOutLine.Width = 2;
pOutLine.Color = color;
pSimpleFillSymbol.Outline = pOutLine;
pSimpleFillSymbol.Style = esriSimpleFillStyle.esriSFSHollow;
IFillSymbol pFillSymbol = pSimpleFillSymbol;
((RectangleElementClass)pRectangleEle).Symbol = pFillSymbol;
var elemnet = (IElement)pRectangleEle;
pGraphicsContainer.AddElement(elemnet, 0);
OverviewMapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, elemnet, elemnet.Geometry.Envelope);
Marshal.ReleaseComObject(pActiveView);
Marshal.ReleaseComObject(pGraphicsContainer);
}
}
/// <summary>
/// 点击鹰眼窗体,主窗体定位到该点
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OverviewMapControl_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e)
{
try
{
if (OverviewMapControl.Map.LayerCount > 0)
{
//左键将所点击的位置,设置为主视图的中心
if (e.button == 1)
{
IPoint pPoint = new PointClass();
pPoint.PutCoords(e.mapX, e.mapY);
MapControl.CenterAt(pPoint);
MapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
DrawRectangle(MapControl.Extent);
}
//右键拉框范围设置为主视图显示范围
else if (e.button == 2)
{
var pEnv = OverviewMapControl.TrackRectangle();
MapControl.Extent = pEnv;
MapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
DrawRectangle(MapControl.Extent);
}
}
}
catch (Exception ex)
{
LogHelper.Error("地图主视图-鹰眼窗体与主视图互动时出错", ex);
}
}
/// <summary>
/// 鹰眼视图上鼠标移动事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OverviewMapControl_OnMouseMove(object sender, IMapControlEvents2_OnMouseMoveEvent e)
{
//左键点击移动,俩个视图联动
if (e.button == 1)
{
IPoint pPoint = new PointClass();
pPoint.PutCoords(e.mapX, e.mapY);
MapControl.CenterAt(pPoint);
MapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
}
}
#endregion
4.内容列表
#region 内容列表
private ToolStripItem deleteToolStripItem;
private void InitializeControls()
{
try
{
overviewMapHost.Child = OverviewMapControl;
OverviewMapControl.OnMouseDown += OverviewMapControl_OnMouseDown;
OverviewMapControl.OnMouseMove += OverviewMapControl_OnMouseMove;
OverviewMapControl.AutoMouseWheel = false;
OverviewMapControl.ShowScrollbars = false;
((ISupportInitialize)OverviewMapControl).BeginInit();
((ISupportInitialize)OverviewMapControl).EndInit();
var tocControl = AppHost.Current.PluginProvider.Gets<TocControlPage>()
.FirstOrDefault(p => ((ControlItem)p.Tag).Id == "Toc1");
if (tocControl == null) return;
tocControl.tocHost.Child = TocControl;
TocControl.SetBuddyControl(MapControl);
TocControl.OnMouseDown += axTOCControl_OnMouseDown;
TocControl.EnableLayerDragDrop = true;
//实例化选择按钮
m_selectTool.OnCreate(MapControl.Object);
//定义一个右键菜单
var tocContextMenuStrip = new ContextMenuStrip();
TocControl.ContextMenuStrip = tocContextMenuStrip;
//定义右键内容
deleteToolStripItem = new ToolStripMenuItem("移除图层",
Image.FromFile(PathHelper.GetCurrentRootPath() + "images\\图层右键\\移除图层.png"), RemoveLayer_Click)
{ Enabled = false };
TocControl.ContextMenuStrip.Items.Add(new SmartZn.Arcgis.Controls.View.CallOpenAttributeTabPage(MapControl, TocControl, "打开属性表",
Image.FromFile(PathHelper.GetCurrentRootPath() + "images\\图层右键\\打开属性表.png")).AttributeTableMenuItem);
TocControl.ContextMenuStrip.Items.Add("缩放到图层",
Image.FromFile(PathHelper.GetCurrentRootPath() + "images\\图层右键\\缩放到图层.png"), ZoomToLayer_Click);
TocControl.ContextMenuStrip.Items.Add(deleteToolStripItem);
TocControl.ContextMenuStrip.Items.Add("属性",
Image.FromFile(PathHelper.GetCurrentRootPath() + "images\\图层右键\\属性.png"), Attribute_Click);
}
catch (Exception ex)
{
LogHelper.Error("地图主视图-初始化数据列表时出错", ex);
}
}
/// <summary>
/// 内容列表右键
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void axTOCControl_OnMouseDown(object sender, ITOCControlEvents_OnMouseDownEvent e)
{
if (e.button == 2)
{
var pItem = esriTOCControlItem.esriTOCControlItemNone;
ILayer pLayer = null;
IBasicMap pMap = null;
object pIndex = null;
object pOther = null;
TocControl.HitTest(e.x, e.y, ref pItem, ref pMap, ref pLayer, ref pIndex, ref pOther);
TocControl.ContextMenuStrip.Show(TocControl, e.x, e.y);
}
}
/// <summary>
/// 缩放到图层
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ZoomToLayer_Click(object sender, EventArgs e)
{
var pItem = esriTOCControlItem.esriTOCControlItemNone;
ILayer pLayer = null;
IBasicMap pMap = null;
object pIndex = null;
object pOther = null;
TocControl.GetSelectedItem(ref pItem, ref pMap, ref pLayer, ref pIndex, ref pOther);
if (pLayer != null)
{
var pGeoDataset = pLayer as IGeoDataset;
MapControl.ActiveView.Extent = pGeoDataset.Extent;
MapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
}
}
/// <summary>
/// 删除图层
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void RemoveLayer_Click(object sender, EventArgs e)
{
var pItem = esriTOCControlItem.esriTOCControlItemNone;
ILayer pLayer = null;
IBasicMap pMap = null;
object pIndex = null;
object pOther = null;
TocControl.GetSelectedItem(ref pItem, ref pMap, ref pLayer, ref pIndex, ref pOther);
if (pLayer != null)
{
MapControl.Map.DeleteLayer(pLayer);
///图层解锁,否则图层目录下依然存在lock文件
IDataset pDataSet = pLayer as IDataset;
IWorkspace pWs = pDataSet.Workspace;
var pWorkspaceFactory = pWs.WorkspaceFactory;
IWorkspaceFactoryLockControl ipWsFactoryLock = pWorkspaceFactory as IWorkspaceFactoryLockControl;
if (ipWsFactoryLock.SchemaLockingEnabled)
{
ipWsFactoryLock.DisableSchemaLocking();
}
System.Runtime.InteropServices.Marshal.ReleaseComObject(pWorkspaceFactory);
MapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
}
}
/// <summary>
/// 属性
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Attribute_Click(object sender, EventArgs e)
{
try
{
var pItem = esriTOCControlItem.esriTOCControlItemNone;
ILayer pLayer = null;
IBasicMap pMap = null;
object pIndex = null;
object pOther = null;
TocControl.GetSelectedItem(ref pItem, ref pMap, ref pLayer, ref pIndex, ref pOther);
if (pLayer != null && pLayer is IFeatureLayer)
{
IComPropertySheet pComPropSheet;
pComPropSheet = new ComPropertySheet();
pComPropSheet.Title = pLayer.Name + " - 属性";
UID pPPUID = new UIDClass();
pComPropSheet.AddCategoryID(pPPUID);
// General
IPropertyPage pGenPage = new GeneralLayerPropPageClass();
pComPropSheet.AddPage(pGenPage);
// Source
IPropertyPage pSrcPage = new FeatureLayerSourcePropertyPageClass();
pComPropSheet.AddPage(pSrcPage);
Selection
//IPropertyPage pSelectPage = new FeatureLayerSelectionPropertyPageClass();
//pComPropSheet.AddPage(pSelectPage);
// Display
IPropertyPage pDispPage = new FeatureLayerDisplayPropertyPageClass();
pComPropSheet.AddPage(pDispPage);
// Symbology
IPropertyPage pDrawPage = new LayerDrawingPropertyPageClass();
pComPropSheet.AddPage(pDrawPage);
// Fields
IPropertyPage pFieldsPage = new LayerFieldsPropertyPageClass();
pComPropSheet.AddPage(pFieldsPage);
Definition Query...
//IPropertyPage pQueryPage = new LayerDefinitionQueryPropertyPageClass();
//pComPropSheet.AddPage(pQueryPage);
// Labels
IPropertyPage pSelPage = new LayerLabelsPropertyPageClass();
pComPropSheet.AddPage(pSelPage);
Joins & Relates....
//IPropertyPage pJoinPage = new JoinRelatePageClass();
//pComPropSheet.AddPage(pJoinPage);
Setup layer link
ISet pMySet = new SetClass();
pMySet.Add(pLayer);
pMySet.Reset();
// 默认选中第四项
pComPropSheet.ActivePage = 4;
var bOK = pComPropSheet.EditProperties(pMySet, 0);
MapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null,
MapControl.ActiveView.Extent);
MapControl.Update();
MapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
}
}
catch (Exception ex)
{
LogHelper.Error("地图主视图-内容列表右键【属性】时出错", ex);
}
}
#endregion
5. 打印视图
#region 打印视图AxPageLayoutControl
public AxPageLayoutControl layoutControl = new AxPageLayoutControl();
public LayoutControlPage()
{
InitializeComponent();
printHost.Child = layoutControl;
layoutControl.OnMouseDown += AxPageLayoutControl_OnMouseDown;
layoutControl.OnMouseMove += AxPageLayoutControl_OnMouseMove;
layoutControl.OnDoubleClick += AxPageLayoutControl_OnDbClick;
}
/// <summary>
/// 与地图界面实现同步
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void PrintPage_Loaded(object sender, RoutedEventArgs e)
{
try
{
var page = AppHost.Current.PluginProvider.Gets<MapControlPage>()
.FirstOrDefault(p => ((ControlItem) p.Tag).Id == "Map1");
((ISupportInitialize) layoutControl).BeginInit();
ControlsSynchronizer.PageLayoutPage = this;
((ISupportInitialize) layoutControl).EndInit();
layoutControl.ActiveView.Clear();
ControlsSynchronizer.SynchronizerMap();
}
catch (Exception ex)
{
LogHelper.Error("打印视图-页面加载时出错", ex);
}
}
/// <summary>
/// 打印视图mousedown事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void AxPageLayoutControl_OnMouseDown(object sender, IPageLayoutControlEvents_OnMouseDownEvent e)
{
if (e.button == 1)
layoutControl.MousePointer = esriControlsMousePointer.esriPointerDefault;
}
/// <summary>
/// 打印视图mousemove事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void AxPageLayoutControl_OnMouseMove(object sender, IPageLayoutControlEvents_OnMouseMoveEvent e)
{
}
/// <summary>
/// 打印视图双击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void AxPageLayoutControl_OnDbClick(object sender, IPageLayoutControlEvents_OnDoubleClickEvent e)
{
}
#endregion