www.pudn.com > AS15044.zip > Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geometry;
namespace DemoControls
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private EngineInkEnvironment m_EngineInkEnvironment = new EngineInkEnvironmentClass();
private void Form1_Load(object sender, EventArgs e)
{
#region Load MXD file
string filePath = @"c:\engine controls\engine controls final.mxd";
if (axMapControl1.CheckMxFile(filePath))
axMapControl1.LoadMxFile(filePath);
else
MessageBox.Show("File not present: " + filePath, "Engine Controls");
#endregion
#region MAPCONTROL
IToolbarControl mapToolBarControl;
mapToolBarControl = (IToolbarControl)axToolbarControl1.Object;
AddMapCommandsToToolbarControl(mapToolBarControl);
mapToolBarControl.SetBuddyControl(axMapControl1);
CreateToolbarMenu(mapToolBarControl);
CreateToolbarPalette(mapToolBarControl);
//Demo3 - Add SaveLayer command to the ToolbarControl
mapToolBarControl.AddItem("CustomCommands.SaveLayer", -1, -1, false, -1, esriCommandStyles.esriCommandStyleIconOnly);
//Automatically commit ink
m_EngineInkEnvironment.ToolCommitType = esriEngineInkToolCommitType.esriEngineInkToolCommitTypeAutoGraphic;
m_EngineInkEnvironment.ToolCommitDelay = 1;
//CustomizeDialog
CreateCustomizeDialog(mapToolBarControl);
#endregion
#region PAGELAYOUTCONTROL
IToolbarControl pageToolBarControl;
pageToolBarControl = (IToolbarControl)axToolbarControl2.Object;
AddPageLayoutCommandsToToolbarControl(pageToolBarControl);
pageToolBarControl.AddItem("esriControls.ControlsMapZoomInTool", -1, -1, true, 0, ESRI.ArcGIS.SystemUI.esriCommandStyles.esriCommandStyleIconOnly);
pageToolBarControl.AddItem("esriControls.ControlsMapZoomOutTool", -1, -1, true, 0, ESRI.ArcGIS.SystemUI.esriCommandStyles.esriCommandStyleIconOnly);
pageToolBarControl.SetBuddyControl(axPageLayoutControl1);
#endregion
#region TOCCONTROL
ITOCControl2 mapTOCControl;
mapTOCControl = (ITOCControl2)axTOCControl1.Object;
mapTOCControl.EnableLayerDragDrop = true;
mapTOCControl.SetBuddyControl(axMapControl1);
#endregion
}
//SNIPPETS
#region "Add Map Commands to ToolbarControl"
// ArcGIS Snippet Title:
// Add Map Commands to ToolbarControl
//
// Add the following references to the project:
// ESRI.ArcGIS.Controls
// ESRI.ArcGIS.SystemUI
//
// Intended ArcGIS Products for this snippet:
// ArcGIS Engine
//
// Required ArcGIS Extensions:
// (NONE)
//
// Notes:
// This snippet is intended to be inserted at the base level of a Class.
// It is not intended to be nested within an existing Method.
//
// Use the following XML documentation comments to use this snippet:
/// Add Map Commands to ToolbarControl.
///
/// An IToolbarControl inferface to add map commands to.
///
///
/// Call to add the map commands to the ToolbarControl
/// For example, if a ToolbarControl named axToolbarControl1 exists use the following code:
/// AddMapCommandsToToolbarControl(axToolbarControl1.Object as ESRI.ArcGIS.Controls.IToolbarControl);
///
public void AddMapCommandsToToolbarControl(ESRI.ArcGIS.Controls.IToolbarControl toolbarControl)
{
toolbarControl.AddItem("esriControls.ControlsAddDataCommand", -1, -1, true, 0, ESRI.ArcGIS.SystemUI.esriCommandStyles.esriCommandStyleIconOnly);
toolbarControl.AddToolbarDef("esriControls.ControlsMapNavigationToolbar", -1, false, 0, ESRI.ArcGIS.SystemUI.esriCommandStyles.esriCommandStyleIconOnly);
toolbarControl.AddItem("esriControls.ControlsLayerListToolControl", -1, -1, false, 0, ESRI.ArcGIS.SystemUI.esriCommandStyles.esriCommandStyleIconOnly);
toolbarControl.AddItem("esriControls.ControlsLayerTransparencyCommand", -1, -1, false, 0, ESRI.ArcGIS.SystemUI.esriCommandStyles.esriCommandStyleIconOnly);
toolbarControl.AddItem("esriControls.ControlsMapSwipeTool", -1, -1, false, 0, ESRI.ArcGIS.SystemUI.esriCommandStyles.esriCommandStyleIconOnly);
}
#endregion
#region "Add Page Layout Commands to ToolbarControl"
// ArcGIS Snippet Title:
// Add Page Layout Commands to ToolbarControl
//
// Add the following references to the project:
// ESRI.ArcGIS.Controls
// ESRI.ArcGIS.SystemUI
//
// Intended ArcGIS Products for this snippet:
// ArcGIS Engine
//
// Required ArcGIS Extensions:
// (NONE)
//
// Interfaces and Classes used:
// ESRI.ArcGIS.Controls.IToolbarControl
// ESRI.ArcGIS.SystemUI.esriCommandStyles.esriCommandStyleIconOnly
//
// Notes:
// This snippet is intended to be inserted at the base level of a Class.
// It is not intended to be nested within an existing Method.
//
// GUID:
// {73A51A23-E872-4e54-887D-9262A1C73E22}
//
// Use the following XML documentation comments to use this snippet:
/// Add Page Layout Commands to ToolbarControl
///
/// An IToolbarControl interface to which Page Layout Controls will be added.
///
///
/// Call to add the page layout commands to the ToolbarControl
/// For example, if a ToolbarControl named axToolbarControl1 exists use the following code:
/// AddPageLayoutCommandsToToolbarControl(axToolbarControl1.Object as ESRI.ArcGIS.Controls.IToolbarControl);
///
public void AddPageLayoutCommandsToToolbarControl(ESRI.ArcGIS.Controls.IToolbarControl toolbarControl)
{
toolbarControl.AddToolbarDef("esriControls.ControlsPageLayoutToolbar", -1, false, 0, ESRI.ArcGIS.SystemUI.esriCommandStyles.esriCommandStyleIconOnly);
}
#endregion
#region "Customize ToolbarControl"
// ArcGIS Snippet Title:
// Customize ToolbarControl
//
// Add the following references to the project:
// ESRI.ArcGIS.Controls
// ESRI.ArcGIS.Display
// ESRI.ArcGIS.System
//
// Intended ArcGIS Products for this snippet:
// ArcGIS Engine
//
// Required ArcGIS Extensions:
// (NONE)
//
// Notes:
// This snippet is intended to be inserted at the base level of a Class.
// It is not intended to be nested within an existing Method.
//
// Use the following XML documentation comments to use this snippet:
/// Use the customize dialog to modify a ToolbarControl.
///
/// This snippet assumes a ToolbarControl is embedded into a container.
// The ToolbarControl
public ESRI.ArcGIS.Controls.IToolbarControl m_ToolbarControl;
//The CustomizeDialog used by the ToolbarControl
public ESRI.ArcGIS.Controls.ICustomizeDialog m_CustomizeDialog;
//The CustomizeDialog start event
public ESRI.ArcGIS.Controls.ICustomizeDialogEvents_OnStartDialogEventHandler startDialogE;
//The CustomizeDialog close event
public ESRI.ArcGIS.Controls.ICustomizeDialogEvents_OnCloseDialogEventHandler closeDialogE;
// Call to create the CustomizeDialog and wire its events
// For example, if a ToolbarControl named axToolbarControl1 exists use the following code:
// CreateCustomizeDialog(axToolbarControl1.Object as ESRI.ArcGIS.Controls.IToolbarControl);
// To display the CustomizeDialog and customize the ToolbarControl use the following code:
// m_CustomizeDialog.StartDialog(m_ToolbarControl.hWnd);
public void CreateCustomizeDialog(ESRI.ArcGIS.Controls.IToolbarControl toolbarControl)
{
//Set the ToolbarControl
m_ToolbarControl = toolbarControl;
//Create new customize dialog
m_CustomizeDialog = new ESRI.ArcGIS.Controls.CustomizeDialogClass();
//Set the title
m_CustomizeDialog.DialogTitle = "Customize ToolbarControl Items";
//Show the 'Add from File' button
m_CustomizeDialog.ShowAddFromFile = true;
//Set the ToolbarControl that new items will be added to
m_CustomizeDialog.SetDoubleClickDestination(m_ToolbarControl);
//Set the customize dialog events
startDialogE = new ESRI.ArcGIS.Controls.ICustomizeDialogEvents_OnStartDialogEventHandler(OnStartDialog);
((ESRI.ArcGIS.Controls.ICustomizeDialogEvents_Event)m_CustomizeDialog).OnStartDialog += startDialogE;
closeDialogE = new ESRI.ArcGIS.Controls.ICustomizeDialogEvents_OnCloseDialogEventHandler(OnCloseDialog);
((ESRI.ArcGIS.Controls.ICustomizeDialogEvents_Event)m_CustomizeDialog).OnCloseDialog += closeDialogE;
}
public void OnStartDialog()
{
//Put the ToolbarControl into customize mode
m_ToolbarControl.Customize = true;
}
public void OnCloseDialog()
{
//Take the ToolbarControl out of customize mode
m_ToolbarControl.Customize = false;
chkCustomize.Checked = false;
}
#endregion
#region "Create PopUp ToolbarMenu"
// ArcGIS Snippet Title:
// Create PopUp ToolbarMenu
//
// Add the following references to the project:
// ESRI.ArcGIS.Controls
// ESRI.ArcGIS.System
// ESRI.ArcGIS.SystemUI
//
// Intended ArcGIS Products for this snippet:
// ArcGIS Engine
//
// Required ArcGIS Extensions:
// (NONE)
//
// Notes:
// This snippet is intended to be inserted at the base level of a Class.
// It is not intended to be nested within an existing Method.
//
// Use the following XML documentation comments to use this snippet:
/// Stub code for creating a popup or context menu.
///
/// This snippet assumes a ToolbarControl has a MapControl, PageLayoutControl GlobeControl or SceneControl buddy set.
public ESRI.ArcGIS.Controls.IToolbarMenu m_ToolbarMenu;
// Call to create a ToolbarMenu. For example, if a ToolbarControl named axToolbarControl1 exists use the following code:
// CreateToolbarMenu(axToolbarControl1.Object as ESRI.ArcGIS.Controls.IToolbarControl);
public void CreateToolbarMenu(ESRI.ArcGIS.Controls.IToolbarControl toolbarControl)
{
// Create a new ToolbarMenu
m_ToolbarMenu = new ESRI.ArcGIS.Controls.ToolbarMenuClass();
// Share the ToolbarControl's command pool
m_ToolbarMenu.CommandPool = toolbarControl.CommandPool;
// Set the hook to be the toolbarControl
m_ToolbarMenu.SetHook(toolbarControl);
// Add commands items to the ToolbarMenu.
// Add additional commands based on the mapping control being used
// For a listing of the ArcGIS Engine Controls CLSID or ProgID see: ms-help://MS.VSCC.v80/MS.VSIPCC.v80/ESRI.EDNv9.2/NET_Engine/shared/reference/guids_ctrls_cmds.htm
m_ToolbarMenu.AddItem("esriControls.ControlsAddDataCommand", -1, -1, false, ESRI.ArcGIS.SystemUI.esriCommandStyles.esriCommandStyleIconOnly);
m_ToolbarMenu.AddItem("esriControls.ControlsFullScreenCommand", -1, -1, false, ESRI.ArcGIS.SystemUI.esriCommandStyles.esriCommandStyleIconOnly);
m_ToolbarMenu.AddSubMenu("esriControls.ControlsMapBookmarkMenu", -1, false);
}
// Call to popup the ToolbarMenu on the display of the ToolbarControl's buddy.
// For example, if axToolbarControl1 is buddied to axMapControl1 use the following code from one of the MapControl's mouse events:
// PopupToolbarMenu(e.x, e.y, axMapControl1.hWnd);
public void PopupToolbarMenu(System.Int32 x, System.Int32 y, System.Int32 hWnd)
{
m_ToolbarMenu.PopupMenu(x, y, hWnd);
}
#endregion
#region "Create PopUp ToolbarPalette"
// ArcGIS Snippet Title:
// Create PopUp ToolbarPalette
//
// Add the following references to the project:
// ESRI.ArcGIS.Controls
// ESRI.ArcGIS.System
//
// Intended ArcGIS Products for this snippet:
// ArcGIS Engine
//
// Required ArcGIS Extensions:
// (NONE)
//
// Notes:
// This snippet is intended to be inserted at the base level of a Class.
// It is not intended to be nested within an existing Method.
//
// Use the following XML documentation comments to use this snippet:
/// Stub code for creating a popup palette.
///
/// This snippet assumes a ToolbarControl has a MapControl, PageLayoutControl GlobeControl or SceneControl buddy set.
public ESRI.ArcGIS.Controls.IToolbarPalette m_ToolbarPalette;
// Call to create a ToolbarPalette. For example, if a ToolbarControl named axToolbarControl1 exists use the following code:
// CreateToolbarPalette(axToolbarControl1.Object as ESRI.ArcGIS.Controls.IToolbarControl);
public void CreateToolbarPalette(ESRI.ArcGIS.Controls.IToolbarControl toolbarControl)
{
// Create a new ToolbarPalette
m_ToolbarPalette = new ESRI.ArcGIS.Controls.ToolbarPaletteClass();
// Share the ToolbarControl's command pool
m_ToolbarPalette.CommandPool = toolbarControl.CommandPool;
// Set the hook
m_ToolbarPalette.SetHook(toolbarControl);
// Add commands items to the ToolbarPalette.
// Add additional commands based on the mapping control being used
// For a listing of the ArcGIS Engine Controls CLSID or ProgID see: ms-help://MS.VSCC.v80/MS.VSIPCC.v80/ESRI.EDNv9.2/NET_Engine/shared/reference/guids_ctrls_cmds.htm
m_ToolbarPalette.AddItem("esriControls.ControlsSelectTool", -1, -1);
m_ToolbarPalette.AddItem("esriControls.ControlsInkHighlightTool", -1, -1);
m_ToolbarPalette.AddItem("esriControls.ControlsInkPenTool", -1, -1);
}
// Call to popup the ToolbarPalette on the display of the ToolbarControl's buddy.
// For example, if axToolbarControl1 is buddied to axMapControl1 use the following code from one of the MapControl's mouse events:
// PopupToolbarPalette(e.x, e.y, axMapControl1.hWnd);
public void PopupToolbarPalette(System.Int32 x, System.Int32 y, System.Int32 hWnd)
{
m_ToolbarPalette.PopupPalette(x, y, hWnd);
}
#endregion
//FORM EVENTS
private void axMapControl1_OnMouseUp(object sender, IMapControlEvents2_OnMouseUpEvent e)
{
if (e.button == 2)
{
if (e.shift == 1)
PopupToolbarPalette(e.x, e.y, axMapControl1.hWnd);
else
PopupToolbarMenu(e.x, e.y, axMapControl1.hWnd);
}
}
private void axTOCControl1_OnMouseDown(object sender, ITOCControlEvents_OnMouseDownEvent e)
{
//Exit if not right mouse button
if (e.button != 2) return;
IBasicMap map = new MapClass();
ILayer layer = new FeatureLayerClass();
object other = new Object();
object index = new Object();
esriTOCControlItem item = new esriTOCControlItem();
//Determine what kind of item has been clicked on
axTOCControl1.HitTest(e.x, e.y, ref item, ref map, ref layer, ref other, ref index);
//QI to IFeatureLayer and IGeoFeatuerLayer interface
if (layer == null) return;
IFeatureLayer featureLayer = layer as IFeatureLayer;
if (featureLayer == null) return;
IGeoFeatureLayer geoFeatureLayer = (IGeoFeatureLayer)featureLayer;
ISimpleRenderer simpleRenderer = (ISimpleRenderer)geoFeatureLayer.Renderer;
//Create the form with the SymbologyControl
frmSymbol symbolForm = new frmSymbol();
//Get the IStyleGalleryItem
IStyleGalleryItem styleGalleryItem = null;
//Select SymbologyStyleClass based upon feature type
switch (featureLayer.FeatureClass.ShapeType)
{
case esriGeometryType.esriGeometryPoint:
styleGalleryItem = symbolForm.GetItem(esriSymbologyStyleClass.esriStyleClassMarkerSymbols, simpleRenderer.Symbol);
break;
case esriGeometryType.esriGeometryPolyline:
styleGalleryItem = symbolForm.GetItem(esriSymbologyStyleClass.esriStyleClassLineSymbols, simpleRenderer.Symbol);
break;
case esriGeometryType.esriGeometryPolygon:
styleGalleryItem = symbolForm.GetItem(esriSymbologyStyleClass.esriStyleClassFillSymbols, simpleRenderer.Symbol);
break;
}
//Release the form
symbolForm.Dispose();
this.Activate();
if (styleGalleryItem == null) return;
//Create a new renderer
simpleRenderer = new SimpleRendererClass();
//Set its symbol from the styleGalleryItem
simpleRenderer.Symbol = (ISymbol)styleGalleryItem.Item;
//Set the renderer into the geoFeatureLayer
geoFeatureLayer.Renderer = (IFeatureRenderer)simpleRenderer;
//Fire contents changed event that the TOCControl listens to
axMapControl1.ActiveView.ContentsChanged();
//Refresh the display
axMapControl1.Refresh(esriViewDrawPhase.esriViewGeography, null, null);
}
private void chkDynamicMapDisplay_CheckedChanged(object sender, EventArgs e)
{
IDynamicMap dynamicMap = axMapControl1.Map as IDynamicMap;
//Make MapDisplay Dynamic
if (chkDynamicMapDisplay.Checked == false)
dynamicMap.DynamicMapEnabled = false;
else
dynamicMap.DynamicMapEnabled = true;
}
private void chkCustomize_CheckedChanged(object sender, EventArgs e)
{
//Show or hide the customize dialog
if (chkCustomize.Checked == false)
m_CustomizeDialog.CloseDialog();
else
m_CustomizeDialog.StartDialog(axMapControl1.hWnd);
}
}
}