private void CreateLayer(IName name) { ILayerFactoryHelper helper = new LayerFactoryHelperClass(); try { IEnumLayer layer = helper.CreateLayersFromName(name); layer.Reset(); for (ILayer layer2 = layer.Next(); layer2 != null; layer2 = layer.Next()) { int toIndex = 0; toIndex = LocalCommonHelper.GetLayerIndex(this.axMapControl1.Map, layer2); this.axMapControl1.AddLayer(layer2, toIndex); this.axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, layer2, null); } } catch (Exception) { MessageBox.Show("无法打开指定文档!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand); } }
private void axMapControl1_OnOleDrop(object sender, IMapControlEvents2_OnOleDropEvent e) { IDataObjectHelper dataObjectHelper = (IDataObjectHelper)e.dataObjectHelper; esriControlsDropAction dropAction = e.dropAction; e.effect = 0; switch (dropAction) { case esriControlsDropAction.esriDropEnter: if (dataObjectHelper.CanGetFiles() | dataObjectHelper.CanGetNames()) { this.m_Effect = esriControlsDragDropEffect.esriDragDropCopy; } else if (dataObjectHelper.InternalObject is IDataObject) { this.m_Effect = esriControlsDragDropEffect.esriDragDropCopy; } break; case esriControlsDropAction.esriDropOver: e.effect = (int)this.m_Effect; break; } if (dropAction == esriControlsDropAction.esriDropped) { int num; if (dataObjectHelper.CanGetFiles()) { System.Array files = System.Array.CreateInstance(typeof(string), 0, 0); files = (System.Array)dataObjectHelper.GetFiles(); for (num = 0; num <= (files.Length - 1); num++) { if (this.axMapControl1.CheckMxFile(files.GetValue(num).ToString())) { try { this.axMapControl1.LoadMxFile(files.GetValue(num).ToString(), System.Type.Missing, ""); if (ApplicationRef.Application != null) { ApplicationRef.Application.MapDocumentChanged(); } } catch (Exception exception) { MessageBox.Show("Error:" + exception.Message); return; } } else { IFileName name = new FileNameClass { Path = files.GetValue(num).ToString() }; this.CreateLayer((IName)name); if (ApplicationRef.Application != null) { ApplicationRef.Application.MapDocumentChanged(); } } } } else if (dataObjectHelper.CanGetNames()) { IEnumName names = dataObjectHelper.GetNames(); names.Reset(); for (IName name3 = names.Next(); name3 != null; name3 = names.Next()) { this.CreateLayer(name3); } if (ApplicationRef.Application != null) { ApplicationRef.Application.MapDocumentChanged(); } } if (dataObjectHelper.InternalObject is IDataObject) { IList data = (dataObjectHelper.InternalObject as IDataObject).GetData("System.Collections.ArrayList") as IList; for (num = 0; num < data.Count; num++) { IWorkspaceName workspaceName; IWorkspaceName name6; object obj3 = data[num]; if (obj3 is IFeatureDatasetName) { IFeatureDatasetName name4 = new FeatureDatasetNameClass(); workspaceName = (obj3 as IDatasetName).WorkspaceName; name6 = new WorkspaceNameClass { WorkspaceFactoryProgID = workspaceName.WorkspaceFactoryProgID }; if (workspaceName.ConnectionProperties != null) { name6.ConnectionProperties = workspaceName.ConnectionProperties; } else { name6.PathName = workspaceName.PathName; } (name4 as IDatasetName).WorkspaceName = name6; (name4 as IDatasetName).Name = (obj3 as IDatasetName).Name; this.CreateLayer(name4 as IName); } else if (obj3 is IRasterDatasetName) { IRasterDatasetName name7 = new RasterDatasetNameClass(); workspaceName = (obj3 as IDatasetName).WorkspaceName; name6 = new WorkspaceNameClass { WorkspaceFactoryProgID = workspaceName.WorkspaceFactoryProgID }; if (workspaceName.ConnectionProperties != null) { name6.ConnectionProperties = workspaceName.ConnectionProperties; } else { name6.PathName = workspaceName.PathName; } (name7 as IDatasetName).WorkspaceName = name6; (name7 as IDatasetName).Name = (obj3 as IDatasetName).Name; this.CreateLayer(name7 as IName); } else if (obj3 is IDatasetName) { IDatasetName name8 = null; if ((obj3 as IDatasetName).Type == esriDatasetType.esriDTTin) { name8 = new TinNameClass(); } if ((obj3 as IDatasetName).Type == esriDatasetType.esriDTFeatureClass) { name8 = new FeatureClassNameClass(); (name8 as IFeatureClassName).FeatureDatasetName = (obj3 as IFeatureClassName).FeatureDatasetName; } else if ((obj3 as IDatasetName).Type == esriDatasetType.esriDTTopology) { name8 = new TopologyNameClass(); (name8 as ITopologyName).FeatureDatasetName = (obj3 as ITopologyName).FeatureDatasetName; } else if ((obj3 as IDatasetName).Type == esriDatasetType.esriDTRasterBand) { name8 = new RasterBandNameClass(); (name8 as IRasterBandName).RasterDatasetName = (obj3 as IRasterBandName).RasterDatasetName; } else if ((obj3 as IDatasetName).Type == esriDatasetType.esriDTGeometricNetwork) { name8 = new GeometricNetworkNameClass(); (name8 as IGeometricNetworkName).FeatureDatasetName = (obj3 as IGeometricNetworkName).FeatureDatasetName; } else if ((obj3 as IDatasetName).Type == esriDatasetType.esriDTRasterCatalog) { name8 = new RasterCatalogNameClass(); (name8 as IFeatureClassName).FeatureDatasetName = (obj3 as IFeatureClassName).FeatureDatasetName; } else if ((obj3 as IDatasetName).Type == esriDatasetType.esriDTNetworkDataset) { name8 = new NetworkDatasetNameClass(); (name8 as INetworkDatasetName).FeatureDatasetName = (obj3 as INetworkDatasetName).FeatureDatasetName; } if (name8 != null) { if (name8.WorkspaceName == null) { workspaceName = (obj3 as IDatasetName).WorkspaceName; name6 = new WorkspaceNameClass { WorkspaceFactoryProgID = workspaceName.WorkspaceFactoryProgID }; if (workspaceName.ConnectionProperties != null) { name6.ConnectionProperties = workspaceName.ConnectionProperties; } else { name6.PathName = workspaceName.PathName; } name8.WorkspaceName = name6; } name8.Name = (obj3 as IDatasetName).Name; this.CreateLayer(name8 as IName); } } else if (obj3 is IWorkspaceName) { workspaceName = obj3 as IWorkspaceName; name6 = new WorkspaceNameClass { WorkspaceFactoryProgID = workspaceName.WorkspaceFactoryProgID }; if (workspaceName.ConnectionProperties != null) { name6.ConnectionProperties = workspaceName.ConnectionProperties; } else { name6.PathName = workspaceName.PathName; } this.CreateLayer(name6 as IName); } else if (obj3 is IPropertySet) { IPropertySet set = obj3 as IPropertySet; ILayer property = set.GetProperty("Layer") as ILayer; int toIndex = 0; toIndex = LocalCommonHelper.GetLayerIndex(this.axMapControl1.Map, property); this.axMapControl1.AddLayer(property, toIndex); } } if (ApplicationRef.Application != null) { ApplicationRef.Application.MapDocumentChanged(); } } } }