Provides access to members that control the reading and writing of map document files.(提供访问的成员,控制读写地图文档文件)
1:MapDocumentClass Class
The MapDocument coclass is used to read and write map document files.(地图文档组件类是用于读写地图文档文件。)实现了IMapDocument接口。
m_MapDocument = new MapDocumentClass();
2:Open Method
[C#]public void Open (string sDocument,string bsPassword);
[C++]HRESULT Open( BSTR sDocument, BSTR bsPassword);
The MapDocument will be cached so no other users will be able to access the MapDocument until it has been closed.(MapDocument将会被缓存,所以没有其他用户可以访问MapDocument,除非它已被关闭。)Before using the Open methods check whether the specified document
IsPresent, IsRestricted, IsMapDocument and IsPasswordProtected. If the MapDocument is password protected, specify the password in the Open method.
3:MapCount Property
The number of Map objects contained within the map document
// 使用IMapDocument打开文档
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并与MapControl联系起来
m_MapDocument.Open(sFilePath, "");
int i;
int sum = m_MapDocument.MapCount ;
for (i = 0; i <sum; i++)
{
// 一个IMapDocument对象中可能包含很多Map对象,遍历map对象
axMapControl1.Map = m_MapDocument.get_Map(i);
}
// 刷新地图控件
axMapControl1.Refresh();
}
catch ( Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
4:Map Property
[C#]public IMap get_Map (int mapIndex);
[C++]HRESULT get_Map(long mapIndex, IMap** ppMap);
5:IsReadOnly Property
[C#]public bool get_IsReadOnly (string sDocument);
[C++]HRESULT get_IsReadOnly( BSTR sDocument, VARIANT_BOOL* IsReadOnly);
Determines whether the specified file is read only. The
Save method cannot overwrite a read-only MapDocument, use the
SaveAs to write to a new document.(保存方法不能覆盖只读的地图文件,使用另存为)
6:Save Method
[C#]public void Save (bool bUseRelativePaths, bool bCreateThumnbail);
[C#]
Optional Values
bUseRelativePaths Supply true as a default value.(true代码默认值)
bCreateThumnbail Supply true as a default value.
[C++]HRESULT Save(VARIANT_BOOL bUseRelativePaths,VARIANT_BOOL bCreateThumnbail);
7:UsesRelativePaths Property
[C#]public
boolUsesRelativePaths {get;}
[C++]HRESULT get_UsesRelativePaths(VARIANT_BOOL* bUsesRelativePaths);文件是
Indicates if the data in the map document is referenced using relative paths.
(表明如果数据在地图文件是使用相对路径引用。)
// 使用IMapDocument保存文档
private void SaveDocument()
{
// 判断文档是否为只读
if (m_MapDocument.get_IsReadOnly(m_MapDocument.DocumentFilename))
{
MessageBox.Show( "This map document is read only!");
return;
}
//用当前的文件路径设置保存文件
m_MapDocument.Save(m_MapDocument.UsesRelativePaths, true);
MessageBox.Show( "Changes saved successfully!");
}
8:SaveAs Method
Save the contents of the map document to the specified file name.
[C#]public void SaveAs (string sDocument, bool bUseRelativePaths, boolbCreateThumnbail);
Optional Values
bUseRelativePaths Supply true as a default value.
bCreateThumnbail Supply true as a default value.
// 地图文档另存为
private void SaveAsDocument ()
{
//Open a file dialog for saving map documents
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
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)
{
// 当选择文件和当前文件相同时,保存即可
SaveDocument();
}
else
{
//SaveAs a new document with relative paths
m_MapDocument.SaveAs(sFilePath, true, true);
//Open document
MessageBox.Show( "Document saved successfully!");
}
}