地圖和圖層
窗體界面:
要求:
1、加載MapDocument,並獲取其包含的Map。
2、獲取當前選中Map所包含的Layer。
3、顯示選中Layer的屬性信息。
1.加載MapDocument,並獲取其包含的Map
public partial class Form1 : Form
{
IMapDocument pMapDocument; //全局變量 pMapDocument
public Form1()
{
InitializeComponent();
}
//加載MapDocument,並獲取其包含的Map
private void LoadMxd_Click(object sender,EventArgs e)
{
comboBox1.Items.Clear();
OpenFileDialog pOpenFileDialog = new OpenFileDialog();
pOpenFileDialog.Title = "打開地圖文檔";
pOpenFileDialog.Filter = "地圖文檔(*.mxd)|*.mxd";
pOpenFileDialog.RestoreDirectory = true;
if (pOpenFileDialog.ShowDialog() == DialogResult.OK) //檢查地圖文檔有效性
{
string MxdFilePath = pOpenFileDialog.FileName; //Mxd地圖文檔地址
if (axMapControl1.CheckMxFile(MxdFilePath)) //判斷地址是否有效
{
axMapControl1.LoadMxFile(MxdFilePath,0,Type.Missing);
pMapDocument = new MapDocumentClass();
for (int i = 0; i < pMapDocument.MapCount; i++)
{
ComboBox1.Items.Add(axMapControl1.Map.Name);
}
axMapControl1.ActiveView.Refresh();
axMapControl1.Extent = axMapControl1.FullExtent;
}
else
{
MessageBox.Show("MxdFilePath" + "是無效的地圖文檔!", "信息提示");
return;
}
}
}
2.獲取當前選中Map所包含的Layer
private void comboBox1_SelectedIndexChanged(object sender,EventArgs e)
{
listBox1.Items.Clear();
int MapIndex = ComboBox1.SelectedIndex;
axMapControl1.Map = pMapDocument.get_Map(MapIndex);
axMapControl1.ActiveView.Refresh();
axMapControl1.Extent = axMapControl1.FullExtent;
IMap pMap = axMapControl1.Map;
for (int LayerIndex = 0; LayerIndex < pMap.LayerCount; LayerIndex++)
{
ILayer pLayer = pMap.get_Layer(LayerIndex);
ListBox1.Items.Add(pLayer.Name);
}
}
3.顯示選中Layer的屬性信息
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
int i = listBox1.SelectedIndex;
IMap pMap = axMapControl1.Map;
ILayer pLayer = pMap.get_Layer(i);
if (i < 0) return; //防止異常報錯,如果i爲-1,即沒選擇listBox1中的選項(一般此類空值,程序默認爲-1)
bool ShowTips = pLayer.ShowTips;
textBox1.Text = pLayer.MinimumScale.ToString();
textBox2.Text = pLayer.MaximumScale.ToString();
if (ShowTips.ToString() == "true")
textBox3.Text = "Yes";
else
textBox3.Text = "No";
}
4.清除按鈕
private void Clear_Click(object sender, EventArgs e)
{
axMapControl1.ClearLayers();
axMapControl1.Refresh();
comboBox1.Items.Clear();
listBox1.Items.Clear();
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
}
參考文章:
https://blog.csdn.net/Domen_Dragon/article/details/86497799
https://blog.csdn.net/m0_37768631/article/details/84346403