AO總結10:MapControl控件


MapControl對應ArcMap中的數據視圖,它封裝了Map對象,並提供了額外的屬性、方法、事件用於:

1 管理控件的外觀、顯示屬性和地圖屬性

2 添加並管理控件中的數據層

3 裝載Map文檔控件中

4 從其它應用程序拖放數據到控件中

5 tracking shapes and drawing to the display

MapControl實現的主要接口有:IMapControlDefault   IMapControl2 IMapControl3和事件接口IMapControlEvents2

IMapControlDefault接口是地圖控件缺省接口,多數開發環境自動使用這個接口定義的屬性和方法。由於MapControl是一個自動化控件,當它被放到一個容器---如窗體上后,它會自動產生一個被稱為axMapControl1的對象,這個對象可以直接使用缺省接口定義的屬性和方法。MapControl當前最新版本接口為IMapControl3.

當需要使用這個接口時,可使用下面的代碼:

IMapControlDefault pMapControl;
pMapControl = axMapControl1.Object as IMapControlDefault;

對於文檔文件,MapControl控件可以直接使用LoadMxFile方法來載入,這是最簡單的方法。除此之外,也可以使用IMapDocument接口定義的屬性和方法來加載一個MSD文件。下面是一個載入文檔的例子:

private void LoadMapDocument()
{
System.Windows.Forms.OpenFileDialog openFileDialog2;
openFileDialog2 = new OpenFileDialog();
openFileDialog2.Title = "Open Map Document";
openFileDialog2.Filter = "Map Documents (*.mxd)|*.mxd";
openFileDialog2.ShowDialog();
string sFilePath = openFileDialog2.FileName;
if (axMapControl1.CheckMxFile(sFilePath))
{
axMapControl1.MousePointer =
esriControlsMousePointer.esriPointerHourglass;
axMapControl1.LoadMxFile(sFilePath, 0,Type.Missing);
axMapControl1.MousePointer =
esriControlsMousePointer.esriPointerDefault;
}
else
{
MessageBox.Show(sFilePath + " is not a valid ArcMap document");
return;
}
}

上例中,先使用.NET框架類庫提供的OpenFileDialog對話框找到要打開的MSD文檔sFilePath,然后利用AxMapControl提供的方法LoadMxFile()打開該MXD文檔,否則,顯示有關信息。

如果要裝載某個地圖文檔中的特定地圖,則可以使用以下方法:

private void LoadMapDocument2()
{
System.Windows.Forms.OpenFileDialog openFileDialog1;
openFileDialog1 = new OpenFileDialog();
openFileDialog1.Title = "Open Map Document";

openFileDialog1.Filter = "Map Documents (*.mxd)|*.mxd";
openFileDialog1.ShowDialog();
string sFilePath = openFileDialog1.FileName;
if (axMapControl1.CheckMxFile(sFilePath))
{
IArraypArray ;
pArray = axMapControl1.ReadMxMaps(sFilePath,Type.Missing);
int i ;
IMap pMap;
//遍歷這些Map對象
for(i = 0;i<=pArray.Count - 1;i++)
{
//QI
pMap = pArray.get_Element(i) as IMap;
if (pMap.Name == "Layers")
{
//載入文檔中特定的Map對象
axMapControl1.MousePointer =
esriControlsMousePointer.esriPointerHourglass;
axMapControl1.LoadMxFile(sFilePath, 0,Type.Missing);
axMapControl1.MousePointer =
esriControlsMousePointer.esriPointerDefault;
break;
}
}
}
else
{
MessageBox.Show(sFilePath + " is not a valid ArcMap document");
return;
}
}

下面的例子是打開、保存、另存為一個文檔文件,詳細代碼如下:

IMapDocument m_MapDocument;
private void LoadMapDoc()
{
m_MapDocument = new MapDocumentClass();
try
{
//打開文件對話框選取MXD文件
System.Windows.Forms.OpenFileDialog openFileDialog2;
openFileDialog2 = new OpenFileDialog();
openFileDialog2.Title = "Open Map Document";
openFileDialog2.Filter = "Map Documents (*.mxd)|*.mxd";
openFileDialog2.ShowDialog();
string sFilePath = openFileDialog2.FileName;
//將數據載入pMapDocument並與map控件聯系起來
m_MapDocument.Open(sFilePath, "");
int i;
for (i = 0 ; i<= pMapDocument.MapCount - 1;i++)
{
//一個IMapDocument對象中可能有多個Map對象,遍歷每個map對象
axMapControl1.Map = m_MapDocument.get_Map(i);
}
//刷新地圖
axMapControl1.Refresh();
}
catch( Exception ex)
{
MessageBox.Show(ex.ToString());
}
}


private void SaveDocument()
{

//Check that the document is not read only
if (m_MapDocument.get_IsReadOnly(m_MapDocument.DocumentFilename)
== true)
{
MessageBox.Show("This map document is read only!");
return;
}
//Save with the current relative path setting
m_MapDocument.Save(m_MapDocument.UsesRelativePaths,true);
MessageBox.Show("Changes saved successfully!");
}
private void SaveAsDocument ()
{
//Open a file dialog for saving map documents
saveFileDialog1.Title = "Save Map Document As";
saveFileDialog1.Filter = "Map Documents (*.mxd)|*.mxd";
saveFileDialog1.ShowDialog();
//Exit if no map document is selected
string sFilePath = saveFileDialog1.FileName;
if (sFilePath == "")
{
return;
}
if (sFilePath == m_MapDocument.DocumentFilename)
{
//Save changes to the current document
SaveDocument();
}
else
{
//SaveAs a new document with relative paths
m_MapDocument.SaveAs(sFilePath, true, true);
//Open document
OpenDocument((sFilePath));
MessageBox.Show("Document saved successfully!");

}
}
新建一個空的地圖文檔,並將其加載到 MapControl 的代碼如下:
private void mnuNew_Click(object sender, System.EventArgs e)
{
// IMap pMap = new MapClass();
// axMapControl1.Map = pMap ;
MapDocument pMapDocument = new MapDocumentClass();
string sFilePath = @"d:\temp\untitled.mxd";
pMapDocument.New(sFilePath);
pMapDocument.Open(sFilePath,"");
axMapControl1.Map = pMapDocument.get_Map(0);
}

此外,MapControl控件還可以使用AddLayerFromFile方法添加一個圖層文件,使用AddshapeFile添加一個Shape文件,使用AddLayer快速添加一個圖層,DeleteLayer快速刪除一個圖層,MoveLayerTo改變一個圖層的索引等。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM