標籤:style io 使用 ar 資料 sp 代碼 log on
新接觸TOCControl空間的自訂菜單,其功能強大令人歎為觀止。可用AddItem添加自訂Command、menu或者esriControls下的Command、menu;瞭解使用esriControls下的Command、menu,可以為開發人員省下大量的時間。舉例說明如下:自訂了一個添加圖層的命令(在其中特別注意CustomProperty這個屬性,它可以靈活附加傳遞多種資料)。
class AddLayer : BaseCommand
{
private IMapControl3 m_mapControl;
public AddLayer()
{
base.m_caption = "添加地圖文檔";
}
public override void OnClick()
{
ILayer layer = (ILayer)m_mapControl.CustomProperty;
m_mapControl.Map.DeleteLayer(layer);
OpenFileDialog openFileDialog1 = (OpenFileDialog)m_mapControl.CustomProperty;
openFileDialog1.Title = "開啟地圖文檔";
openFileDialog1.Filter = "map documents(*.mxd)|*.mxd";
openFileDialog1.ShowDialog();
string filePath = openFileDialog1.FileName;
if (m_mapControl.CheckMxFile(filePath))
{
m_mapControl.MousePointer = esriControlsMousePointer.esriPointerHourglass;
m_mapControl.LoadMxFile(filePath);
m_mapControl.MousePointer = esriControlsMousePointer.esriPointerDefault;
}
else
{
MessageBox.Show("地圖文檔無效!");
}
}
public override void OnCreate(object hook)
{
m_mapControl = (IMapControl3)hook;
}
}
toolbarMenu.AddItem(new AddLayer(), -1, 0, false, esriCommandStyles.esriCommandStyleTextOnly);
但其實足夠瞭解esriControls下的現成命令的話,上述代碼可以濃縮為:
toolbarMenu.AddItem("esriControls.ControlsOpenDocCommand", -1, 0, false,esriCommandStyles.esriCommandStyleIconOnly);
就這樣。
TOCControl右鍵菜單