![]()
1 using ESRI.ArcGIS.Carto;
2 using ESRI.ArcGIS.Controls;
3 using ESRI.ArcGIS.Display;
4 using ESRI.ArcGIS.esriSystem;
5 using ESRI.ArcGIS.Geometry;
6 using System;
7 using System.Collections.Generic;
8 using System.ComponentModel;
9 using System.Data;
10 using System.Drawing;
11 using System.Linq;
12 using System.Text;
13 using System.Threading.Tasks;
14 using System.Windows.Forms;
15
16 namespace Ch02Ex06
17 {
18 public partial class Form1 : Form
19 {
20 public Form1()
21 {
22 InitializeComponent();
23 }
24 private void Form1_Click(object sender, EventArgs e)
25 {
26 LoadMap();
27 }
28 private void LoadMap()
29 {
30 OpenFileDialog of = new OpenFileDialog();
31 of.Title = "打开地图";
32 of.Filter = "打开(*.mxd)|*.mxd";
33 of.ShowDialog();
34 string path = of.FileName;
35 if (axMapControl1.CheckMxFile(path))
36 {
37 axMapControl1.MousePointer = esriControlsMousePointer.esriPointerHourglass;
38 axMapControl1.LoadMxFile(path, 0, Type.Missing);
39 axMapControl1.MousePointer = esriControlsMousePointer.esriPointerDefault;
40 LoadEyeMap(path);
41 }
42 else
43 {
44 MessageBox.Show("不是有效地地图","错误");
45 }
46 }
47 private void LoadEyeMap(string filePath)
48 {
49 if (axMapControl1.CheckMxFile(filePath))
50 {
51 axMapControl2.MousePointer =ESRI.ArcGIS.Controls.esriControlsMousePointer.esriPointerHourglass;
52 axMapControl2.LoadMxFile(filePath, 0, Type.Missing);
53 axMapControl2.MousePointer = esriControlsMousePointer.esriPointerDefault;
54 }
55 else
56 {
57 MessageBox.Show("不是有效地地图", "错误");
58 }
59 }
60
61 private void axMapControl1_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e)
62 {
63 if (e.button == 1)
64 {
65 axMapControl1.Extent = axMapControl1.TrackRectangle();
66 axMapControl1.Refresh(esriViewDrawPhase.esriViewBackground, null, null);
67 }
68 else if(e.button==2)
69 {
70 axMapControl1.Pan();
71 axMapControl1.Refresh(esriViewDrawPhase.esriViewBackground,null,null);
72 }
73 }
74
75 private void axMapControl1_OnExtentUpdated(object sender, IMapControlEvents2_OnExtentUpdatedEvent e)
76 {
77 IEnvelope envelope = e.newEnvelope as IEnvelope;
78 IGraphicsContainer graphicsContainer = axMapControl2.Map as IGraphicsContainer;
79 IActiveView activeView = graphicsContainer as IActiveView;
80 graphicsContainer.DeleteAllElements();
81 IElement element = new RectangleElement();
82 element.Geometry = envelope;
83
84 ILineSymbol outLineSymbol = new SimpleLineSymbol();
85 outLineSymbol.Width = 2;
86 outLineSymbol.Color = GetColor(255, 0, 0, 255);
87
88 IFillSymbol fillSymbol = new SimpleFillSymbol();
89 fillSymbol.Color = GetColor(9, 0, 0, 0);
90 fillSymbol.Outline = outLineSymbol;
91
92 IFillShapeElement fillShapeElement = element as IFillShapeElement;
93 fillShapeElement.Symbol = fillSymbol;
94
95 graphicsContainer.AddElement(fillShapeElement as IElement, 0);
96 activeView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null,null);
97 }
98 private IRgbColor GetColor(int r, int g, int b, int t)
99 {
100 IRgbColor rgbColor = new RgbColor();
101 rgbColor.Red = r;
102 rgbColor.Green = g;
103 rgbColor.Blue = b;
104 rgbColor.Transparency = (byte)t;
105 return rgbColor;
106 }
107
108 private void axMapControl2_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e)
109 {
110 IPoint point = new ESRI.ArcGIS.Geometry.Point();
111 point.X = e.mapX;
112 point.Y = e.mapY;
113 axMapControl1.CenterAt(point);
114 }
115
116
117 }
118 }