Exemplo n.º 1
0
        private void frmMain_Load(object sender, System.EventArgs e)
        {
            // Add commands to the NALayer context menu
            m_menuLayer = new ToolbarMenuClass();

            int nItem = -1;

            m_menuLayer.AddItem(new cmdLoadLocations(), -1, ++nItem, false, esriCommandStyles.esriCommandStyleTextOnly);
            m_menuLayer.AddItem(new cmdRemoveLayer(), -1, ++nItem, false, esriCommandStyles.esriCommandStyleTextOnly);
            m_menuLayer.AddItem(new cmdClearAnalysisLayer(), -1, ++nItem, true, esriCommandStyles.esriCommandStyleTextOnly);
            m_menuLayer.AddItem(new cmdNALayerProperties(), -1, ++nItem, true, esriCommandStyles.esriCommandStyleTextOnly);

            // Since this ToolbarMenu is a standalone popup menu use the SetHook method to
            //  specify the object that will be sent as a "hook" to the menu commands in their OnCreate methods.
            m_menuLayer.SetHook(axMapControl1);

            // Add command for ArcGIS Network Analyst extension env properties to end of "Network Analyst" dropdown menu
            nItem = -1;
            for (int i = 0; i < axToolbarControl1.Count; ++i)
            {
                IToolbarItem item = axToolbarControl1.GetItem(i);
                IToolbarMenu mnu  = item.Menu;

                if (mnu == null)
                {
                    continue;
                }

                IMenuDef mnudef = mnu.GetMenuDef();
                string   name   = mnudef.Name;

                // Find the ArcGIS Network Analyst extension solver menu drop down and note the index
                if (name == "ControlToolsNetworkAnalyst_SolverMenu")
                {
                    nItem = i;
                    //break;
                }
            }

            if (nItem >= 0)
            {
                // Using the index found above, get the solver menu drop down and add the Properties command to the end of it.
                IToolbarItem item = axToolbarControl1.GetItem(nItem);
                IToolbarMenu mnu  = item.Menu;
                if (mnu != null)
                {
                    mnu.AddItem(new cmdNAProperties(), -1, mnu.Count, true, esriCommandStyles.esriCommandStyleTextOnly);
                }

                // Since this ToolbarMenu is an item on the ToolbarControl the Hook is shared and initialized by the ToolbarControl.
                //  Therefore, SetHook is not called here, like it is for the menu above.
            }

            // Initialize naEnv variables
            m_naEnv = CommonFunctions.GetTheEngineNetworkAnalystEnvironment();
            if (m_naEnv == null)
            {
                MessageBox.Show("Error: EngineNetworkAnalystEnvironment is not properly configured");
                return;
            }

            m_naEnv.ZoomToResultAfterSolve         = false;
            m_naEnv.ShowAnalysisMessagesAfterSolve = (int)(esriEngineNAMessageType.esriEngineNAMessageTypeInformative |
                                                           esriEngineNAMessageType.esriEngineNAMessageTypeWarning);

            // Set up the buddy control and initialize the NA extension, so we can get to NAWindow to listen to window events.
            // This is necessary, as the various controls are not yet set up. They need to be in order to get the NAWindow's events.
            axToolbarControl1.SetBuddyControl(axMapControl1);
            IExtension ext = m_naEnv as IExtension;
            object     obj = axToolbarControl1.Object;

            ext.Startup(ref obj);

            // m_naWindow is set after Startup of the Network Analyst extension
            m_naWindow = m_naEnv.NAWindow;
            if (m_naWindow == null)
            {
                MessageBox.Show("Error: Unexpected null NAWindow");
                return;
            }

            m_onContextMenu = new IEngineNAWindowEventsEx_OnContextMenuEventHandler(OnContextMenu);
            ((IEngineNAWindowEventsEx_Event)m_naWindow).OnContextMenu += m_onContextMenu;

            m_OnNetworkLayersChanged = new IEngineNetworkAnalystEnvironmentEvents_OnNetworkLayersChangedEventHandler(OnNetworkLayersChanged);
            ((IEngineNetworkAnalystEnvironmentEvents_Event)m_naEnv).OnNetworkLayersChanged += m_OnNetworkLayersChanged;

            m_OnCurrentNetworkLayerChanged = new IEngineNetworkAnalystEnvironmentEvents_OnCurrentNetworkLayerChangedEventHandler(OnCurrentNetworkLayerChanged);
            ((IEngineNetworkAnalystEnvironmentEvents_Event)m_naEnv).OnCurrentNetworkLayerChanged += m_OnCurrentNetworkLayerChanged;
        }
        private void frmMain_Load(object sender, System.EventArgs e)
        {
            // Add commands to the NALayer context menu
            // 向Nalayer上下文菜单添加命令
            m_menuLayer = new ToolbarMenuClass();

            int nItem = -1;

            m_menuLayer.AddItem(new cmdLoadLocations(), -1, ++nItem, false, esriCommandStyles.esriCommandStyleTextOnly);
            m_menuLayer.AddItem(new cmdRemoveLayer(), -1, ++nItem, false, esriCommandStyles.esriCommandStyleTextOnly);
            m_menuLayer.AddItem(new cmdClearAnalysisLayer(), -1, ++nItem, true, esriCommandStyles.esriCommandStyleTextOnly);
            m_menuLayer.AddItem(new cmdNALayerProperties(), -1, ++nItem, true, esriCommandStyles.esriCommandStyleTextOnly);
            m_menuLayer.SetHook(axMapControl1);

            // Add command for Network Analyst env properties to end of "Network Analyst" dropdown menu
            // 将网络分析env属性的命令添加到“网络分析师”下拉菜单的末尾
            nItem = -1;
            for (int i = 0; i < axToolbarControl1.Count; ++i)
            {
                IToolbarItem item = axToolbarControl1.GetItem(i);
                IToolbarMenu mnu  = item.Menu;

                if (mnu == null)
                {
                    continue;
                }

                IMenuDef mnudef = mnu.GetMenuDef();
                string   name   = mnudef.Name;
                if (name == "ControlToolsNetworkAnalyst_SolverMenu")
                {
                    nItem = i;
                    break;
                }
            }

            if (nItem >= 0)
            {
                IToolbarItem item = axToolbarControl1.GetItem(nItem);
                IToolbarMenu mnu  = item.Menu;
                if (mnu != null)
                {
                    mnu.AddItem(new cmdNAProperties(), -1, mnu.Count, true, esriCommandStyles.esriCommandStyleTextOnly);
                }
            }

            // Initialize naEnv variables
            // 初始化naEnv变量
            m_naEnv = new EngineNetworkAnalystEnvironmentClass();
            m_naEnv.ZoomToResultAfterSolve         = false;
            m_naEnv.ShowAnalysisMessagesAfterSolve = (int)(esriEngineNAMessageType.esriEngineNAMessageTypeInformative | esriEngineNAMessageType.esriEngineNAMessageTypeWarning);

            // Explicitly setup buddy control and initialize NA extension
            // so we can get to NAWindow to listen to window events
            // This is necessary the various controls are not yet setup and they
            // need to be in order to get the NAWindow's events.
            // 显式设置伙伴控件并初始化na扩展,以便我们可以访问na window来侦听窗口事件。
            // 这是必需的,各种控件尚未设置,它们需要用于获取nawindow的事件。
            axToolbarControl1.SetBuddyControl(axMapControl1);
            IExtension ext = m_naEnv as IExtension;
            object     obj = axToolbarControl1.Object;

            ext.Startup(ref obj);
            m_naWindow      = m_naEnv.NAWindow;
            m_onContextMenu = new IEngineNAWindowEventsEx_OnContextMenuEventHandler(OnContextMenu);
            ((IEngineNAWindowEventsEx_Event)m_naWindow).OnContextMenu += m_onContextMenu;
        }