📄 form1.cs
字号:
cmdSave.Enabled = false;
cmdSaveAs.Enabled = false;
//Create UID's and add new items to the ToolBarControl
UID uID = new UIDClass();
uID.Value = "esriControlCommands.ControlsOpenDocCommand";
axToolbarControl1.AddItem(uID, 0, -1, false, -1, esriCommandStyles.esriCommandStyleIconOnly);
uID.Value = "esriControlCommands.ControlsSaveAsDocCommand";
axToolbarControl1.AddItem(uID, -1, -1, false, -1, esriCommandStyles.esriCommandStyleIconOnly);
uID.Value = "esriControlCommands.ControlsMapZoomInTool";
axToolbarControl1.AddItem(uID, -1 , -1, false, -1, esriCommandStyles.esriCommandStyleIconOnly);
uID.Value = "esriControlCommands.ControlsMapZoomOutTool";
axToolbarControl1.AddItem(uID, -1, -1, false, -1, esriCommandStyles.esriCommandStyleIconOnly);
uID.Value = "esriControlCommands.ControlsMapZoomInFixedCommand";
axToolbarControl1.AddItem(uID, -1, -1, false, -1, esriCommandStyles.esriCommandStyleIconOnly);
uID.Value = "esriControlCommands.ControlsMapZoomOutFixedCommand";
axToolbarControl1.AddItem(uID, -1, -1, false, -1, esriCommandStyles.esriCommandStyleIconOnly);
uID.Value = "esriControlCommands.ControlsMapPanTool";
axToolbarControl1.AddItem(uID, -1, -1, false, -1, esriCommandStyles.esriCommandStyleIconOnly);
uID.Value = "esriControlCommands.ControlsMapFullExtentCommand";
axToolbarControl1.AddItem(uID, -1, -1, false, -1, esriCommandStyles.esriCommandStyleIconOnly);
uID.Value = "esriControlCommands.ControlsMapZoomToLastExtentBackCommand";
axToolbarControl1.AddItem(uID, -1, -1, true, -1, esriCommandStyles.esriCommandStyleIconOnly);
uID.Value = "esriControlCommands.ControlsMapZoomToLastExtentForwardCommand";
axToolbarControl1.AddItem(uID, -1, -1, false, -1, esriCommandStyles.esriCommandStyleIconOnly);
uID.Value = "esriControlCommands.ControlsPageZoom100PercentCommand";
axToolbarControl1.AddItem(uID,-1,-1,false,-1,esriCommandStyles.esriCommandStyleIconOnly);
//共享ToolbarControl的命令池,使点击右键弹出的菜单命令可用
m_ToolbarMenu.CommandPool = axToolbarControl1.CommandPool;
//向ToolbarMenu(弹出式菜单)增加命令
uID.Value = "esriControlCommands.ControlsOpenDocCommand";
m_ToolbarMenu.AddItem(uID, -1, -1, false,esriCommandStyles.esriCommandStyleIconAndText);
uID.Value = "esriControlCommands.ControlsSaveAsDocCommand";
m_ToolbarMenu.AddItem(uID, -1, -1, false,esriCommandStyles.esriCommandStyleIconAndText);
uID.Value = "esriControlCommands.ControlsMapZoomInFixedCommand";
m_ToolbarMenu.AddItem(uID, -1, -1, false,esriCommandStyles.esriCommandStyleIconAndText);
uID.Value = "esriControlCommands.ControlsMapZoomOutFixedCommand";
m_ToolbarMenu.AddItem(uID, -1, -1, false,esriCommandStyles.esriCommandStyleIconAndText);
//string progID = "esriControlToolsMap.ControlsMapZoomInFixedCommand";
//m_ToolbarMenu.AddItem(progID,-1,-1,false,esriCommandStyles.esriCommandStyleIconAndText);
//progID = "esriControlToolsMap.ControlsMapZoomOutFixedCommand";
//m_ToolbarMenu.AddItem(progID,-1,-1,false,esriCommandStyles.esriCommandStyleIconAndText);
uID.Value = "esriControlCommands.ControlsMapFullExtentCommand";
m_ToolbarMenu.AddItem(uID, -1, -1, false,esriCommandStyles.esriCommandStyleIconAndText);
uID.Value = "esriControlCommands.ControlsMapZoomToLastExtentBackCommand";
m_ToolbarMenu.AddItem(uID, -1, -1, true,esriCommandStyles.esriCommandStyleIconAndText);
uID.Value = "esriControlCommands.ControlsMapZoomToLastExtentForwardCommand";
m_ToolbarMenu.AddItem(uID, -1, -1, true,esriCommandStyles.esriCommandStyleIconAndText);
//设置与MapControl挂接
//m_ToolbarMenu.SetHook(axMapControl1);
}
private void cmdMapDoc_Click(object sender, System.EventArgs e)
{
openFileDialog1.Title = "Open Map Document";
openFileDialog1.Filter = "Map Documents (*.mxd,*.mxt,*.pmf,*.shp)|*.mxd;*.mxt;*.pmf;*.shp";
openFileDialog1.ShowDialog();
// Exit if no map document is selected
string sFilePath = openFileDialog1.FileName;
if (sFilePath == "")
{
return;
}
//Open document
OpenDocument((sFilePath));
if (cmdSave.Enabled == false)
{
cmdSave.Enabled = true;
}
if (cmdSaveAs.Enabled == false)
{
cmdSaveAs.Enabled = true;
}
/* string sFileName = openFileDialog1.FileName;
//Validate the and load the map document
if (axMapControl1.CheckMxFile(sFileName))
{
axMapControl1.LoadMxFile(sFileName,Type.Missing,Type.Missing);
}*/
}
private void axMapControl1_OnMouseDown(object sender, ESRI.ArcGIS.MapControl.IMapControlEvents2_OnMouseDownEvent e)
{
//弹出ToolbarMenu
if(e.button == 2) //点击右键弹出菜单
{
m_ToolbarMenu.PopupMenu(e.x,e.y,axMapControl1.hWnd);
}
}
private void saveButton_Click(object sender, System.EventArgs e)
{
//Save changes to the current document
SaveDocument();
}
private void cmdSaveAs_Click(object sender, System.EventArgs e)
{
//Open a file dialog for saving map documents
saveFileDialog1.Title = "把图形文档另存为";
//saveFileDialog1.Filter = "Map Documents (*.mxd)|*.mxd";
saveFileDialog1.Filter = "Map Documents (*.mxd,*.mxt,*.pmf)|*.mxd;*.mxt;*.pmf";
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));
System.Windows.Forms.MessageBox.Show("保存/替换成功!");
}
}
private void OpenDocument(string sFilePath)
{
//Create a new map document
m_MapDocument = new MapDocumentClass();
//Open the map document selected
m_MapDocument.Open(sFilePath,"");
//Set the PageLayoutControl page layout to the map document page layout
axMapControl1.Map = m_MapDocument.get_Map(0);
txtMapDocument.Text = m_MapDocument.DocumentFilename;
}
private void SaveDocument()
{
//Check that the document is not read only
if (m_MapDocument.get_IsReadOnly(m_MapDocument.DocumentFilename) == true)
{
System.Windows.Forms.MessageBox.Show("This map document is read only!");
return;
}
//Save with the current relative path setting
m_MapDocument.Save(m_MapDocument.UsesRelativePaths,true);
System.Windows.Forms.MessageBox.Show("保存成功!");
}
//在TOCControl标签编辑后的事件
private void axTOCControl1_OnEndLabelEdit(object sender, ESRI.ArcGIS.TOCControl.ITOCControlEvents_OnEndLabelEditEvent e)
{
//禁止在编辑标签时输入空字符窜
string newLable = e.newLabel;
if(e.newLabel.Trim() == "")
{
e.canEdit = false ;
}
}
private void CreateCustomizeDialog()
{
//创建自定义对话框事件
startDialogE = new ICustomizeDialogEvents_OnStartDialogEventHandler(OnStartDialog);
((ICustomizeDialogEvents_Event)m_CustomizeDialog).OnStartDialog += startDialogE;
closeDialogE = new ICustomizeDialogEvents_OnCloseDialogEventHandler(OnCloseDialog);
((ICustomizeDialogEvents_Event)m_CustomizeDialog).OnCloseDialog += closeDialogE;
//显示标题
m_CustomizeDialog.DialogTitle = "自定义ToolbarControl项目";
//显示从文件添加按钮
m_CustomizeDialog.ShowAddFromFile = true;
//设置将增加新项目的ToolbarControl
m_CustomizeDialog.SetDoubleClickDestination(axToolbarControl1);
}
private void chkCustomization_CheckedChanged(object sender, System.EventArgs e)
{
//Set whether control can be customized at run time
if (chkCustomization.CheckState == CheckState.Checked)
{
m_CustomizeDialog.StartDialog(axToolbarControl1.hWnd);
axToolbarControl1.Customize = true;
}
else
{
m_CustomizeDialog.CloseDialog();
axToolbarControl1.Customize = false;
}
}
private void OnStartDialog()
{
axToolbarControl1.Customize = true;
}
private void OnCloseDialog()
{
axToolbarControl1.Customize = false;
chkCustomization.Checked = false;
}
private void axMapControl1_OnMapReplaced(object sender, ESRI.ArcGIS.MapControl.IMapControlEvents2_OnMapReplacedEvent e)
{
esriUnits mapUnits = axMapControl1.MapUnits;
switch (mapUnits)
{
case esriUnits.esriCentimeters:
sMapUnits = "Centimeters";
break;
case esriUnits.esriDecimalDegrees:
sMapUnits = "Decimal Degrees";
break;
case esriUnits.esriDecimeters:
sMapUnits = "Decimeters";
break;
case esriUnits.esriFeet:
sMapUnits = "Feet";
break;
case esriUnits.esriInches:
sMapUnits = "Inches";
break;
case esriUnits.esriKilometers:
sMapUnits = "Kilometers";
break;
case esriUnits.esriMeters:
sMapUnits = "Meters";
break;
case esriUnits.esriMiles:
sMapUnits = "Miles";
break;
case esriUnits.esriMillimeters:
sMapUnits = "Millimeters";
break;
case esriUnits.esriNauticalMiles:
sMapUnits = "NauticalMiles";
break;
case esriUnits.esriPoints:
sMapUnits = "Points";
break;
case esriUnits.esriUnknownUnits:
sMapUnits = "Unknown";
break;
case esriUnits.esriYards:
sMapUnits = "Yards";
break;
}
}
private void axToolbarControl1_OnMouseMove(object sender, ESRI.ArcGIS.ToolbarControl.IToolbarControlEvents_OnMouseMoveEvent e)
{
//Determine if the mouse is over the item
int index = axToolbarControl1.HitTest(e.x,e.y,false);
if(index != -1)
{
//Get a reference to the ToolbarItem interface
IToolbarItem toolbarItem = axToolbarControl1.GetItem(index);
//set statusbar text
statusBar1.Panels[0].Text = toolbarItem.Command.Message;
}
else
{
statusBar1.Panels[0].Text = "No tool selected!";
}
}
private void axMapControl1_OnMouseMove(object sender, ESRI.ArcGIS.MapControl.IMapControlEvents2_OnMouseMoveEvent e)
{
statusBar1.Panels[1].Text = "X:"+e.mapX.ToString(".00") + " " + "Y:"+e.mapY.ToString(".00")+ " " + sMapUnits;
}
private void clearall_Click(object sender, System.EventArgs e)
{
axMapControl1.ClearLayers();
axTOCControl1.SetBuddyControl(axMapControl1);
txtMapDocument.Text = "";
}
private void selectfeature_Click(object sender, System.EventArgs e)
{
IMxDocument pMxDoc = new MapDocumentClass();
pMxDoc = application.Document;
IMap pMap = new MapClass();
pMap = pMxDoc.FocusMap;
IActiveView pActiveView;
IFeatureSelection pFeatureSelection;
IFeatureLayer pFeatureLayer = new FeatureLayerClass();
IQueryFilter pQueryFilter = new QueryFilterClass();
pActiveView = (IActiveView)pMap;
pFeatureLayer = (IFeatureLayer)pMap.LayerCount;
pFeatureSelection = (IFeatureSelection)pFeatureLayer;
pQueryFilter.WhereClause = "NAME = 'Green River'";
pActiveView.PartialRefresh(esriViewGeoSelection,null,null);
pFeatureSelection.SelectFeatures(pQueryFilter,esriSelectionResultNew,false);
pActiveView.PartialRefresh(esriGeoSelection,null,null);
}
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
//Release COM objects and shutdown
ESRI.ArcGIS.Utility.COMSupport.AOUninitialize.Shutdown();
m_AoInitialize.Shutdown();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -