AO操作-拖拽打开地图
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
5.Manage the esriDropped action of the onOLEDrop event:
When data is dropped onto the MapControl, the OnOleDrop event is triggered with an esriDropped action. The IDataObjectHelper interface is used to obtain an array of file path strings from the data using the GetFiles method if the CanGetFiles method returns true; otherwise, an IEnumName collection is obtained using the GetNames method. The CheckMxFile method is used to determine whether each file path in the array is a valid Mx document. If valid, the LoadMxFile method is used to load the document into the MapControl; otherwise, a new IFileName object is created with its path property set to the file path in the array. IFileName is used to create a layer that can be added to the MapControl in the same way that the INames obtained by enumerating the IEnumName collection do.
Copy and paste the following axMapControl1_OnOleDrop method (for Steps 3-5) and the CreateLayer method (for Step 6) into your application where appropriate: [C#] private void axMapControl1_OnOleDrop(object sender,
ESRI.ArcGIS.Controls.IMapControlEvents2_OnOleDropEvent e) {
//QI for IDataObjectHelper. IDataObjectHelper dataObject = (IDataObjectHelper)e.dataObjectHelper; esriControlsDropAction action = e.dropAction; e.effect = (int)esriControlsDragDropEffect.esriDragDropNone; //If the mouse has just entered the MapControl. if (action == esriControlsDropAction.esriDropEnter) {
Dropping data onto the MapControl
Do the following steps to drop data onto the MapControl:
1.Set the IMapControl4.OleDropEnabled property to true.
2.Set the IMapControlEvents2.OnOleDrop event to axMapControl1_OnOleDrop.
//If the data is from Windows Explorer or ArcCatalog. if (dataObject.CanGetFiles() | dataObject.CanGetNames()) {
//Store the copy effect. m_Effect = esriControlsDragDropEffect.esriDragDropCopy; } } //If the mouse is moving over the MapControl. if (action == esriControlsDropAction.esriDropOver) { //Set the effect. e.effect = (int)m_Effect; } //If data is dropped onto the MapControl. if (action == esriControlsDropAction.esriDropped) { //If the data is from Windows Explorer. if (dataObject.CanGetFiles() == true) { //Get an array of file paths through the IDataObjectHelper. System.Array filePaths百度文库= System.Array.CreateInstance(typeof(string), 0,
IEnumLayer enumLayer = layerFactoryHelper.CreateLayersFromName(name); enumLayer.Reset(); //Get the ILayer interface. ILayer layer = enumLayer.Next(); //Loop through layers. while (layer != null) {
0); filePaths = (System.Array)dataObject.GetFiles(); //Loop through the array. for (int i = 0; i <= filePaths.Length - 1; i++) {
//If a valid Mx document, load it into the MapControl. if (axMapControl1.CheckMxFile(filePaths.GetValue(i).ToString()) ==
4.Manage the esriDropOver action of the onOLEDrop event:
When data is being dragged around the MapControl, the OnOleDrop event is triggered with an esriDropOver action. The DragDropEffect is set to the effect stored when the data initially entered the control.
//Create a layer or layers from a name and add to MapControl.
private void CreateLayer(IName name) {
//Set the mouse pointer. axMapControl1.MousePointer = esriControlsMousePointer.esriPointerHourglass; //Get the ILayerFactoryHelper interface. ILayerFactoryHelper layerFactoryHelper = new LayerFactoryHelperClass(); //Get the IEnumLayer interface through the ILayerFatcoryHelper interface. try {
3.Manage the esriDropEnter action of the onOLEDrop event:
When dragged, data initially enters the MapControl and the OnOleDrop event is triggered with an esriDropEnter action. The IDataObjectHelper interface is used to determine whether the data is being dragged from ArcCatalog or Windows Explorer using the CanGetNames and CanGetFiles methods. If the data is from ArcCatalog or Windows Explorer, the DragDropEffect is set to esriDragDropCopy; otherwise, it is set to esriDragDropNone. The DragDropEffect is then stored for later use, for example, when the same data is dragged around or dropped onto the MapControl.
If dropped from ArcCatalog, Mx document and layer (.lyr) files will return true for CanGetFiles as they are both a type of IFileName.
6.Call the CreateLayersFromName method to create a collection of ILayers by using the ILayerFactoryHelper interface. Each layer is then added to the MapControl using the AddLayer method.
true) {
try {
axMapControl1.LoadMxFile(filePaths.GetValue(i).ToString(), Type.Missing, "");
} catch (System.Exception ex) {
MessageBox.Show("Error:" + ex.Message); return ; } } else { //Get the IFileName interface and set its path. IFileName fileName = new FileNameClass(); fileName.Path = filePaths.GetValue(i).ToString(); //Create a map layer. CreateLayer((IName)fileName); } } } //If data is from ArcCatalog. else if (dataObject.CanGetNames() == true) { //Get the IEnumName interface through the IDataObjectHelper. IEnumName enumName = dataObject.GetNames(); enumName.Reset(); //Get the IName interface. IName name = enumName.Next(); //Loop through the names. while (name != null) { //Create a map layer. CreateLayer(name); name = enumName.Next(); } } } }
When data is dropped onto the MapControl, the OnOleDrop event is triggered with an esriDropped action. The IDataObjectHelper interface is used to obtain an array of file path strings from the data using the GetFiles method if the CanGetFiles method returns true; otherwise, an IEnumName collection is obtained using the GetNames method. The CheckMxFile method is used to determine whether each file path in the array is a valid Mx document. If valid, the LoadMxFile method is used to load the document into the MapControl; otherwise, a new IFileName object is created with its path property set to the file path in the array. IFileName is used to create a layer that can be added to the MapControl in the same way that the INames obtained by enumerating the IEnumName collection do.
Copy and paste the following axMapControl1_OnOleDrop method (for Steps 3-5) and the CreateLayer method (for Step 6) into your application where appropriate: [C#] private void axMapControl1_OnOleDrop(object sender,
ESRI.ArcGIS.Controls.IMapControlEvents2_OnOleDropEvent e) {
//QI for IDataObjectHelper. IDataObjectHelper dataObject = (IDataObjectHelper)e.dataObjectHelper; esriControlsDropAction action = e.dropAction; e.effect = (int)esriControlsDragDropEffect.esriDragDropNone; //If the mouse has just entered the MapControl. if (action == esriControlsDropAction.esriDropEnter) {
Dropping data onto the MapControl
Do the following steps to drop data onto the MapControl:
1.Set the IMapControl4.OleDropEnabled property to true.
2.Set the IMapControlEvents2.OnOleDrop event to axMapControl1_OnOleDrop.
//If the data is from Windows Explorer or ArcCatalog. if (dataObject.CanGetFiles() | dataObject.CanGetNames()) {
//Store the copy effect. m_Effect = esriControlsDragDropEffect.esriDragDropCopy; } } //If the mouse is moving over the MapControl. if (action == esriControlsDropAction.esriDropOver) { //Set the effect. e.effect = (int)m_Effect; } //If data is dropped onto the MapControl. if (action == esriControlsDropAction.esriDropped) { //If the data is from Windows Explorer. if (dataObject.CanGetFiles() == true) { //Get an array of file paths through the IDataObjectHelper. System.Array filePaths百度文库= System.Array.CreateInstance(typeof(string), 0,
IEnumLayer enumLayer = layerFactoryHelper.CreateLayersFromName(name); enumLayer.Reset(); //Get the ILayer interface. ILayer layer = enumLayer.Next(); //Loop through layers. while (layer != null) {
0); filePaths = (System.Array)dataObject.GetFiles(); //Loop through the array. for (int i = 0; i <= filePaths.Length - 1; i++) {
//If a valid Mx document, load it into the MapControl. if (axMapControl1.CheckMxFile(filePaths.GetValue(i).ToString()) ==
4.Manage the esriDropOver action of the onOLEDrop event:
When data is being dragged around the MapControl, the OnOleDrop event is triggered with an esriDropOver action. The DragDropEffect is set to the effect stored when the data initially entered the control.
//Create a layer or layers from a name and add to MapControl.
private void CreateLayer(IName name) {
//Set the mouse pointer. axMapControl1.MousePointer = esriControlsMousePointer.esriPointerHourglass; //Get the ILayerFactoryHelper interface. ILayerFactoryHelper layerFactoryHelper = new LayerFactoryHelperClass(); //Get the IEnumLayer interface through the ILayerFatcoryHelper interface. try {
3.Manage the esriDropEnter action of the onOLEDrop event:
When dragged, data initially enters the MapControl and the OnOleDrop event is triggered with an esriDropEnter action. The IDataObjectHelper interface is used to determine whether the data is being dragged from ArcCatalog or Windows Explorer using the CanGetNames and CanGetFiles methods. If the data is from ArcCatalog or Windows Explorer, the DragDropEffect is set to esriDragDropCopy; otherwise, it is set to esriDragDropNone. The DragDropEffect is then stored for later use, for example, when the same data is dragged around or dropped onto the MapControl.
If dropped from ArcCatalog, Mx document and layer (.lyr) files will return true for CanGetFiles as they are both a type of IFileName.
6.Call the CreateLayersFromName method to create a collection of ILayers by using the ILayerFactoryHelper interface. Each layer is then added to the MapControl using the AddLayer method.
true) {
try {
axMapControl1.LoadMxFile(filePaths.GetValue(i).ToString(), Type.Missing, "");
} catch (System.Exception ex) {
MessageBox.Show("Error:" + ex.Message); return ; } } else { //Get the IFileName interface and set its path. IFileName fileName = new FileNameClass(); fileName.Path = filePaths.GetValue(i).ToString(); //Create a map layer. CreateLayer((IName)fileName); } } } //If data is from ArcCatalog. else if (dataObject.CanGetNames() == true) { //Get the IEnumName interface through the IDataObjectHelper. IEnumName enumName = dataObject.GetNames(); enumName.Reset(); //Get the IName interface. IName name = enumName.Next(); //Loop through the names. while (name != null) { //Create a map layer. CreateLayer(name); name = enumName.Next(); } } } }