GIS二次开发之放大
刚刚才开始学习GIS的二次开发,前面一个随笔中记录了几种实现平移的方法,今天就来看看地图放大的功能代码。
同样在实现放大的时候我们也来看看几种不同的方法(按钮的Event事件中触发,还有就是利用Com技术,编译为DLL,注册到机器中)。
第一种方法编译成DLL注册
代码
public MapZoomIn()
{
string[] res = GetType().Assembly.GetManifestResourceNames();
base.m_bitmap = new System.Drawing.Bitmap(GetType().Assembly.GetManifestResourceStream("Commands.zoomin.bmp"));
base.m_caption = "放大地图";
base.m_category = "ToolBar";
base.m_message = "放大地图";
base.m_name = "ToolBar_MapPan";
base.m_toolTip = "放大";
m_ZoomInStrart = true;
m_feedBack = null;
try
{
base.m_cursor = new System.Windows.Forms.Cursor(GetType().Assembly.GetManifestResourceStream("Commands.ZoomIn.cur"));
}
catch (Exception ex)
{
}
}
public override void OnCreate(object hook)
{
m_HookHelper.Hook = hook;
try
{
m_pActiveView = m_HookHelper.ActiveView as IActiveView;
}
catch { }
}
public override void OnMouseDown(int Button, int Shift, int X, int Y)
{
if (null == m_pActiveView) return;
m_pPoint = m_pActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);
m_ZoomInStrart = true;
}
public override void OnMouseMove(int Button, int Shift, int X, int Y)
{
if (null == m_pActiveView) return;
if (!m_ZoomInStrart) return;
if (Button == 1)//拖动
{
if (null == m_feedBack)
{
m_feedBack = new NewEnvelopeFeedbackClass();
m_feedBack.Display = m_pActiveView.ScreenDisplay;
m_feedBack.Start(m_pPoint);
}
}
m_feedBack.MoveTo(m_pActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y));
}
public override void OnMouseUp(int Button, int Shift, int X, int Y)
{
if (null == m_pActiveView) return;
if (!m_ZoomInStrart) return;
IEnvelope m_pEnvelope = new EnvelopeClass();
if (null == m_feedBack) //点缩放
{
m_pEnvelope = m_pActiveView.Extent;
m_pEnvelope.Expand(0.5, 0.5, true);
m_pActiveView.Extent.CenterAt(m_pPoint);
}
else //矩形缩放
{
try
{
m_pEnvelope = m_feedBack.Stop();
if (m_pEnvelope.Height == 0 || m_pEnvelope.Width == 0)
{
m_feedBack = null;
m_ZoomInStrart = false;
}
}
catch
{
m_pEnvelope = m_pActiveView.Extent;
m_pEnvelope.Expand(0.5, 0.5, true);
m_pActiveView.Extent.CenterAt(m_pPoint);
}
}
m_pActiveView.Extent = m_pEnvelope;
m_pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
m_feedBack = null;
Marshal.ReleaseComObject(m_feedBack);
m_ZoomInStrart = false;
}
public override void OnKeyDown(int keyCode, int Shift)
{
if (keyCode == 27)
{
m_ZoomInStrart = false;
base.m_cursor = System.Windows.Forms.Cursors.Default;
}
}
{
string[] res = GetType().Assembly.GetManifestResourceNames();
base.m_bitmap = new System.Drawing.Bitmap(GetType().Assembly.GetManifestResourceStream("Commands.zoomin.bmp"));
base.m_caption = "放大地图";
base.m_category = "ToolBar";
base.m_message = "放大地图";
base.m_name = "ToolBar_MapPan";
base.m_toolTip = "放大";
m_ZoomInStrart = true;
m_feedBack = null;
try
{
base.m_cursor = new System.Windows.Forms.Cursor(GetType().Assembly.GetManifestResourceStream("Commands.ZoomIn.cur"));
}
catch (Exception ex)
{
}
}
public override void OnCreate(object hook)
{
m_HookHelper.Hook = hook;
try
{
m_pActiveView = m_HookHelper.ActiveView as IActiveView;
}
catch { }
}
public override void OnMouseDown(int Button, int Shift, int X, int Y)
{
if (null == m_pActiveView) return;
m_pPoint = m_pActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);
m_ZoomInStrart = true;
}
public override void OnMouseMove(int Button, int Shift, int X, int Y)
{
if (null == m_pActiveView) return;
if (!m_ZoomInStrart) return;
if (Button == 1)//拖动
{
if (null == m_feedBack)
{
m_feedBack = new NewEnvelopeFeedbackClass();
m_feedBack.Display = m_pActiveView.ScreenDisplay;
m_feedBack.Start(m_pPoint);
}
}
m_feedBack.MoveTo(m_pActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y));
}
public override void OnMouseUp(int Button, int Shift, int X, int Y)
{
if (null == m_pActiveView) return;
if (!m_ZoomInStrart) return;
IEnvelope m_pEnvelope = new EnvelopeClass();
if (null == m_feedBack) //点缩放
{
m_pEnvelope = m_pActiveView.Extent;
m_pEnvelope.Expand(0.5, 0.5, true);
m_pActiveView.Extent.CenterAt(m_pPoint);
}
else //矩形缩放
{
try
{
m_pEnvelope = m_feedBack.Stop();
if (m_pEnvelope.Height == 0 || m_pEnvelope.Width == 0)
{
m_feedBack = null;
m_ZoomInStrart = false;
}
}
catch
{
m_pEnvelope = m_pActiveView.Extent;
m_pEnvelope.Expand(0.5, 0.5, true);
m_pActiveView.Extent.CenterAt(m_pPoint);
}
}
m_pActiveView.Extent = m_pEnvelope;
m_pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
m_feedBack = null;
Marshal.ReleaseComObject(m_feedBack);
m_ZoomInStrart = false;
}
public override void OnKeyDown(int keyCode, int Shift)
{
if (keyCode == 27)
{
m_ZoomInStrart = false;
base.m_cursor = System.Windows.Forms.Cursors.Default;
}
}
主要的代码在Mouse的UP,DOWN,MOVE事件中。在实现拉框放大的时候,利用了INewEnvelopeFeedback接口继承自IDisplayFeedback,帮助文档中有他的案例可以看看。
上面的方法的好处是可以集成ControlsMapZoomInTool和ControlsMapZoomInFixedCommand这两种功能。首先我们知道ControlsMapZoomInTool是实现拉框放大,ControlsMapZoomInFixedCommand则是实现点击放大。那么下面第二种方法就是讲述他们的实现代码
第二种方法直接触发按钮事件(在主界面中添加一个按钮,比如Button,注册其事件,在事件中写入以下代码 )
①:拉框放大
代码
/// <summary>
/// 放大
/// </summary>
private void btnZoomIn_Click(object sender, EventArgs e)
{
ControlsMapZoomInTool mapZoomIn = new ControlsMapZoomInTool();
mapZoomIn.OnCreate(axMapMain.Object);
axMapMain.CurrentTool = mapZoomIn as ITool;
}
/// 放大
/// </summary>
private void btnZoomIn_Click(object sender, EventArgs e)
{
ControlsMapZoomInTool mapZoomIn = new ControlsMapZoomInTool();
mapZoomIn.OnCreate(axMapMain.Object);
axMapMain.CurrentTool = mapZoomIn as ITool;
}
②:点击放大
代码
/// <summary>
/// 放大
/// </summary>
private void btnZoomIn_Click(object sender, EventArgs e)
{
ControlsMapZoomInFixedCommand mapZoomIn = new ControlsMapZoomInFixedCommand();
mapZoomIn.OnCreate(axMapMain.Object);
mapZoomIn.OnClick();
}
/// 放大
/// </summary>
private void btnZoomIn_Click(object sender, EventArgs e)
{
ControlsMapZoomInFixedCommand mapZoomIn = new ControlsMapZoomInFixedCommand();
mapZoomIn.OnCreate(axMapMain.Object);
mapZoomIn.OnClick();
}
记得在esri论坛中看过这样一句话:“Command对应的功能可以直接在按钮事件中书写,Tool对于的功能可以借助MouseMove、MouseDown、MouseUp事件书写”(大致是这个意思,找不到原帖了),可以从上一篇“GIS二次开发之平移”中利用ControlsMapPanTool就是借助Mouse事件完成平移的。(这里的放大没有借助Mouse事件也可以实现)。可以作为以后学习的指导吧。