ArcObjects Library Reference  

MainForm

About the Schematics Engine application Sample

[C#]

MainForm.cs

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;

using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.ADF;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.SchematicControls;
using ESRI.ArcGIS.Schematic;

namespace SchematicApplication
{
	public sealed partial class MainForm : Form
	{
		#region class private members
		private ITOCControl2 m_tocControl;
		private IMapControl3 m_mapControl = null;
		private string m_mapDocumentName = string.Empty;
		private IToolbarMenu m_menuSchematicLayer;
		private IToolbarMenu m_menuLayer;
		private IToolbarMenu m_CreateMenu = new ToolbarMenuClass();

		#endregion

		#region class constructor
		public MainForm()
		{
			InitializeComponent();
		}
		#endregion

		private void MainForm_Load(object sender, EventArgs e)
		{
			//get the MapControl and tocControl
			m_tocControl = (ITOCControl2)axTOCControl1.Object;
			m_mapControl = (IMapControl3)axMapControl1.Object;

			//Set buddy control for tocControl
			m_tocControl.SetBuddyControl(m_mapControl);

			//disable the Save menu (since there is no document yet)
			menuSaveDoc.Enabled = false;

			//Create a SchematicEditor's MenuDef object
			IMenuDef menuDefSchematicEditor = new CreateMenuSchematicEditor();

			//Add SchematicEditor on the ToolBarMenu
			m_CreateMenu.AddItem(menuDefSchematicEditor, 0, -1, false, esriCommandStyles.esriCommandStyleIconAndText);

			//Set the ToolbarMenu's hook
			m_CreateMenu.SetHook(axToolbarControl2.Object);

			//Set the ToolbarMenu's caption
			m_CreateMenu.Caption = "SchematicEditor";

			/// Add ToolbarMenu on the ToolBarControl
			axToolbarControl2.AddItem(m_CreateMenu, -1, -1, false, 0, esriCommandStyles.esriCommandStyleMenuBar);

			///Create a other ToolbarMenu for layer
			m_menuSchematicLayer = new ToolbarMenuClass();
			m_menuLayer = new ToolbarMenuClass();

			///Add 3 items on the SchematicLayer properties menu 
			m_menuSchematicLayer.AddItem(new RemoveLayer(), -1, 0, false, esriCommandStyles.esriCommandStyleTextOnly);
			m_menuSchematicLayer.AddItem("esriControls.ControlsSchematicSaveEditsCommand", -1, 1, true, esriCommandStyles.esriCommandStyleIconAndText);
			m_menuSchematicLayer.AddItem("esriControls.ControlsSchematicUpdateDiagramCommand", -1, 2, false, esriCommandStyles.esriCommandStyleIconAndText);

			IMenuDef subMenuDef = new CreateSubMenuSchematic();
			m_menuSchematicLayer.AddSubMenu(subMenuDef, 3, true);
			////Add the sub-menu as the third item on the Layer properties menu, making it start a new group
			m_menuSchematicLayer.AddItem(new ZoomToLayer(), -1, 4, true, esriCommandStyles.esriCommandStyleTextOnly);

			m_menuLayer.AddItem(new RemoveLayer(), -1, 0, false, esriCommandStyles.esriCommandStyleTextOnly);
			m_menuLayer.AddItem(new ZoomToLayer(), -1, 1, true, esriCommandStyles.esriCommandStyleTextOnly);

			////Set the hook of each menu
			m_menuSchematicLayer.SetHook(m_mapControl);
			m_menuLayer.SetHook(m_mapControl);
		}

		private void axTOCControl1_OnMouseDown(object sender, ITOCControlEvents_OnMouseDownEvent e)
		{
			if (e.button != 2) return;

			esriTOCControlItem item = esriTOCControlItem.esriTOCControlItemNone;
			IBasicMap map = null; ILayer layer = null;
			object other = null; object index = null;

			//Determine what kind of item is selected
			m_tocControl.HitTest(e.x, e.y, ref item, ref map, ref layer, ref other, ref index);

			//Ensure the item gets selected 
			if (item == esriTOCControlItem.esriTOCControlItemMap)
				m_tocControl.SelectItem(map, null);
			else
				m_tocControl.SelectItem(layer, null);

			//Set the layer into the CustomProperty (this is used by the custom layer commands)			
			m_mapControl.CustomProperty = layer;

			ISchematicLayer schLayer = layer as ISchematicLayer;
			if (schLayer != null) /// attach menu for SchematicLayer
			{
				ISchematicTarget schematicTarget = new ESRI.ArcGIS.SchematicControls.EngineSchematicEnvironmentClass() as ISchematicTarget;
				if (schematicTarget != null)
					schematicTarget.SchematicTarget = schLayer;
				//Popup the correct context menu
				if (item == esriTOCControlItem.esriTOCControlItemLayer) m_menuSchematicLayer.PopupMenu(e.x, e.y, m_tocControl.hWnd);
			}
			else /// attach menu for Layer
			{
				//Popup the correct context menu
				if (item == esriTOCControlItem.esriTOCControlItemLayer) m_menuLayer.PopupMenu(e.x, e.y, m_tocControl.hWnd);
			}
		}


		#region Main Menu event handlers
		private void menuNewDoc_Click(object sender, EventArgs e)
		{
			//execute New Document command
			ICommand command = new CreateNewDocument();
			command.OnCreate(m_mapControl.Object);
			command.OnClick();
		}

		private void menuOpenDoc_Click(object sender, EventArgs e)
		{
			//execute Open Document command
			ICommand command = new ControlsOpenDocCommandClass();
			command.OnCreate(m_mapControl.Object);
			command.OnClick();
		}

		private void menuSaveDoc_Click(object sender, EventArgs e)
		{
			//execute Save Document command
			if (m_mapControl.CheckMxFile(m_mapDocumentName))
			{
				//create a new instance of a MapDocument
				IMapDocument mapDoc = new MapDocumentClass();
				mapDoc.Open(m_mapDocumentName, string.Empty);

				//Make sure that the MapDocument is not read only
				if (mapDoc.get_IsReadOnly(m_mapDocumentName))
				{
					MessageBox.Show("Map document is read only!");
					mapDoc.Close();
					return;
				}

				//Replace its contents with the current map
				mapDoc.ReplaceContents((IMxdContents)m_mapControl.Map);

				//save the MapDocument in order to persist it
				mapDoc.Save(mapDoc.UsesRelativePaths, false);

				//close the MapDocument
				mapDoc.Close();
			}
		}

		private void menuSaveAs_Click(object sender, EventArgs e)
		{
			//execute SaveAs Document command
			ICommand command = new ControlsSaveAsDocCommandClass();
			command.OnCreate(m_mapControl.Object);
			command.OnClick();
		}

		private void menuExitApp_Click(object sender, EventArgs e)
		{
			//exit the application
			Application.Exit();
		}


		#endregion


		//listen to MapReplaced event in order to update the status bar and the Save menu
		private void axMapControl1_OnMapReplaced(object sender, IMapControlEvents2_OnMapReplacedEvent e)
		{
			//get the current document name from the MapControl
			m_mapDocumentName = m_mapControl.DocumentFilename;

			//if there is no MapDocument, disable the Save menu and clear the status bar
			if (m_mapDocumentName == string.Empty)
			{
				menuSaveDoc.Enabled = false;
				statusBarXY.Text = string.Empty;
			}
			else
			{
				//enable the Save menu and write the doc name to the status bar
				menuSaveDoc.Enabled = true;
				statusBarXY.Text = Path.GetFileName(m_mapDocumentName);
			}
		}

		private void axMapControl1_OnMouseMove(object sender, IMapControlEvents2_OnMouseMoveEvent e)
		{
			statusBarXY.Text = string.Format("{0}, {1}  {2}", e.mapX.ToString("#######.##"), e.mapY.ToString("#######.##"), axMapControl1.MapUnits.ToString().Substring(4));
		}
	}
}
[Visual Basic .NET]

MainForm.vb

Imports System.IO
Imports ESRI.ArcGIS.esriSystem
Imports ESRI.ArcGIS.Carto
Imports ESRI.ArcGIS.Controls
Imports ESRI.ArcGIS.ADF
Imports ESRI.ArcGIS.SystemUI
Imports ESRI.ArcGIS.SchematicControls
Imports ESRI.ArcGIS.Schematic


Public Class MainForm
  Private m_mapControl As IMapControl3 = Nothing
	Private m_mapDocumentName As String = String.Empty
	Private m_menuLayer As IToolbarMenu
	Private m_menuSchematicLayer As IToolbarMenu
	Private m_createMenu As IToolbarMenu = New ToolbarMenuClass
	Private m_pTocControl As ITOCControl2


  Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As EventArgs) Handles MyBase.Load
    'get the MapControl
		m_mapControl = CType(axMapControl1.Object, IMapControl3)
		m_pTocControl = CType(axTOCControl1.Object, ITOCControl2)

    'disable the Save menu (since there is no document yet)
		menuSaveDoc.Enabled = False

		Dim menuDefSchematicEditor As IMenuDef

		'Create a SchematicEditor's MenuDef object
		menuDefSchematicEditor = New CreateMenuSchematicEditor

		'Add SchematicEditor on the ToolBarMenu
		m_createMenu.AddItem(menuDefSchematicEditor, 0, -1, False, esriCommandStyles.esriCommandStyleIconAndText)
		'Set the Toolbar menu's hook
		m_createMenu.SetHook(axToolbarControl2.Object)

		'Set the ToolbarMenu's caption
		m_createMenu.Caption = "SchematicEditor"

		'Add ToolbarMenu on the ToolBarControl
		axToolbarControl2.AddItem(m_createMenu, -1, -1, False, 0, esriCommandStyles.esriCommandStyleMenuBar)

		'Create a other ToolbarMenu for layer
		m_menuSchematicLayer = New ToolbarMenuClass()
		m_menuLayer = New ToolbarMenuClass()

		'Add 2 ControlsSchematic items on the Layer properties menu 
		m_menuSchematicLayer.AddItem(New RemoveLayer, -1, 0, False, esriCommandStyles.esriCommandStyleTextOnly)
		m_menuSchematicLayer.AddItem("esriControls.ControlsSchematicSaveEditsCommand", 0, 1, True, esriCommandStyles.esriCommandStyleIconAndText)
		m_menuSchematicLayer.AddItem("esriControls.ControlsSchematicUpdateDiagramCommand", 0, 2, False, esriCommandStyles.esriCommandStyleIconAndText)

		Dim subMenuDef As IMenuDef = New CreateSubMenuSchematic()

		'Add the sub-menu as the third item on the Layer properties menu, making it start a new group
		m_menuSchematicLayer.AddSubMenu(subMenuDef, 3, True)
		m_menuSchematicLayer.AddItem(New ZoomToLayer, -1, 4, True, esriCommandStyles.esriCommandStyleTextOnly)

		m_menuLayer.AddItem(New RemoveLayer, -1, 0, False, esriCommandStyles.esriCommandStyleTextOnly)
		m_menuLayer.AddItem(New ZoomToLayer, -1, 1, True, esriCommandStyles.esriCommandStyleTextOnly)
		'Set the hook of each menu

		m_menuSchematicLayer.SetHook(m_mapControl)
		m_menuLayer.SetHook(m_mapControl)

  End Sub


