看,別人是怎麼實現鷹眼的(C#版)!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Controls;
/*
* 鷹眼視圖的簡單實現,只是通過拖動鷹眼視圖的區域實現在主視圖中的顯示:
*/ 
namespace OverView
{
public partial class MainForm : Form
{
#region "界面初始化"


public MainForm()
{
InitializeComponent();
}


private void MainForm_Load(object sender, EventArgs e)
{
try
{
string strFileName = Application.StartupPath + @"\數據\房地產策劃信息系統.mxd";
//在數據視圖中加載地圖,供查詢操作
if (axMapControl2.CheckMxFile(strFileName))
{
axMapControl2.LoadMxFile(strFileName);
}
else
{
MessageBox.Show("錯誤的數據路徑:" + strFileName);
}
}
catch (Exception ex)
{
MessageBox.Show("Error01 in MainForm.cs" + ex.Message);
}
}


#endregion "界面初始化"


#region "鷹眼地圖"


/* 實現功能:
* 1、鷹眼控件會根據主控件的視圖範圍生成一個來導航
* 2、在鷹眼控件裏點擊左鍵移動,紅色距形框會跟着鼠標移動,主控件的視圖範圍會根據紅色距形框位置而做出相應的移動
* 3、在鷹眼控件中可以通過按住鼠標右鍵來拖動,生成一個新的紅色距形框,來導航 
* 實現步驟如下(通過多個例子綜合而成,write by booker,2008.4.12
*/
//1.鷹眼地圖資源載入
//當數據視圖文檔發生變化後,把文檔加載到鷹眼視圖的地圖控件axMapControl1中
private void axMapControl2_OnMapReplaced(object sender, IMapControlEvents2_OnMapReplacedEvent e)
{
//加載地圖文檔到MapControl中
axMapControl1.LoadMxFile(axMapControl2.DocumentFilename, null, null);
//設置MapControl顯示範圍至數據的全局範圍
axMapControl1.Extent = axMapControl1.FullExtent;
}


//2.繪製鷹眼矩形框
private void axMapControl2_OnExtentUpdated(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnExtentUpdatedEvent e)
{
// 得到新範圍
IEnvelope pEnv = (IEnvelope)e.newEnvelope;
IGraphicsContainer pGra = axMapControl1.Map as IGraphicsContainer;
IActiveView pAv = pGra as IActiveView;
//在繪製前,清除鷹眼axMapControl1中的任何圖形元素
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();
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);
}


//3. 實現互動
private void axMapControl1_OnMouseDown(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseDownEvent e)
{
/*
IPoint pPt = new PointClass();
pPt.PutCoords(e.mapX, e.mapY);
//改變主控件的視圖範圍
axMapControl2.CenterAt(pPt);
*/
if (e.button == 1)
{
IPoint pPt = new PointClass();
pPt.X = e.mapX;
pPt.Y = e.mapY;
IEnvelope pEnvelope = this.axMapControl1.TrackRectangle();
this.axMapControl2.Extent = pEnvelope;
}
}


private void axMapControl1_OnMouseMove(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseMoveEvent e)
{
if (e.button == 1)
{
IPoint pPt = new PointClass();
pPt.X = e.mapX;
pPt.Y = e.mapY;
//
IEnvelope pEnvelope = this.axMapControl2.Extent as IEnvelope;
pEnvelope.CenterAt(pPt);
this.axMapControl2.Extent = pEnvelope;
}
}


#endregion "鷹眼地圖"
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章