多个窗体(MDI)同时展示,并相互操作这并不困难,小弟在这介绍一下由MapInfo中MapControl组成的多窗体关联处理。
首先看看效果:
以上多关联地图窗体实现了:在同一个窗体中加载多个(最多4个)地图,地图的拖拽、缩放操作可相互关联,每个地图上各个数据都可以完全不相同!
现在我来说一下实现过程:
首先要在同一个窗体中加载多个地图,我采用了UserControl来做:
public partial class ViewMap :System.Windows.Forms.UserControl
在这个ViewMap控件中,我把地图控件所需要的所有东西(包括加载数据的方法、展示图元的方法等)都封装其中。
声明事件
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/-->
/**//// <summary>
/// 传递子控件事件到父窗体的中间事件
/// </summary>
public event MouseEventHandler MapControl_MouseMove;
/**//// <summary>
/// 传递子控件事件到父窗体的中间事件
/// </summary>
public event EventHandler ViewChanged;
/**//// <summary>
/// 控件关闭事件
/// </summary>
public event EventHandler Closed;
/**//// <summary>
/// 在地图控件上点击后触发
/// </summary>
public event MouseEventHandler MapControl_MouseClick;
由于在ViewMap中的MapControl是私有的,MapControl的事件并不能被外界所调用,所以在此声明两个用于传递事件的中间事件,以使各地图控件相互关联操作。当然,这些MapControl的事件需要在ViewMap中注册并调用:
转发事件
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/-->
void mapControl1_MapControl_MouseMove ( object sender, MouseEventArgs e )
{
if ( this.MapControl_MouseMove != null )
{
//把事件转发出去
MapControl_MouseMove ( this, e );
}
}
void mapControl1_MapControl_MouseClick ( object sender, MouseEventArgs e )
{
if ( this.MapControl_MouseClick != null )
{
MapControl_MouseClick ( this, e );
}
}
在以上代码中,需要注意到是,我在调用MapControl_MouseMove事件时,把sender的参数转换为了ViewMap本身,这样做,是为了将来避免死循环。
添加子控件
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/-->
/**//// <summary>
/// 为窗体添加子控件
/// </summary>
/// <param name="childForm">需要添加的子窗体</param>
public void AddChildForm ( ViewMap childForm )
{
if ( !childFormList.Contains ( childForm ) )
{
if ( !( childFormList.Count + 1 > _maxChildForm ) )
{
this.childFormList.Add ( childForm );
}
else
{
MessageBox.Show ( _OVER_MAX_CHILDFORM_COUNT );
return;
}
}
childForm.MapControl_MouseMove += new MouseEventHandler ( vm_MapControl_MouseMove );
childForm.ViewChanged += new EventHandler ( vm_ViewChanged );
childForm.Closed += new EventHandler ( childForm_Closed );
childForm.MapControl_MouseClick += new MouseEventHandler ( childForm_MapControl_MouseClick );
SetChildFormLocation ();
this.Controls.Add ( childForm );
}
执行方法节选
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/-->
void vm_MapControl_MouseMove ( object sender, MouseEventArgs e )
{
foreach ( ViewMap vm in childFormList )
{
if ( !sender.Equals ( vm ) )
{
vm.DrawAssistLine ( e.Location );
}
}
}
void vm_ViewChanged ( object sender, EventArgs e )
{
//遍历所有子窗体,如当前sender不为本身,执行事件
MapControlEventArg mce = e as MapControlEventArg;
foreach ( ViewMap vm in childFormList )
{
if ( !sender.Equals ( vm ) )
{
vm.SetCenter ( mce.ZoomLevel, mce.Center );
}
}
}
在这里,就可以看出来使用ViewMap本身替代sender的优点了,在这里循环判断一下子控件列表,如果不为控件本身,便执行相应方法……