📄 form1.cs
字号:
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System;
using System.Windows.Forms;
// ArcGIS Engine引用
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.ToolbarControl;
using ESRI.ArcGIS.TOCControl;
namespace GIS二次开发_简单_
{
public partial class Form1 : Form
{
private ESRI.ArcGIS.Controls.ITOCControl2 m_tocControl;
private ESRI.ArcGIS.Controls.IMapControl3 m_mapControl;
private ESRI.ArcGIS.Controls.IToolbarMenu m_menuMap;
private ESRI.ArcGIS.Controls.IToolbarMenu m_menuLayer;
//几个常量
private const int WM_ENTERSIZEMOVE = 0x231;
private const int WM_EXITSIZEMOVE = 0x232;
private ESRI.ArcGIS.ToolbarControl.AxToolbarControl axToolbarControl2;
//存储路径
private string sFilePath;
public Form1()
{
InitializeComponent();
}
//窗体登陆
private void Form1_Load(object sender, EventArgs e)
{
//添加对TOCControl的菜单
LoadAddMenu();
//窗体缩放时禁止重绘
this.SetStyle(ControlStyles.EnableNotifyMessage, true);
}
private void axMapControl1_OnMapReplaced(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMapReplacedEvent e)
{
// 设置绑定控件
axTOCControl1.SetBuddyControl(axMapControl1);
axToolbarControl1.SetBuddyControl(axMapControl1);
}
/*
* 注:禁止缩放时重画方法是通过检查发送到窗体的Windows消息工作的。
* 当窗口开发缩放时,Windows发送WM_ENTERSIZEMOVE窗口消息。
* 此时,我们禁止在MapControl和PageLayoutControl上绘制图形,而是使用“stretchy bitmap”绘制。
* 当Windows发送WM_EXITSIZEMOVE消息时,窗体结束缩放,这时我们全部重绘新的范围。
*/
protected override void OnNotifyMessage(Message m)
{
base.OnNotifyMessage(m);
// 以下为手工添加的代码
if (m.Msg == WM_ENTERSIZEMOVE)
{
axMapControl1.SuppressResizeDrawing(true, 0);
}
else if (m.Msg == WM_EXITSIZEMOVE)
{
axMapControl1.SuppressResizeDrawing(false, 0);
}
}
//TOCControl默认允许用户自动地切换图层的可见性并改变显示在目录表中的名称。
//以下是增加代码防止用户在编辑名称时输入空的字符串。
private void axTOCControl1_OnEndLabelEdit(object sender, ESRI.ArcGIS.Controls.ITOCControlEvents_OnEndLabelEditEvent e)
{
// 禁止在编辑标签时键入空字串
string newLabel = e.newLabel;
if (newLabel.Trim() == "")
{
e.canEdit = false;
}
}
private void axMapControl1_OnMouseMove(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseMoveEvent e)
{
toolStripStatusLabel1.Text = "X:" + e.mapX + ",Y:" + e.mapY + "("+axMapControl1.MapUnits.ToString().Substring(4)+")";
}
private void axTOCControl1_OnMouseDown(object sender, ESRI.ArcGIS.Controls.ITOCControlEvents_OnMouseDownEvent e)
{
if (e.button != 2) return;
ESRI.ArcGIS.Controls.esriTOCControlItem item = ESRI.ArcGIS.Controls.esriTOCControlItem.esriTOCControlItemNone;
IBasicMap map = null; ILayer layer = null;
object other = null; object index = null;
//Determine what kind of item is selected
m_tocControl.HitTest(e.x, e.y, ref item, ref map, ref layer, ref other, ref index);
//Ensure the item gets selected
if (item == ESRI.ArcGIS.Controls.esriTOCControlItem.esriTOCControlItemMap)
m_tocControl.SelectItem(map, null);
else
m_tocControl.SelectItem(layer, null);
//Set the layer into the CustomProperty (this is used by the custom layer commands)
m_mapControl.CustomProperty = layer;
//Popup the correct context menu
if (item == ESRI.ArcGIS.Controls.esriTOCControlItem.esriTOCControlItemMap) m_menuMap.PopupMenu(e.x, e.y, m_tocControl.hWnd);
if (item == ESRI.ArcGIS.Controls.esriTOCControlItem.esriTOCControlItemLayer) m_menuLayer.PopupMenu(e.x, e.y, m_tocControl.hWnd);
}
private void LoadAddMenu()
{
m_tocControl = (ESRI.ArcGIS.Controls.ITOCControl2)axTOCControl1.Object;
m_mapControl = (ESRI.ArcGIS.Controls.IMapControl3)axMapControl1.Object;
//Set buddy control
m_tocControl.SetBuddyControl(m_mapControl);
axToolbarControl1.SetBuddyControl(m_mapControl);
//Add pre-defined control commands to the ToolbarControl
axToolbarControl1.AddItem("esriControls.ControlsOpenDocCommand", -1, 0, false, 0, esriCommandStyles.esriCommandStyleIconOnly);
//Add custom commands to the map menu
m_menuMap = new ESRI.ArcGIS.Controls.ToolbarMenuClass();
m_menuMap.AddItem(new LayerVisibility(), 1, 0, false, esriCommandStyles.esriCommandStyleTextOnly);
m_menuMap.AddItem(new LayerVisibility(), 2, 1, false, esriCommandStyles.esriCommandStyleTextOnly);
//Add pre-defined menu to the map menu as a sub menu
m_menuMap.AddSubMenu("esriControls.ControlsFeatureSelectionMenu", 2, true);
//Add custom commands to the map menu
m_menuLayer = new ESRI.ArcGIS.Controls.ToolbarMenuClass();
m_menuLayer.AddItem(new RemoveLayer(), -1, 0, false, esriCommandStyles.esriCommandStyleTextOnly);
m_menuLayer.AddItem(new ScaleThresholds(), 1, 1, true, esriCommandStyles.esriCommandStyleTextOnly);
m_menuLayer.AddItem(new ScaleThresholds(), 2, 2, false, esriCommandStyles.esriCommandStyleTextOnly);
m_menuLayer.AddItem(new ScaleThresholds(), 3, 3, false, esriCommandStyles.esriCommandStyleTextOnly);
m_menuLayer.AddItem(new LayerSelectable(), 1, 4, true, esriCommandStyles.esriCommandStyleTextOnly);
m_menuLayer.AddItem(new LayerSelectable(), 2, 5, false, esriCommandStyles.esriCommandStyleTextOnly);
m_menuLayer.AddItem(new ZoomToLayer(), -1, 6, true, esriCommandStyles.esriCommandStyleTextOnly);
//Set the hook of each menu
m_menuLayer.SetHook(m_mapControl);
m_menuMap.SetHook(m_mapControl);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -