主题描述
通过菜单的方法启动新的窗体并向新的窗体传递图层信息,然后在新的窗体中对图层进行其他操作和分析。
基本思路
1.通过点击菜单,通过建立的Base Command工具实现启动先的窗体,前几篇符号系统处理中有详细介绍即代码介绍
主窗体中新建菜单,菜单点击事件下添加代码调用Base Command命令启动窗体:
ICommand command = new 主窗体.子窗体Cmd();
command.OnCreate(m_mapControl.Object);
command.OnClick();
2.通过IHookHelper接口传递参数
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Carto;
namespace Farm.统计分析
{
/// <summary>
/// Command that works in ArcMap/Map/PageLayout
/// </summary>
[Guid("1db569c4-e9c4-4dec-82d9-3c65531b1ef0")]
[ClassInterface(ClassInterfaceType.None)]
[ProgId("Farm.统计分析.StatisticsChartCmd")]
public sealed class StatisticsChartCmd : BaseCommand
{
#region COM Registration Function(s)
[ComRegisterFunction()]
[ComVisible(false)]
static void RegisterFunction(Type registerType)
{
// Required for ArcGIS Component Category Registrar support
ArcGISCategoryRegistration(registerType);
//
// TODO: Add any COM registration code here
//
}
[ComUnregisterFunction()]
[ComVisible(false)]
static void UnregisterFunction(Type registerType)
{
// Required for ArcGIS Component Category Registrar support
ArcGISCategoryUnregistration(registerType);
//
// TODO: Add any COM unregistration code here
//
}
#region ArcGIS Component Category Registrar generated code
/// <summary>
/// Required method for ArcGIS Component Category registration -
/// Do not modify the contents of this method with the code editor.
/// </summary>
private static void ArcGISCategoryRegistration(Type registerType)
{
string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
MxCommands.Register(regKey);
ControlsCommands.Register(regKey);
}
/// <summary>
/// Required method for ArcGIS Component Category unregistration -
/// Do not modify the contents of this method with the code editor.
/// </summary>
private static void ArcGISCategoryUnregistration(Type registerType)
{
string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
MxCommands.Unregister(regKey);
ControlsCommands.Unregister(regKey);
}
#endregion
#endregion
private IHookHelper m_hookHelper = null;
IMapControl3 m_mapcontrol = null;
IFeatureLayer currentLayer = null;
public StatisticsChartCmd()
{
//
// TODO: Define values for the public properties
//
base.m_category = ""; //localizable text
base.m_caption = ""; //localizable text
base.m_message = "This should work in ArcMap/MapControl/PageLayoutControl"; //localizable text
base.m_toolTip = ""; //localizable text
base.m_name = ""; //unique id, non-localizable (e.g. "MyCategory_MyCommand")
try
{
//
// TODO: change bitmap name if necessary
//
string bitmapResourceName = GetType().Name + ".bmp";
base.m_bitmap = new Bitmap(GetType(), bitmapResourceName);
}
catch (Exception ex)
{
System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap");
}
}
#region Overridden Class Methods
/// <summary>
/// Occurs when this command is created
/// </summary>
/// <param name="hook">Instance of the application</param>
public override void OnCreate(object hook)
{
if (hook == null)
return;
try
{
m_hookHelper = new HookHelperClass();
m_hookHelper.Hook = hook;
if (m_hookHelper.ActiveView == null)
m_hookHelper = null;
}
catch
{
m_hookHelper = null;
}
if (m_hookHelper == null)
base.m_enabled = false;
else
base.m_enabled = true;
// TODO: Add other initialization code
}
/// <summary>
/// Occurs when this command is clicked
/// </summary>
public override void OnClick()
{
// TODO: Add ExportMapCmd.OnClick implementation
// TODO: Add ExportMapCmd.OnClick implementation
if (m_hookHelper == null) return;
if (m_hookHelper.FocusMap.LayerCount > 0)
{
StatisticsChartFrm StatisticsChartfrm = new StatisticsChartFrm(m_hookHelper);
StatisticsChartfrm.Show(m_hookHelper as System.Windows.Forms.IWin32Window);
}
// TODO: Add StatisticsChartCmd.OnClick implementation
}
#endregion
}
}
新窗体获取图层列表
代码如下:
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.esriSystem;
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;
namespace Farm.统计分析
{
public partial class StatisticsChartFrm : Form
{
IHookHelper m_hookHelper = null;
IActiveView m_activeView = null;
IMap m_map = null;
const string m_dataSetName = "m_layerDataSet";
const string m_dataSourceName = "GeoDataSource";
IFeatureLayer currentLayer = null;
DataSet m_layerDataSet = new DataSet(m_dataSetName);
public StatisticsChartFrm(IHookHelper hookHelper)
{
InitializeComponent();
if (hookHelper == null) return;
m_hookHelper = hookHelper;
m_activeView = m_hookHelper.ActiveView;
m_map = m_hookHelper.FocusMap;
}
private void StatisticsChartFrm_Load(object sender, EventArgs e)
{
initialize();
}
private void initialize()
{
if (GetLayers() == null) return;
IEnumLayer layers = GetLayers();
layers.Reset();
ILayer layer = layers.Next();
while (layer != null)
{
if (layer is IFeatureLayer)
{
LayerListCob.Items.Add(layer.Name);
}
layer = layers.Next();
}
}
#region "GetLayers"
private IEnumLayer GetLayers()
{
UID uid = new UIDClass();
uid.Value = "{40A9E885-5533-11d0-98BE-00805F7CED21}";// IFeatureLayer
//uid.Value = "{E156D7E5-22AF-11D3-9F99-00C04F6BC78E}"; // IGeoFeatureLayer
//uid.Value = "{6CA416B1-E160-11D2-9F4E-00C04F6BC78E}"; // IDataLayer
if (m_map.LayerCount != 0)
{
IEnumLayer layers = m_map.get_Layers(uid, true);
return layers;
}
return null;
}
#endregion
}
}
结果显示: