vs2005代码编辑窗口中,右击插入代码段:
Add Shapefile Using OpenFileDialog
1 #region"Add Shapefile Using OpenFileDialog" 2 // ArcGIS Snippet Title: 3 // Add Shapefile Using OpenFileDialog 4 // 5 // Long Description: 6 // Add a shapefile to the ActiveView using the Windows.Forms.OpenFileDialog control. 7 // 8 // Add the following references to the project: 9 // ESRI.ArcGIS.Carto 10 // ESRI.ArcGIS.DataSourcesFile 11 // ESRI.ArcGIS.Display 12 // ESRI.ArcGIS.Geodatabase 13 // ESRI.ArcGIS.Geometry 14 // ESRI.ArcGIS.System 15 // System.Windows.Forms 16 // 17 // Intended ArcGIS Products for this snippet: 18 // ArcGIS Desktop (ArcEditor, ArcInfo, ArcView) 19 // ArcGIS Engine 20 // 21 // Applicable ArcGIS Product Versions: 22 // 9.2 23 // 9.3 24 // 25 // Required ArcGIS Extensions: 26 // (NONE) 27 // 28 // Notes: 29 // This snippet is intended to be inserted at the base level of a Class. 30 // It is not intended to be nested within an existing Method. 31 // 32 33 ///<summary>Add a shapefile to the ActiveView using the Windows.Forms.OpenFileDialog control.</summary> 34 /// 35 ///<param name="activeView">An IActiveView interface</param> 36 /// 37 ///<remarks></remarks> 38 public void AddShapefileUsingOpenFileDialog(ESRI.ArcGIS.Carto.IActiveView activeView) 39 { 40 //parameter check 41 if (activeView == null) 42 { 43 return; 44 } 45 46 // Use the OpenFileDialog Class to choose which shapefile to load. 47 System.Windows.Forms.OpenFileDialog openFileDialog = new System.Windows.Forms.OpenFileDialog(); 48 openFileDialog.InitialDirectory = @"F:\ArcGis学习\Arcgis实验-杨克诚编写\Ex11"; 49 openFileDialog.Filter = "Shapefiles (*.shp)|*.shp"; 50 openFileDialog.FilterIndex = 2; 51 openFileDialog.RestoreDirectory = true; 52 openFileDialog.Multiselect = false; 53 54 55 if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) 56 { 57 // The user chose a particular shapefile. 58 59 // The returned string will be the full path, filename and file-extension for the chosen shapefile. Example: "C:\test\cities.shp" 60 string shapefileLocation = openFileDialog.FileName; 61 62 if (shapefileLocation != "") 63 { 64 // Ensure the user chooses a shapefile 65 66 // Create a new ShapefileWorkspaceFactory CoClass to create a new workspace 67 ESRI.ArcGIS.Geodatabase.IWorkspaceFactory workspaceFactory = new ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactoryClass(); 68 69 // System.IO.Path.GetDirectoryName(shapefileLocation) returns the directory part of the string. Example: "C:\test\" 70 ESRI.ArcGIS.Geodatabase.IFeatureWorkspace featureWorkspace = (ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)workspaceFactory.OpenFromFile(System.IO.Path.GetDirectoryName(shapefileLocation), 0); // Explicit Cast 71 72 // System.IO.Path.GetFileNameWithoutExtension(shapefileLocation) returns the base filename (without extension). Example: "cities" 73 try 74 { 75 ESRI.ArcGIS.Geodatabase.IFeatureClass featureClass = featureWorkspace.OpenFeatureClass(System.IO.Path.GetFileNameWithoutExtension(shapefileLocation)); 76 77 78 ESRI.ArcGIS.Carto.IFeatureLayer featureLayer = new ESRI.ArcGIS.Carto.FeatureLayerClass(); 79 featureLayer.FeatureClass = featureClass; 80 featureLayer.Name = featureClass.AliasName; 81 featureLayer.Visible = true; 82 activeView.FocusMap.AddLayer(featureLayer); 83 84 // Zoom the display to the full extent of all layers in the map 85 activeView.Extent = activeView.FullExtent; 86 activeView.PartialRefresh(ESRI.ArcGIS.Carto.esriViewDrawPhase.esriViewGeography, null, null); 87 } 88 catch (Exception m) 89 { 90 MessageBox.Show(m.Message); 91 } 92 } 93 else 94 { 95 // The user did not choose a shapefile. 96 // Do whatever remedial actions as necessary 97 System.Windows.Forms.MessageBox.Show("No shapefile chosen", "No Choice #1", 98 System.Windows.Forms.MessageBoxButtons.OK, 99 System.Windows.Forms.MessageBoxIcon.Exclamation); 100 } 101 } 102 else 103 { 104 // The user did not choose a shapefile. They clicked Cancel or closed the dialog by the "X" button. 105 // Do whatever remedial actions as necessary. 106 System.Windows.Forms.MessageBox.Show("No shapefile chosen", "No Choice #2", 107 System.Windows.Forms.MessageBoxButtons.OK, 108 System.Windows.Forms.MessageBoxIcon.Exclamation); 109 } 110 } 111 #endregion
如何调用?
private void button1_Click(object sender, EventArgs e) { AddShapefileUsingOpenFileDialog(axMapControl1.ActiveView); }
注意:参数。(axMapControl1.ActiveView)
Classes that implement ICommand:
自定义用户放大工具:
private void button2_Click(object sender, EventArgs e) { ICommand command = new ControlsMapZoomInTool(); command.OnCreate(axMapControl1.Object); axMapControl1.CurrentTool = command as ITool; //ICommand p = new ControlsMapZoomInFixedCommandClass(); //p.OnCreate(axMapControl1.Object); //axMapControl1.CurrentTool = p as ITool; //ICommand p = new ControlsMapZoomInFixedCommandClass(); //p.OnCreate(axMapControl1.Object); //axMapControl1.CurrentTool = p as ITool; }
自定义用户缩小大工具:
private void button3_Click(object sender, EventArgs e) { ICommand pCmd = new ControlsMapZoomOutToolClass(); pCmd.OnCreate(axMapControl1.Object); axMapControl1.CurrentTool = pCmd as ITool; }
程序界面:
添加engine右键菜单:
代码如下:
EngineContextMenu1
1 using System; 2 using System.Runtime.InteropServices; 3 using ESRI.ArcGIS.esriSystem; 4 using ESRI.ArcGIS.SystemUI; 5 using ESRI.ArcGIS.Controls; 6 7 namespace WindowsApplication10 8 { 9 /// <summary> 10 /// Context menu class for Engine applications. 11 ///</summary> 12 [Guid("0b78c5ca-3e39-4f54-99d2-df4cbcd9ba26")] 13 [ClassInterface(ClassInterfaceType.None)] 14 [ProgId("WindowsApplication10.EngineContextMenu1")] 15 public class EngineContextMenu1 16 { 17 private IToolbarMenu2 m_toolbarMenu = null; 18 private bool m_beginGroupFlag = false; 19 20 public EngineContextMenu1() 21 { 22 } 23 24 /// <summary> 25 /// Instantiate the underlying ToolbarMenu and set the hook object to be 26 /// passed into the OnCreate event of each command item. 27 /// </summary> 28 public void SetHook(object hook) 29 { 30 m_toolbarMenu = new ToolbarMenuClass(); 31 m_toolbarMenu.SetHook(hook); 32 33 // 34 // TODO: Define context menu items here 35 // 36 AddItem("esriControls.ControlsMapZoomOutFixedCommand", -1); 37 AddItem("esriControls.ControlsMapZoomInFixedCommand", -1); 38 BeginGroup(); //Separator 39 AddItem("{380FB31E-6C24-4F5C-B1DF-47F33586B885}", -1); //undo command 40 AddItem(new Guid("B0675372-0271-4680-9A2C-269B3F0C01E8"), -1); //redo command 41 BeginGroup(); //Separator 42 //AddItem("MyCustomCommandCLSIDorProgID", -1); 43 } 44 45 /// <summary> 46 /// Popup the context menu at the given location 47 /// </summary> 48 /// <param name="X">X coordinate where to popup the menu</param> 49 /// <param name="Y">Y coordinate where to popup the menu</param> 50 /// <param name="hWndParent">Handle to the parent window</param> 51 public void PopupMenu(int X, int Y, int hWndParent) 52 { 53 if (m_toolbarMenu != null) 54 m_toolbarMenu.PopupMenu(X, Y, hWndParent); 55 } 56 57 /// <summary> 58 /// Retrieve the ToolbarMenu object in case if needed to be modified at 59 /// run time. 60 /// </summary> 61 public IToolbarMenu2 ContextMenu 62 { 63 get 64 { 65 return m_toolbarMenu; 66 } 67 } 68 69 #region Helper methods to add items to the context menu 70 /// <summary> 71 /// Adds a separator bar on the command bar to begin a group. 72 /// </summary> 73 private void BeginGroup() 74 { 75 m_beginGroupFlag = true; 76 } 77 78 /// <summary> 79 /// Add a command item to the command bar by an Unique Identifier Object (UID). 80 /// </summary> 81 private void AddItem(UID itemUID) 82 { 83 m_toolbarMenu.AddItem(itemUID.Value, itemUID.SubType, -1, m_beginGroupFlag, esriCommandStyles.esriCommandStyleIconAndText); 84 m_beginGroupFlag = false; //Reset group flag 85 } 86 87 /// <summary> 88 /// Add a command item to the command bar by an identifier string 89 /// and a subtype index 90 /// </summary> 91 private void AddItem(string itemID, int subtype) 92 { 93 UID itemUID = new UIDClass(); 94 try 95 { 96 itemUID.Value = itemID; 97 } 98 catch 99 { 100 //Add an empty guid to indicate something's wrong "Missing" 101 itemUID.Value = Guid.Empty.ToString("B"); 102 } 103 104 if (subtype > 0) 105 itemUID.SubType = subtype; 106 AddItem(itemUID); 107 108 } 109 110 /// <summary> 111 /// Add a command item to the command bar by the Guid 112 /// and a subtype index. 113 /// </summary> 114 private void AddItem(Guid itemGuid, int subtype) 115 { 116 AddItem(itemGuid.ToString("B"), subtype); 117 } 118 119 /// <summary> 120 /// Add a command item to the command bar by a type 121 /// and a subtype index. 122 /// </summary> 123 private void AddItem(Type itemType, int subtype) 124 { 125 if (itemType != null) 126 AddItem(itemType.GUID, subtype); 127 } 128 129 #endregion 130 131 } 132 }
完整代码:
View Code
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Text; 7 using System.Windows.Forms; 8 using ESRI.ArcGIS.Carto; 9 using ESRI.ArcGIS.DataSourcesFile; 10 using ESRI.ArcGIS.Display; 11 using ESRI.ArcGIS.Geodatabase; 12 using ESRI.ArcGIS.Geometry; 13 using ESRI.ArcGIS.SystemUI; 14 15 namespace WindowsApplication10 16 { 17 public partial class Form1 : Form 18 { 19 public Form1() 20 { 21 InitializeComponent(); 22 } 23 24 #region"Add Shapefile Using OpenFileDialog" 25 // ArcGIS Snippet Title: 26 // Add Shapefile Using OpenFileDialog 27 // 28 // Long Description: 29 // Add a shapefile to the ActiveView using the Windows.Forms.OpenFileDialog control. 30 // 31 // Add the following references to the project: 32 // ESRI.ArcGIS.Carto 33 // ESRI.ArcGIS.DataSourcesFile 34 // ESRI.ArcGIS.Display 35 // ESRI.ArcGIS.Geodatabase 36 // ESRI.ArcGIS.Geometry 37 // ESRI.ArcGIS.System 38 // System.Windows.Forms 39 // 40 // Intended ArcGIS Products for this snippet: 41 // ArcGIS Desktop (ArcEditor, ArcInfo, ArcView) 42 // ArcGIS Engine 43 // 44 // Applicable ArcGIS Product Versions: 45 // 9.2 46 // 9.3 47 // 48 // Required ArcGIS Extensions: 49 // (NONE) 50 // 51 // Notes: 52 // This snippet is intended to be inserted at the base level of a Class. 53 // It is not intended to be nested within an existing Method. 54 // 55 56 ///<summary>Add a shapefile to the ActiveView using the Windows.Forms.OpenFileDialog control.</summary> 57 /// 58 ///<param name="activeView">An IActiveView interface</param> 59 /// 60 ///<remarks></remarks> 61 public void AddShapefileUsingOpenFileDialog(ESRI.ArcGIS.Carto.IActiveView activeView) 62 { 63 //parameter check 64 if (activeView == null) 65 { 66 return; 67 } 68 69 // Use the OpenFileDialog Class to choose which shapefile to load. 70 System.Windows.Forms.OpenFileDialog openFileDialog = new System.Windows.Forms.OpenFileDialog(); 71 openFileDialog.InitialDirectory = "c:\\"; 72 openFileDialog.Filter = "Shapefiles (*.shp)|*.shp"; 73 openFileDialog.FilterIndex = 2; 74 openFileDialog.RestoreDirectory = true; 75 openFileDialog.Multiselect = false; 76 77 78 if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) 79 { 80 // The user chose a particular shapefile. 81 82 // The returned string will be the full path, filename and file-extension for the chosen shapefile. Example: "C:\test\cities.shp" 83 string shapefileLocation = openFileDialog.FileName; 84 85 if (shapefileLocation != "") 86 { 87 // Ensure the user chooses a shapefile 88 89 // Create a new ShapefileWorkspaceFactory CoClass to create a new workspace 90 ESRI.ArcGIS.Geodatabase.IWorkspaceFactory workspaceFactory = new ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactoryClass(); 91 92 // System.IO.Path.GetDirectoryName(shapefileLocation) returns the directory part of the string. Example: "C:\test\" 93 ESRI.ArcGIS.Geodatabase.IFeatureWorkspace featureWorkspace = (ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)workspaceFactory.OpenFromFile(System.IO.Path.GetDirectoryName(shapefileLocation), 0); // Explicit Cast 94 95 // System.IO.Path.GetFileNameWithoutExtension(shapefileLocation) returns the base filename (without extension). Example: "cities" 96 ESRI.ArcGIS.Geodatabase.IFeatureClass featureClass = featureWorkspace.OpenFeatureClass(System.IO.Path.GetFileNameWithoutExtension(shapefileLocation)); 97 98 ESRI.ArcGIS.Carto.IFeatureLayer featureLayer = new ESRI.ArcGIS.Carto.FeatureLayerClass(); 99 featureLayer.FeatureClass = featureClass; 100 featureLayer.Name = featureClass.AliasName; 101 featureLayer.Visible = true; 102 activeView.FocusMap.AddLayer(featureLayer); 103 104 // Zoom the display to the full extent of all layers in the map 105 activeView.Extent = activeView.FullExtent; 106 activeView.PartialRefresh(ESRI.ArcGIS.Carto.esriViewDrawPhase.esriViewGeography, null, null); 107 } 108 else 109 { 110 // The user did not choose a shapefile. 111 // Do whatever remedial actions as necessary 112 // System.Windows.Forms.MessageBox.Show("No shapefile chosen", "No Choice #1", 113 // System.Windows.Forms.MessageBoxButtons.OK, 114 // System.Windows.Forms.MessageBoxIcon.Exclamation); 115 } 116 } 117 else 118 { 119 // The user did not choose a shapefile. They clicked Cancel or closed the dialog by the "X" button. 120 // Do whatever remedial actions as necessary. 121 // System.Windows.Forms.MessageBox.Show("No shapefile chosen", "No Choice #2", 122 // System.Windows.Forms.MessageBoxButtons.OK, 123 // System.Windows.Forms.MessageBoxIcon.Exclamation); 124 } 125 } 126 #endregion 127 128 EngineContextMenu1 mycontext = new EngineContextMenu1(); 129 private void button1_Click(object sender, EventArgs e) 130 { 131 AddShapefileUsingOpenFileDialog(axMapControl1.ActiveView); 132 } 133 134 private void axMapControl1_OnMouseDown(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseDownEvent e) 135 { 136 //mycontext.SetHook(axMapControl1.Object); 137 if (e.button == 2) 138 { 139 mycontext.PopupMenu(e.x,e.y,axMapControl1.hWnd); 140 } 141 } 142 143 private void Form1_Load(object sender, EventArgs e) 144 { 145 mycontext.SetHook(axMapControl1.Object); 146 } 147 } 148 }