#Region "Main Menu event handlers"
    Private Sub menuNewDoc_Click(ByVal sender As Object, ByVal e As EventArgs) Handles menuNewDoc.Click
        'execute New Document command
        Dim command As ICommand = New CreateNewDocument()
        command.OnCreate(m_mapControl.Object)
        command.OnClick()
    End Sub

    Private Sub menuOpenDoc_Click(ByVal sender As Object, ByVal e As EventArgs) Handles menuOpenDoc.Click
        'execute Open Document command
        Dim command As ICommand = New ControlsOpenDocCommandClass()
        command.OnCreate(m_mapControl.Object)
        command.OnClick()
    End Sub

    Private Sub menuSaveDoc_Click(ByVal sender As Object, ByVal e As EventArgs) Handles menuSaveDoc.Click
        'execute Save Document command
        If m_mapControl.CheckMxFile(m_mapDocumentName) Then
            'create a new instance of a MapDocument
            Dim mapDoc As IMapDocument = New MapDocumentClass()
            mapDoc.Open(m_mapDocumentName, String.Empty)

			'Make sure that the MapDocument is not read only
            If mapDoc.IsReadOnly(m_mapDocumentName) Then
                MsgBox("Map document is read only!")
                mapDoc.Close()
                Return
            End If

            'Replace its contents with the current map
            mapDoc.ReplaceContents(CType(m_mapControl.Map, IMxdContents))

            'save the MapDocument in order to persist it
            mapDoc.Save(mapDoc.UsesRelativePaths, False)

            'close the MapDocument
            mapDoc.Close()
        End If
    End Sub

    Private Sub menuSaveAs_Click(ByVal sender As Object, ByVal e As EventArgs) Handles menuSaveAs.Click
        'execute SaveAs Document command
        Dim command As ICommand = New ControlsSaveAsDocCommandClass()
        command.OnCreate(m_mapControl.Object)
        command.OnClick()
    End Sub

    Private Sub menuExitApp_Click(ByVal sender As Object, ByVal e As EventArgs) Handles menuExitApp.Click
        'exit the application
        Application.Exit()
    End Sub
