《GS软件设计》实习报告
if (index != -1) {
// 取得鼠标所在工具的 ToolbarItem
IToolbarItem toolbarItem = axToolbarControl1.GetItem(index); // 设置状态栏信息
MessageLabel.Text = toolbarItem.Command.Message; } else {
MessageLabel.Text = \就绪 \ }
}
添加 axMapControl1 的 OnMouseMove 事件,其代码如下: private void axMapControl1_OnMouseMove(object sender, IMapControlEvents2_OnMouseMoveEvent e) {
// 显示当前比例尺
ScaleLabel.Text = \比例尺 1:\
}
显示当前坐标也是 axMapControl1 的 OnMouseMove 事件中响应,故只要在 axMapControl1_OnMouseMove 函数中添加如下代码即可: // 显示当前坐标
CoordinateLabel.Text = \当前坐标 X = \+ e.mapX.ToString() + \Y = \+ e.mapY.ToString() + \
按 F5 编译运行, c.鹰眼图
鹰眼和主地图的互动,主要体现在两个方面:一是主地图的地图改变了,则鹰眼里的矩形框要移动到对应的位置,以指示当前地图在整个地图中的位置;二是鹰眼的矩形框移动了,则主地图中显示的地图要移动到相应的位置。在主地图的OnAfterDrawMapControl事件中,根据当前地图的大小,改变鹰眼中矩形框的大小。 实现过程:
将鹰眼放在图层控件的下方,调整方法如下:
( 1 )在设计视图中,选择 tabControl1 控件,即放图层和属性的那个容器,将其 Dock 属性设为 None ,并用鼠标拖拽将其缩小。把工具箱中的 SplitContainer 控件拖到窗体的左窗格,即放在 tabControl1 控件的旁边。并将其 Orientation 属性设置为 Horizontal 。
( 2 )选中 tabControl1 控件,按 Ctrl+X 剪切,再选中刚才粘贴到 SplitContainer2 的 Panel1 中,如图 2 所示。操作完成后效果如图 3 所示。 ( 3 )再选中 SplitContainer2 控件(如果不好选中,直接以属性面板中选择 SplitContainer2 ),将其 Dock 属性设置为Fill 。再选中 tabControl1 ,将其 Dock 属性也设置为 Fill 。
25
《GS软件设计》实习报告
( 4 )从工具箱中选择 MapControl 控件并拖到 SplitContainer2 的 Panel2 ,作为鹰眼控件。最终效果如图 4 所示。 图 4
2 、鹰眼的实现
( 1 )载入地图到鹰眼控件
当地图载入到主 Map 控件时,同时也载入到鹰眼控件,在 axMapControl1_OnMapReplaced 事件响应函数中添加如下代码:
private void axMapControl1_OnMapReplaced(object sender, IMapControlEvents2_OnMapReplacedEvent e)
{
// 前面代码省略
this.axMapControl2.Map = new MapClass();
for (int i = 1; i <= this.axMapControl1.LayerCount; i++) {
this.axMapControl2.AddLayer(this.axMapControl1.get_Layer(this.axMapControl1.LayerCount - i)); }
this.axMapControl2.Extent = this.axMapControl1.FullExtent; this.axMapControl2.Refresh(); } ( 2
)绘制鹰眼矩形框
为鹰眼控件 MapControl1 添加 OnExtentUpdated事件,此事件是在主 Map
应更新鹰
眼控件中的矩形框。其响应函数代码如下:
private void axMapControl1_OnExtentUpdated(object sender, IMapControlEvents2_OnExtentUpdatedEvent e)
{
IEnvelope pEnv = (IEnvelope)e.newEnvelope;
IGraphicsContainer pGra = axMapControl2.Map as IGraphicsContainer; IActiveView pAv = pGra as IActiveView; pGra.DeleteAllElements();
IRectangleElement pRectangleEle = new RectangleElementClass(); IElement pEle = pRectangleEle as IElement; pEle.Geometry = pEnv;
IRgbColor pColor = new RgbColorClass(); pColor.Red = 255; pColor.Green = 0; pColor.Blue = 0;
pColor.Transparency = 255;
ILineSymbol pOutline = new SimpleLineSymbolClass(); pOutline.Width = 2;
pOutline.Color = pColor;
pColor = new RgbColorClass(); pColor.Red = 255; pColor.Green = 0; pColor.Blue = 0;
pColor.Transparency = 0;
IFillSymbol pFillSymbol = new SimpleFillSymbolClass();
26
控件的显示范围改变时响应,从而相
《GS软件设计》实习报告
pFillSymbol.Color = pColor; pFillSymbol.Outline = pOutline;
IFillShapeElement pFillShapeEle = pEle as IFillShapeElement; pFillShapeEle.Symbol = pFillSymbol;
pGra.AddElement((IElement)pFillShapeEle, 0);
pAv.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null); }
鹰眼与主 Map控件互动
为鹰眼控件 MapControl2添加 OnMouseDown 事件,代码如下:
private void axMapControl2_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e)
{
if (this.axMapControl2.Map.LayerCount != 0) {
if (e.button == 1) {
IPoint pPoint = new PointClass(); pPoint.PutCoords(e.mapX, e.mapY);
IEnvelope pEnvelope = this.axMapControl1.Extent; pEnvelope.CenterAt(pPoint);
this.axMapControl1.Extent = pEnvelope;
this.axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null); }
else if (e.button == 2) {
IEnvelope pEnvelop = this.axMapControl2.TrackRectangle(); this.axMapControl1.Extent = pEnvelop;
this.axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null); } } }
为鹰眼控件 MapControl2添加 OnMouseMove 事件,主要实现按下鼠标左键的时候移动矩形框,同时也改变主的图控件的 显示范围。代码如下:
private void axMapControl2_OnMouseMove(object sender, IMapControlEvents2_OnMouseMoveEvent e)
{
if (e.button != 1) return;
IPoint pPoint = new PointClass(); pPoint.PutCoords(e.mapX, e.mapY); this.axMapControl1.CenterAt(pPoint);
this.axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null); }
按下左键在鹰眼窗口中移动,或者按下右键在鹰眼窗口中画一个矩形,主地图窗口的显示范围都会跟着变化。主地图窗口中的地图经放大缩小等操作后,鹰眼窗口的矩形框大小也会随着改变。
2. 地图数据编辑模块
27
《GS软件设计》实习报告
该模块能够实现新建图层、添加图层、删除所有图层、开始编辑和结束编辑的操作。编辑可以对图层进行创建新特征、修改任务、移动特征和设置捕捉环境等操作。 a.添加图层
添加图层代码如下: private void LoadShapFileToolStripMenuItem_Click(object sender, EventArgs e) { AddShapefile(); } //添加Shapefile文件函数 public void AddShapefile() { ESRI.ArcGIS.Carto.IActiveView activeView = axMapControl1.ActiveView; ESRI.ArcGIS.Carto.IActiveView activeView1 = axMapControl2.ActiveView; if (activeView == null) { return; } if (activeView1 == null) { return; } System.Windows.Forms.OpenFileDialog openFileDialog = new System.Windows.Forms.OpenFileDialog(); openFileDialog.Filter = \; if (openFileDialog.ShowDialog() == DialogResult.OK) { string shapefileLocation = openFileDialog.FileName; string pathFile = shapefileLocation; if (shapefileLocation != \) { IWorkspaceFactory workspaceFactory = new ShapefileWorkspaceFactoryClass(); IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspaceFactory.OpenFromFile(System.IO.Path.GetDirectoryName(shapefileLocation), 0); IFeatureClass featureClass = featureWorkspace.OpenFeatureClass(System.IO.Path.GetFileNameWithoutExtension(shapefileLocation)); IFeatureLayer featureLayer = new FeatureLayerClass(); featureLayer.FeatureClass = featureClass; featureLayer.Name = featureClass.AliasName; featureLayer.Visible = true; //添加到主窗口中的代码
28
《GS软件设计》实习报告
activeView.FocusMap.AddLayer(featureLayer); activeView.Extent = activeView.FullExtent; activeView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null); //添加到鹰眼视图窗口中的代码 activeView1.FocusMap.AddLayer(featureLayer); activeView1.Extent = activeView1.FullExtent; activeView1.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null); } else { MessageBox.Show(\操作出错啦!\); } } } c. 删除图层
public sealed class RemoveLayer : BaseCommand
{
private IMapControl3 m_mapControl; public RemoveLayer() {
base.m_caption = \ }
public override void OnClick() {
ILayer layer = (ILayer)m_mapControl.CustomProperty; m_mapControl.Map.DeleteLayer(layer); }
public override void OnCreate(object hook) {
m_mapControl = (IMapControl3)hook; } } }
public sealed class RemoveLayer : BaseCommand
{
private IMapControl3 m_mapControl; public RemoveLayer() {
base.m_caption = \ }
public override void OnClick() {
ILayer layer = (ILayer)m_mapControl.CustomProperty; m_mapControl.Map.DeleteLayer(layer); }
public override void OnCreate(object hook) {
m_mapControl = (IMapControl3)hook; } }
29