<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->1 /**//// <summary>
2 /// 缩放等级改变事件
3 /// </summary>
4 /// <remarks>在本地图的缩放等级发生变化后触发</remarks>
5 public event EventHandler ZoomLevelChanged;
这个事件的使用如下所示:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> /**//// <summary>
/// 设置地图显示比例
/// </summary>
/// <remarks>窗体上所有缩放事件均进入该方法进行</remarks>
/// <param name="ZoomLevel"></param>
public void SetZoomLevel (int ZoomLevel)
{
_helper.Zoom ( ZoomLevel );
_zoomLevel = _helper.ZoomLevel;
if ( ZoomLevelChanged != null )
{
//触发事件
ZoomLevelChanged ( this, new MapControlEventArg ( _zoomLevel ) );
}
}
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> /// <summary>
/// 设置地图缩放层级
/// </summary>
/// <param name="map">地图实例引用</param>
/// <param name="zoomLevel">缩放层级</param>
private void SetZoomLevel ( Map map,int zoomLevel )
{
try
{
map.Zoom = ExchangeZoomLevel ( zoomLevel );
}
catch
{ }
}
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> /// <summary>
/// 保存对父控件的引用
/// </summary>
private System.Windows.Forms.Control _control;
/// <summary>
/// 维护当前的等级Label
/// </summary>
private List<System.Windows.Forms.Label> levelLabelList;
/// <summary>
/// 控件左上角显示坐标X
/// </summary>
private readonly int xPosition = 10;
/// <summary>
/// 控件左上角显示坐标Y
/// </summary>
private readonly int oginYPosition = 10;
/// <summary>
/// 当前缩放等级Label颜色
/// </summary>
private System.Drawing.Color currentLevelLabelColor = System.Drawing.Color.Blue;
/// <summary>
/// 各控件的ToolTip
/// </summary>
System.Windows.Forms.ToolTip tt;
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> 1 /**//// <summary>
2 /// 构造缩放工具
3 /// </summary>
4 public void BuildZoomTool ( )
5 {
6 System.Windows.Forms.Control mc = ( (MapControl) _control ).mapControl;
7 System.Windows.Forms.Label labelZoomIn = CreateZoomInLabel ();
8 mc.Controls.Add ( labelZoomIn );
9 int yPosition = 32;
10 //循环次数与最大缩放等级有关
11 for ( int i = MapHelper.MinZoomLevel; i <= MapHelper.MaxZoomLevel; i++, yPosition += 15 )
12 {
13 System.Windows.Forms.Label levelLabel = CreateLevelLabel ( i, yPosition+oginYPosition );
14 levelLabelList.Add ( levelLabel );
15 mc.Controls.Add ( levelLabel );
16 }
17 System.Windows.Forms.Label labelZoomOut = CreateZoomOutLabel ( yPosition+oginYPosition );
18 mc.Controls.Add ( labelZoomOut );
19 }
20
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> 1 private System.Windows.Forms.Label CreateLevelLabel ( int zoomLevel,int yPosition )
2 {
3 System.Windows.Forms.Label label = new System.Windows.Forms.Label ();
4 label.BackColor = System.Drawing.Color.White;
5 label.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
6 label.Font = new System.Drawing.Font ( "宋体", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ( (byte) ( 134 ) ) );
7 label.Text = " ";
8 label.Name = "lblLevel" + zoomLevel.ToString ();
9 label.Tag = zoomLevel;
10 label.Cursor = System.Windows.Forms.Cursors.Hand;
11 label.Location = new System.Drawing.Point ( xPosition, yPosition );
12 label.Size = new System.Drawing.Size ( 37, 11 );
13 label.Click += new EventHandler ( label_Click );
14 tt = new System.Windows.Forms.ToolTip ();
15 tt.ShowAlways = true;
16 tt.SetToolTip ( label, "缩放地图到"+zoomLevel.ToString()+"级" );
17 return label;
18 }
19
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->1 void label_Click ( object sender, EventArgs e )
2 {
3 System.Windows.Forms.Label label = (System.Windows.Forms.Label) sender;
4 MapControl mc = (MapControl) _control;
5 mc.SetZoomLevel ( Int32.Parse ( label.Tag.ToString () ) );
6 }
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> 1 public void mc_Map_MouseDoubleClick ( object sender, MouseEventArgs e )
2 {
3 switch ( e.Button )
4 {
5 case MouseButtons.Left:
6 ZoomIn ();
7 break;
8 case MouseButtons.Right:
9 ZoomOut ();
10 break;
11 }
12 _helper.SetCenter ( e.Location );
13 }
14
这是最后的效果,当点击任意的缩放等级,地图就会缩放到适当的大小