mo+c#中鹰眼的实现
//类
using System;
namespace WindowsApplication68
{
/// <summary>
/// DragFeedback 的摘要说明。
/// </summary>
public class DragFeedback
{
public DragFeedback()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
private static extern bool Rectangle(IntPtr hdc,int ulCornerX, int ulCornerY,int lrCornerX, int lrCornerY);
[System.Runtime.InteropServices.DllImport("user32.dll") ]
private static extern IntPtr GetDC(IntPtr hwnd);
[System.Runtime.InteropServices.DllImport("gdi32.dll") ]
private static extern int SetROP2(IntPtr hdc,int fnDrawMode);
public MapObjects2.Rectangle dragfinish(float x,float y)
{
Rectangle (m_hdc,m_xmin,m_ymin,m_xmax,m_ymax);
r = new MapObjects2.RectangleClass();
// return the rectangle
p = new MapObjects2.PointClass();
p =m_map.ToMapPoint(m_xmin,m_ymin);
r.Left =p.X ;
r.Top =p.Y;
p =m_map.ToMapPoint(m_xmax,m_ymax);
r.Right =p.X ;
r.Bottom =p.Y ;
return r;
}
public void dragmove(float x,float y)
{
float xnext;
float ynext;
xnext=x;
ynext=y;
Rectangle(m_hdc,m_xmin,m_ymin,m_xmax,m_ymax);
m_xmin =m_xmin + Convert.ToInt32 (xnext - m_xprev);
m_xmax =m_xmax + Convert.ToInt32 (xnext - m_xprev);
m_ymin =m_ymin + Convert.ToInt32 (ynext - m_yprev);
m_ymax =m_ymax + Convert.ToInt32 (ynext - m_yprev);
Rectangle(m_hdc,m_xmin,m_ymin,m_xmax,m_ymax);
m_xprev =Convert.ToInt32(xnext);
m_yprev =Convert.ToInt32(ynext);
}
public void dragstart(MapObjects2.IMoRectangle rect,AxMapObjects2.AxMap map,float x,float y)
{
float xmin,ymin;
float xmax,ymax;
m_map =map;
// initialize the hwnd and hdc variables
m_hwnd =m_map.Handle ;
m_hdc =GetDC(m_map.Handle );
SetROP2(m_hdc,10); // raster op for inverting
p =new MapObjects2.PointClass();
p.X =rect.Left;
p.Y =rect.Top;
xmin=0;
ymin=0;
xmax=0;
ymax=0;
m_map.FromMapPoint(p,ref xmin,ref ymin);
p.X =rect.Right;
p.Y =rect.Bottom;
m_map.FromMapPoint(p,ref xmax,ref ymax);
m_xmin =Convert.ToInt32 (xmin);
m_ymin =Convert.ToInt32 (ymin);
m_xmax =Convert.ToInt32 (xmax);
m_ymax =Convert.ToInt32 (ymax);
// draw the rectangle
Rectangle(m_hdc,m_xmin,m_ymin,m_xmax,m_ymax);
// remember the click position
m_xprev =Convert.ToInt32 (x);
m_yprev =Convert.ToInt32(y);
}
protected MapObjects2.Point p ;
protected AxMapObjects2.AxMap m_map;
protected System.IntPtr m_hdc;
protected System.IntPtr m_hwnd;
protected int m_xmin;
protected int m_ymin;
protected int m_xmax;
protected int m_ymax;
protected int m_xprev;
protected int m_yprev;
public MapObjects2.Rectangle r ;
}
}
//程序主体
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace WindowsApplication68
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private AxMapObjects2.AxMap axMap1;
private AxMapObjects2.AxMap axMap2;
private System.Windows.Forms.Button button1;
public DragFeedback DragFeedbk ;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
Windows 窗体设计器生成的代码
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
MapObjects2.IMoDataConnection dc;
MapObjects2.IMoMapLayer layer;
dc=new MapObjects2.DataConnectionClass();
dc.Database=@"f:\data\usa";
if (dc.Connect() )
{
layer=new MapObjects2.MapLayerClass();
layer.GeoDataset=dc.FindGeoDataset("STATES");
layer.Symbol.Color=Convert.ToUInt32(MapObjects2.ColorConstants.moPaleYellow) ;
axMap1.Layers.Add(layer);
layer=new MapObjects2.MapLayerClass();
layer.GeoDataset=dc.FindGeoDataset("ushigh");
layer.Symbol.Color=Convert.ToUInt32(MapObjects2.ColorConstants.moDarkGreen) ;
axMap1.Layers.Add(layer);
layer=new MapObjects2.MapLayerClass();
layer.GeoDataset=dc.FindGeoDataset("uslakes");
layer.Symbol.Color=Convert.ToUInt32(MapObjects2.ColorConstants.moBlue) ;
axMap1.Layers.Add(layer);
layer=new MapObjects2.MapLayerClass();
layer.GeoDataset=dc.FindGeoDataset("STATES");
layer.Symbol.Color=Convert.ToUInt32(MapObjects2.ColorConstants.moPaleYellow) ;
axMap2.Layers.Add(layer);
DragFeedbk =new DragFeedback();
}
}
private void button1_Click(object sender, System.EventArgs e)
{
axMap1.Extent=axMap1.FullExtent;
}
private void axMap1_MouseDownEvent(object sender, AxMapObjects2._DMapEvents_MouseDownEvent e)
{
MapObjects2.Rectangle r;
r=new MapObjects2.RectangleClass();
if (e.button==1)
{
r=axMap1.TrackRectangle();
axMap1.Extent=r;
}
else
{ this.Cursor=Cursors.Hand ;
axMap1.Pan();
}
}
private void axMap1_AfterLayerDraw(object sender, AxMapObjects2._DMapEvents_AfterLayerDrawEvent e)
{
if (e.index ==0)
{
axMap2.TrackingLayer.Refresh(true,0);
}
}
private void axMap2_AfterTrackingLayerDraw(object sender, AxMapObjects2._DMapEvents_AfterTrackingLayerDrawEvent e)
{
MapObjects2.Symbol sym;
sym=new MapObjects2.SymbolClass();
sym.OutlineColor=Convert.ToUInt32 (MapObjects2.ColorConstants.moRed);
sym.Style =1 ;
axMap2.DrawShape(axMap1.Extent,sym);
}
private void axMap2_MouseDownEvent(object sender, AxMapObjects2._DMapEvents_MouseDownEvent e)
{
MapObjects2.Point p;
p=new MapObjects2.PointClass();
p=axMap2.ToMapPoint(e.x,e.y );
if (axMap1.Extent.IsPointIn(p))
{
DragFeedbk .dragstart(axMap1.Extent ,axMap2,e.x,e.y );
}
}
private void axMap2_MouseMoveEvent(object sender, AxMapObjects2._DMapEvents_MouseMoveEvent e)
{
if (e.button==1)
{
DragFeedbk.dragmove(e.x,e.y );
}
}
private void axMap1_MouseUpEvent(object sender, AxMapObjects2._DMapEvents_MouseUpEvent e)
{
}
private void axMap2_MouseUpEvent(object sender, AxMapObjects2._DMapEvents_MouseUpEvent e)
{
axMap2.CtlRefresh();
axMap1.Extent=DragFeedbk.dragfinish(e.x,e.y );
}
}
}
using System;
namespace WindowsApplication68
{
/// <summary>
/// DragFeedback 的摘要说明。
/// </summary>
public class DragFeedback
{
public DragFeedback()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
private static extern bool Rectangle(IntPtr hdc,int ulCornerX, int ulCornerY,int lrCornerX, int lrCornerY);
[System.Runtime.InteropServices.DllImport("user32.dll") ]
private static extern IntPtr GetDC(IntPtr hwnd);
[System.Runtime.InteropServices.DllImport("gdi32.dll") ]
private static extern int SetROP2(IntPtr hdc,int fnDrawMode);
public MapObjects2.Rectangle dragfinish(float x,float y)
{
Rectangle (m_hdc,m_xmin,m_ymin,m_xmax,m_ymax);
r = new MapObjects2.RectangleClass();
// return the rectangle
p = new MapObjects2.PointClass();
p =m_map.ToMapPoint(m_xmin,m_ymin);
r.Left =p.X ;
r.Top =p.Y;
p =m_map.ToMapPoint(m_xmax,m_ymax);
r.Right =p.X ;
r.Bottom =p.Y ;
return r;
}
public void dragmove(float x,float y)
{
float xnext;
float ynext;
xnext=x;
ynext=y;
Rectangle(m_hdc,m_xmin,m_ymin,m_xmax,m_ymax);
m_xmin =m_xmin + Convert.ToInt32 (xnext - m_xprev);
m_xmax =m_xmax + Convert.ToInt32 (xnext - m_xprev);
m_ymin =m_ymin + Convert.ToInt32 (ynext - m_yprev);
m_ymax =m_ymax + Convert.ToInt32 (ynext - m_yprev);
Rectangle(m_hdc,m_xmin,m_ymin,m_xmax,m_ymax);
m_xprev =Convert.ToInt32(xnext);
m_yprev =Convert.ToInt32(ynext);
}
public void dragstart(MapObjects2.IMoRectangle rect,AxMapObjects2.AxMap map,float x,float y)
{
float xmin,ymin;
float xmax,ymax;
m_map =map;
// initialize the hwnd and hdc variables
m_hwnd =m_map.Handle ;
m_hdc =GetDC(m_map.Handle );
SetROP2(m_hdc,10); // raster op for inverting
p =new MapObjects2.PointClass();
p.X =rect.Left;
p.Y =rect.Top;
xmin=0;
ymin=0;
xmax=0;
ymax=0;
m_map.FromMapPoint(p,ref xmin,ref ymin);
p.X =rect.Right;
p.Y =rect.Bottom;
m_map.FromMapPoint(p,ref xmax,ref ymax);
m_xmin =Convert.ToInt32 (xmin);
m_ymin =Convert.ToInt32 (ymin);
m_xmax =Convert.ToInt32 (xmax);
m_ymax =Convert.ToInt32 (ymax);
// draw the rectangle
Rectangle(m_hdc,m_xmin,m_ymin,m_xmax,m_ymax);
// remember the click position
m_xprev =Convert.ToInt32 (x);
m_yprev =Convert.ToInt32(y);
}
protected MapObjects2.Point p ;
protected AxMapObjects2.AxMap m_map;
protected System.IntPtr m_hdc;
protected System.IntPtr m_hwnd;
protected int m_xmin;
protected int m_ymin;
protected int m_xmax;
protected int m_ymax;
protected int m_xprev;
protected int m_yprev;
public MapObjects2.Rectangle r ;
}
}
//程序主体
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace WindowsApplication68
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private AxMapObjects2.AxMap axMap1;
private AxMapObjects2.AxMap axMap2;
private System.Windows.Forms.Button button1;
public DragFeedback DragFeedbk ;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
Windows 窗体设计器生成的代码
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
MapObjects2.IMoDataConnection dc;
MapObjects2.IMoMapLayer layer;
dc=new MapObjects2.DataConnectionClass();
dc.Database=@"f:\data\usa";
if (dc.Connect() )
{
layer=new MapObjects2.MapLayerClass();
layer.GeoDataset=dc.FindGeoDataset("STATES");
layer.Symbol.Color=Convert.ToUInt32(MapObjects2.ColorConstants.moPaleYellow) ;
axMap1.Layers.Add(layer);
layer=new MapObjects2.MapLayerClass();
layer.GeoDataset=dc.FindGeoDataset("ushigh");
layer.Symbol.Color=Convert.ToUInt32(MapObjects2.ColorConstants.moDarkGreen) ;
axMap1.Layers.Add(layer);
layer=new MapObjects2.MapLayerClass();
layer.GeoDataset=dc.FindGeoDataset("uslakes");
layer.Symbol.Color=Convert.ToUInt32(MapObjects2.ColorConstants.moBlue) ;
axMap1.Layers.Add(layer);
layer=new MapObjects2.MapLayerClass();
layer.GeoDataset=dc.FindGeoDataset("STATES");
layer.Symbol.Color=Convert.ToUInt32(MapObjects2.ColorConstants.moPaleYellow) ;
axMap2.Layers.Add(layer);
DragFeedbk =new DragFeedback();
}
}
private void button1_Click(object sender, System.EventArgs e)
{
axMap1.Extent=axMap1.FullExtent;
}
private void axMap1_MouseDownEvent(object sender, AxMapObjects2._DMapEvents_MouseDownEvent e)
{
MapObjects2.Rectangle r;
r=new MapObjects2.RectangleClass();
if (e.button==1)
{
r=axMap1.TrackRectangle();
axMap1.Extent=r;
}
else
{ this.Cursor=Cursors.Hand ;
axMap1.Pan();
}
}
private void axMap1_AfterLayerDraw(object sender, AxMapObjects2._DMapEvents_AfterLayerDrawEvent e)
{
if (e.index ==0)
{
axMap2.TrackingLayer.Refresh(true,0);
}
}
private void axMap2_AfterTrackingLayerDraw(object sender, AxMapObjects2._DMapEvents_AfterTrackingLayerDrawEvent e)
{
MapObjects2.Symbol sym;
sym=new MapObjects2.SymbolClass();
sym.OutlineColor=Convert.ToUInt32 (MapObjects2.ColorConstants.moRed);
sym.Style =1 ;
axMap2.DrawShape(axMap1.Extent,sym);
}
private void axMap2_MouseDownEvent(object sender, AxMapObjects2._DMapEvents_MouseDownEvent e)
{
MapObjects2.Point p;
p=new MapObjects2.PointClass();
p=axMap2.ToMapPoint(e.x,e.y );
if (axMap1.Extent.IsPointIn(p))
{
DragFeedbk .dragstart(axMap1.Extent ,axMap2,e.x,e.y );
}
}
private void axMap2_MouseMoveEvent(object sender, AxMapObjects2._DMapEvents_MouseMoveEvent e)
{
if (e.button==1)
{
DragFeedbk.dragmove(e.x,e.y );
}
}
private void axMap1_MouseUpEvent(object sender, AxMapObjects2._DMapEvents_MouseUpEvent e)
{
}
private void axMap2_MouseUpEvent(object sender, AxMapObjects2._DMapEvents_MouseUpEvent e)
{
axMap2.CtlRefresh();
axMap1.Extent=DragFeedbk.dragfinish(e.x,e.y );
}
}
}