//类

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 窗体设计器生成的代码#region Windows 窗体设计器生成的代码

/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()

{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.axMap1 = new AxMapObjects2.AxMap();
this.axMap2 = new AxMapObjects2.AxMap();
this.button1 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.axMap1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.axMap2)).BeginInit();
this.SuspendLayout();
//
// axMap1
//
this.axMap1.Location = new System.Drawing.Point(16, 8);
this.axMap1.Name = "axMap1";
this.axMap1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axMap1.OcxState")));
this.axMap1.Size = new System.Drawing.Size(296, 336);
this.axMap1.TabIndex = 0;
this.axMap1.MouseDownEvent += new AxMapObjects2._DMapEvents_MouseDownEventHandler(this.axMap1_MouseDownEvent);
this.axMap1.AfterLayerDraw += new AxMapObjects2._DMapEvents_AfterLayerDrawEventHandler(this.axMap1_AfterLayerDraw);
this.axMap1.MouseUpEvent += new AxMapObjects2._DMapEvents_MouseUpEventHandler(this.axMap1_MouseUpEvent);
//
// axMap2
//
this.axMap2.Location = new System.Drawing.Point(328, 24);
this.axMap2.Name = "axMap2";
this.axMap2.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axMap2.OcxState")));
this.axMap2.Size = new System.Drawing.Size(184, 176);
this.axMap2.TabIndex = 1;
this.axMap2.MouseDownEvent += new AxMapObjects2._DMapEvents_MouseDownEventHandler(this.axMap2_MouseDownEvent);
this.axMap2.AfterTrackingLayerDraw += new AxMapObjects2._DMapEvents_AfterTrackingLayerDrawEventHandler(this.axMap2_AfterTrackingLayerDraw);
this.axMap2.MouseMoveEvent += new AxMapObjects2._DMapEvents_MouseMoveEventHandler(this.axMap2_MouseMoveEvent);
this.axMap2.MouseUpEvent += new AxMapObjects2._DMapEvents_MouseUpEventHandler(this.axMap2_MouseUpEvent);
//
// button1
//
this.button1.Location = new System.Drawing.Point(376, 264);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(88, 32);
this.button1.TabIndex = 2;
this.button1.Text = "full ";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(528, 365);
this.Controls.Add(this.button1);
this.Controls.Add(this.axMap2);
this.Controls.Add(this.axMap1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.axMap1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.axMap2)).EndInit();
this.ResumeLayout(false);

}
#endregion


/**//// <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 );

}


}
}