#End Region

	'listen to MapReplaced event in order to update the status bar and the Save menu
    Private Sub axMapControl1_OnMapReplaced(ByVal sender As Object, ByVal e As IMapControlEvents2_OnMapReplacedEvent) Handles axMapControl1.OnMapReplaced
        'get the current document name from the MapControl
        m_mapDocumentName = m_mapControl.DocumentFilename

		'if there is no MapDocument, disable the Save menu and clear the status bar
        If m_mapDocumentName = String.Empty Then
            menuSaveDoc.Enabled = False
            statusBarXY.Text = String.Empty
        Else
			'enable the Save menu and write the doc name to the status bar
            menuSaveDoc.Enabled = True
            statusBarXY.Text = Path.GetFileName(m_mapDocumentName)
        End If
    End Sub

    Private Sub axMapControl1_OnMouseMove(ByVal sender As Object, ByVal e As IMapControlEvents2_OnMouseMoveEvent) Handles axMapControl1.OnMouseMove
        statusBarXY.Text = String.Format("{0}, {1}  {2}", e.mapX.ToString("#######.##"), e.mapY.ToString("#######.##"), axMapControl1.MapUnits.ToString().Substring(4))
    End Sub

	Private Sub AxTOCControl1_OnMouseDown(ByVal sender As System.Object, ByVal e As ESRI.ArcGIS.Controls.ITOCControlEvents_OnMouseDownEvent) Handles axTOCControl1.OnMouseDown
		If (e.button <> 2) Then Exit Sub

		Dim pItem As esriTOCControlItem
		Dim pMap As IMap = Nothing, pLayer As ILayer = Nothing
		Dim pOther As Object = Nothing, pIndex As Object = Nothing

		'Determine what kind of item is selected
		m_pTocControl.HitTest(e.x, e.y, pItem, CType(pMap, IBasicMap), pLayer, pOther, pIndex)

		'Ensure the item gets selected 
		If (pItem = esriTOCControlItem.esriTOCControlItemMap) Then
			m_pTocControl.SelectItem(pMap, Nothing)
		Else
			m_pTocControl.SelectItem(pLayer, Nothing)
		End If

		'Set the layer into the CustomProperty (this is used by the custom layer commands)			
		m_mapControl.CustomProperty = pLayer

		Dim schLayer As ISchematicLayer
		schLayer = TryCast(pLayer, ISchematicLayer)

		If Not schLayer Is Nothing Then
			Dim schematicTarget As ISchematicTarget
			schematicTarget = TryCast(New ESRI.ArcGIS.SchematicControls.EngineSchematicEnvironmentClass(), ISchematicTarget)
			If (Not schematicTarget Is Nothing) Then
				schematicTarget.SchematicTarget = schLayer
			End If

			'Popup the correct context menu
			If (pItem = esriTOCControlItem.esriTOCControlItemLayer) Then m_menuSchematicLayer.PopupMenu(e.x, e.y, m_pTocControl.hWnd)
		Else
			'Popup the correct context menu
			If (pItem = esriTOCControlItem.esriTOCControlItemLayer) Then m_menuLayer.PopupMenu(e.x, e.y, m_pTocControl.hWnd)
		End If
	End Sub


End Class