在上一篇文章中,简单的实现了一些常用的功能,当时里面的IToolBarControl是通过设置Buddy属性和AxMapControl绑定的,接下来的几篇文章会简单的介绍一下自己如何实现这些功能。
今天简单的看看平移(Pan).简单的看了一下,目前我这边有三种方法可以实现Pan,当然这三种方法都是利用AE自带的一些方法或是属性实现。
AE本身在二次开发的时候为了方便开发人员,将各个功能独立起来,开发人员只需要简单的设置一些属性即可实现很强大的功能。同样继承这种思想,将接下来实现的Pan功能也注册到机器中,作为功能插件使用。
在建立项目的时候,建立一个“类库”,然后在引用中添加AE的一些常用的引用。
第一种:
在刚建立的类库中添加一个类,类名随便,这里是MapPan
代码
public MapPan()
{
string[] res = GetType().Assembly.GetManifestResourceNames();
base.m_bitmap = new System.Drawing.Bitmap(GetType().Assembly.GetManifestResourceStream("Commands.pan.bmp"));
base.m_caption = "平移地图";
base.m_category = "ToolBar";
base.m_message = "漫游地图";
base.m_name = "ToolBar_MapPan";
base.m_toolTip = "平移";
m_startPan = true;
try
{
base.m_cursor = new System.Windows.Forms.Cursor(GetType().Assembly.GetManifestResourceStream("Commands.Hand.cur"));
}
catch (Exception ex)
{
}
}
public override void OnCreate(object hook)
{
try
{
m_HookHelper.Hook = hook;
m_pActiveView = m_HookHelper.ActiveView as IActiveView;
}
catch { m_pActiveView = null; }
}
public override void OnMouseDown(int Button, int Shift, int X, int Y)
{
if (Button != 1) return;
if (!m_startPan) return;
try
{
IPoint pPt = m_pActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);
m_pActiveView.ScreenDisplay.PanStart(pPt);
m_PanOperation = true;
}
catch { m_PanOperation = false; }
}
public override void OnMouseUp(int Button, int Shift, int X, int Y)
{
if (Button != 1) return;
if (!m_PanOperation) return;
if (!m_startPan) return;
IEnvelope m_pEnvelope = m_pActiveView.ScreenDisplay.PanStop();
if (null != m_pEnvelope)
{
//m_pActiveView.Extent = m_pEnvelope;
m_pActiveView.ScreenDisplay.DisplayTransformation.VisibleBounds = m_pEnvelope;
m_pActiveView.ScreenDisplay.Invalidate(null, true, (short)esriScreenCache.esriAllScreenCaches);
}
m_PanOperation = false;
}
public override void OnMouseMove(int Button, int Shift, int X, int Y)
{
if (Button != 1) return;
if (!m_PanOperation) return;
if (!m_startPan) return;
try
{
IPoint pPt = m_pActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);
m_pActiveView.ScreenDisplay.PanMoveTo(pPt);
}
catch { }
}
//按ESC按键取消
public override void OnKeyDown(int keyCode, int Shift)
{
base.OnKeyDown(keyCode, Shift);
if (keyCode == 27)
{
m_startPan = 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.pan.bmp"));
base.m_caption = "平移地图";
base.m_category = "ToolBar";
base.m_message = "漫游地图";
base.m_name = "ToolBar_MapPan";
base.m_toolTip = "平移";
m_startPan = true;
try
{
base.m_cursor = new System.Windows.Forms.Cursor(GetType().Assembly.GetManifestResourceStream("Commands.Hand.cur"));
}
catch (Exception ex)
{
}
}
public override void OnCreate(object hook)
{
try
{
m_HookHelper.Hook = hook;
m_pActiveView = m_HookHelper.ActiveView as IActiveView;
}
catch { m_pActiveView = null; }
}
public override void OnMouseDown(int Button, int Shift, int X, int Y)
{
if (Button != 1) return;
if (!m_startPan) return;
try
{
IPoint pPt = m_pActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);
m_pActiveView.ScreenDisplay.PanStart(pPt);
m_PanOperation = true;
}
catch { m_PanOperation = false; }
}
public override void OnMouseUp(int Button, int Shift, int X, int Y)
{
if (Button != 1) return;
if (!m_PanOperation) return;
if (!m_startPan) return;
IEnvelope m_pEnvelope = m_pActiveView.ScreenDisplay.PanStop();
if (null != m_pEnvelope)
{
//m_pActiveView.Extent = m_pEnvelope;
m_pActiveView.ScreenDisplay.DisplayTransformation.VisibleBounds = m_pEnvelope;
m_pActiveView.ScreenDisplay.Invalidate(null, true, (short)esriScreenCache.esriAllScreenCaches);
}
m_PanOperation = false;
}
public override void OnMouseMove(int Button, int Shift, int X, int Y)
{
if (Button != 1) return;
if (!m_PanOperation) return;
if (!m_startPan) return;
try
{
IPoint pPt = m_pActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);
m_pActiveView.ScreenDisplay.PanMoveTo(pPt);
}
catch { }
}
//按ESC按键取消
public override void OnKeyDown(int keyCode, int Shift)
{
base.OnKeyDown(keyCode, Shift);
if (keyCode == 27)
{
m_startPan = false;
base.m_cursor = System.Windows.Forms.Cursors.Default;
}
}
是不是感觉很容易,主要借助PanStart、PanMoveTo、PanStop三个方法,先不要感叹了,因为接下来的两个方法更容易
第二种方法:
代码
public MapPan()
{
string[] res = GetType().Assembly.GetManifestResourceNames();
base.m_bitmap = new System.Drawing.Bitmap(GetType().Assembly.GetManifestResourceStream("Commands.pan.bmp"));
base.m_caption = "平移地图";
base.m_category = "ToolBar";
base.m_message = "漫游地图";
base.m_name = "ToolBar_MapPan";
base.m_toolTip = "平移";
m_startPan = true;
try
{
base.m_cursor = new System.Windows.Forms.Cursor(GetType().Assembly.GetManifestResourceStream("Commands.Hand.cur"));
}
catch (Exception ex)
{
}
}
public override void OnCreate(object hook)
{
try
{
m_HookHelper.Hook = hook;
m_pActiveView = m_HookHelper.ActiveView as IActiveView;
}
catch { m_pActiveView = null; }
}
//和方法1中不同的地方
public override void OnMouseMove(int Button, int Shift, int X, int Y)
{
m_pActiveView.ScreenDisplay.TrackPan();
}
public override void OnKeyDown(int keyCode, int Shift)
{
base.OnKeyDown(keyCode, Shift);
if (keyCode == 27)
{
m_startPan = 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.pan.bmp"));
base.m_caption = "平移地图";
base.m_category = "ToolBar";
base.m_message = "漫游地图";
base.m_name = "ToolBar_MapPan";
base.m_toolTip = "平移";
m_startPan = true;
try
{
base.m_cursor = new System.Windows.Forms.Cursor(GetType().Assembly.GetManifestResourceStream("Commands.Hand.cur"));
}
catch (Exception ex)
{
}
}
public override void OnCreate(object hook)
{
try
{
m_HookHelper.Hook = hook;
m_pActiveView = m_HookHelper.ActiveView as IActiveView;
}
catch { m_pActiveView = null; }
}
//和方法1中不同的地方
public override void OnMouseMove(int Button, int Shift, int X, int Y)
{
m_pActiveView.ScreenDisplay.TrackPan();
}
public override void OnKeyDown(int keyCode, int Shift)
{
base.OnKeyDown(keyCode, Shift);
if (keyCode == 27)
{
m_startPan = false;
base.m_cursor = System.Windows.Forms.Cursors.Default;
}
}
第二种方法中用TrackPan方法就完全省去了之前方法中的MouseDown和MouseUp。接下来是看第三种方法,更让人受不了。
前两种方法是可以编译成DLL,注册到机器中去的,在调用的时候,可以如下面的代码
代码
#region 定义工具栏
private void DefineToolBarControl()
{
string progID = "CSharpDotNETCommands.MapPan";//命名空间.类名
axToolbarControl1.AddItem(progID, -1, -1, false, 0, esriCommandStyles.esriCommandStyleIconAndText);
}
#endregion
private void DefineToolBarControl()
{
string progID = "CSharpDotNETCommands.MapPan";//命名空间.类名
axToolbarControl1.AddItem(progID, -1, -1, false, 0, esriCommandStyles.esriCommandStyleIconAndText);
}
#endregion
最后一种方法是可以直接编写在主窗体程序中的
第三种方法:
代码
bool m_startPan = false;
//BUTTON按钮事件
private void btnMapPan_Click(object sender, EventArgs e)
{
m_startPan = true;
}
private void axMapMain_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e)
{
if (e.button == 1)
{
if (m_startPan)
{
axMapMain.Pan();
}
}
}
private void btnMapPan_Click(object sender, EventArgs e)
{
m_startPan = true;
}
private void axMapMain_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e)
{
if (e.button == 1)
{
if (m_startPan)
{
axMapMain.Pan();
}
}
}
是不是最后一种方法最简单。哈哈
当然这里的方法只是个人知道的方法,肯定还有很多方法,比如ControlsMapPanTool,这个方法没有试过,既然网上有这样的代码,也肯定是行的痛的,可以多尝试。