⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 frmmain.cs

📁 最主要的描述了鹰眼功能的实现其中还有源码。谢谢大家下载!
💻 CS
📖 第 1 页 / 共 2 页
字号:

		
		}

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new frmMain());
		}

		private void Map_ViewChanged(object o, ViewChangedEventArgs e) 
		{	
			try
			{
				//判断,如果mapMain有地图存在,则自定义的工具条可用,否则不可用
				if(mapMain.Map.Layers.Count>0)
				{
					toolViewEntire.Enabled=true;
					toolSelectNone.Enabled=true;	
					toolBarRuler.Enabled=true;
					toolBarArea.Enabled=true;
				}
				else
				{
					toolViewEntire.Enabled=false;
					toolSelectNone.Enabled=false;	
					toolBarArea.Enabled=false;
				}

				//显示当前缩放尺度		
				MapInfo.Mapping.Map map = (MapInfo.Mapping.Map) o;			
				Double dblZoom = System.Convert.ToDouble(String.Format("{0:E2}", mapMain.Map.Zoom.Value ));
				statusBarMain.Text = "缩放:"+dblZoom.ToString()+" "+ MapInfo.Geometry.CoordSys.DistanceUnitAbbreviation(mapMain.Map.Zoom.Unit);

				if (panEye.Visible==true)			
				{

					//加载鹰眼矩形临时表
					Table tblRect;
					tblRect=Session.Current.Catalog.GetTable("TempRect");
					if (tblRect!=null)
						tblRect.Close();
					TableInfo tblInfo ;
					tblInfo=TableInfoFactory.CreateTemp("TempRect");
					TableSessionInfo tblSessionInfo =new TableSessionInfo();            
				
					tblRect=Session.Current.Catalog.CreateTable(tblInfo,tblSessionInfo);
					FeatureLayer feaLayer=new FeatureLayer(tblRect);
					mapEye.Map.Layers.Insert(0,feaLayer);

					//实时在鹰眼临时表图上画矩形
					tblRect=Session.Current.Catalog.GetTable("TempRect");					
					(tblRect as ITableFeatureCollection).Clear();//清除当前层上的图元

					//设置矩形的样式
					DRect rect=mapMain.Map.Bounds ;
					FeatureGeometry  feageo=new MapInfo.Geometry.Rectangle(mapMain.Map.GetDisplayCoordSys(),rect);
					SimpleLineStyle simLineStyle=new SimpleLineStyle(new LineWidth(2,MapInfo.Styles.LineWidthUnit.Point ),2,System.Drawing.Color.Red   );
					SimpleInterior  simInterior=new SimpleInterior(9,System.Drawing.Color.Gray  ,System.Drawing.Color.Green,true);
					CompositeStyle comStyle=new CompositeStyle(new AreaStyle(simLineStyle,simInterior),null,null,null);

					//将矩形插入到图层中
					Feature fea=new Feature(feageo,comStyle);
					tblRect.InsertFeature(fea);	
					mapEye.Map.Layers["TempRect"].Invalidate();

					//清理对象变量
					tblSessionInfo=null;
					feageo=null;
					simLineStyle=null;
					simInterior=null;
					comStyle=null;
					fea=null;
				}
			}
			catch (Exception Ex)
			{
                MessageBox.Show ("鹰眼错误:"+Ex.Message.ToString());			
			
			}			
		}
	

		private void mnuDispToolBar_Click(object sender, System.EventArgs e)
		{
			mnuDispToolBar.Checked=!mnuDispToolBar.Checked ;
			if (mnuDispToolBar.Checked)
			{
				mapToolBarMain.Visible=true;
			
			}
			else
			{
				mapToolBarMain.Visible=false;
			
			}		
		}

		private void mnuOpenMap_Click(object sender, System.EventArgs e)
		{
			
			try
			{  
				//加载地图工作空间,该文件由worlSpace管理器生成
				OpenFileDialog openFileDialog1=new OpenFileDialog();
				openFileDialog1.Multiselect = false;
				openFileDialog1.CheckFileExists = true;
				openFileDialog1.DefaultExt = "MWS";
				openFileDialog1.Filter = "地图文件(*.mws)|*.mws||";
				if(openFileDialog1.ShowDialog(this) == DialogResult.OK) 
				{
					//清理原来地图
					mapMain.Map.Clear();
					mapEye.Map.Clear(); 
					Session.Current.Catalog.CloseAll();

					//加载工作空间地图	
					MapWorkSpaceLoader mwl = new MapWorkSpaceLoader(openFileDialog1.FileName);
					mapMain.Map.Load(mwl);
					mapEye.Map.Load(mwl);
					
				}		
				
			}
			catch(Exception Ex)
			{
				MessageBox.Show ("加载地图错误:"+Ex.Message.ToString());
			}
		}

		private void mnuExit_Click(object sender, System.EventArgs e)
		{
			this.Close();
			Application.Exit();
		}

		private void mnuDispEye_Click(object sender, System.EventArgs e)
		{
			mnuDispEye.Checked=!mnuDispEye.Checked ;
			if (mnuDispEye.Checked)
			{
				this.panEye.Visible=true;
			
			}
			else
			{
				this.panEye.Visible=false;
			
			}		
		}

		private void frmMain_Resize(object sender, System.EventArgs e)
		{		
									
			if ( this.ClientSize.Width -this.panEye .Width >0)
			{
				this.panEye.Left=this.ClientSize.Width -panEye.Width ;
			}
			if (this.ClientSize.Height   -this.panEye .Height-this.statusBarMain .Height  >0)
			{
				this.panEye.Top =this.ClientSize.Height  -this.panEye .Height-this.statusBarMain.Height ;
			}
		}

		private void mnuStatusBar_Click(object sender, System.EventArgs e)
		{
			mnuStatusBar.Checked=!mnuStatusBar.Checked ;
			if (mnuStatusBar.Checked)
			{
				this.statusBarMain.Visible=true;
			
			}
			else
			{
				this.statusBarMain.Visible=false;
			
			}				
		}

		private void frmMain_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			mapMain.Map.Clear();
			mapEye.Map.Clear();
			Session.Current.Catalog.CloseAll();
		}
		

		private void frmMain_Load(object sender, System.EventArgs e)
		{
			boolEyeMove=false;
		}
		
		private void mapEye_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			try
			{
				if( boolEyeMove==true)
				{
					System.Drawing.Point pt = new System.Drawing.Point(e.X, e.Y);
					MapInfo.Geometry.DPoint dptCenter;
					mapEye.Map.DisplayTransform.FromDisplay(pt,out dptCenter); 
					mapMain.Map.Center = dptCenter;
				}	
			}
			catch(Exception Ex)
			{
				MessageBox.Show ("鹰眼错误:"+Ex.Message.ToString());
			}
		}

		private void mapEye_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			boolEyeMove=false;		
		}

		private void mapEye_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			try
			{
				boolEyeMove=true;
				System.Drawing.Point pt = new System.Drawing.Point(e.X, e.Y);
				MapInfo.Geometry.DPoint dptCenter;
				mapEye.Map.DisplayTransform.FromDisplay(pt,out dptCenter); 
				mapMain.Map.Center = dptCenter;
			}
			catch(Exception Ex)
			{
				MessageBox.Show ("鹰眼错误:"+Ex.Message.ToString());
			}
		
		}

		private void mapToolBarMain_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
		{
			try
			{
				if (e.Button.Tag==null)			
					return;
				string strTag=e.Button.Tag.ToString();
				switch (strTag)
				{
					case "viewEntire":
						Map map = mapMain.Map;
						IMapLayerFilter lyrFilter = new FilterByLayerType(LayerType.Normal );
						MapLayerEnumerator lyrEnum= map.Layers.GetMapLayerEnumerator(lyrFilter);
						map.SetView(lyrEnum);
						break;
					case "selectNone":
						Session.Current.Selections.DefaultSelection.Clear ();
						break;
					case "ruler":					
						mapMain.Tools.LeftButtonTool  ="DistanceTool";					
						break;
					case "Area":					
						mapMain.Tools.LeftButtonTool  ="AreaTool";					
						break;
					default:
						break;
				}
			}
			catch(Exception Ex)
			{
				MessageBox.Show ("工具条错误:"+Ex.Message.ToString());
			}
		}


		private void Tools_Used(object sender,MapInfo.Tools.ToolUsedEventArgs e)
		{
			try
			{				
				switch (e.ToolName)
				{
					case "DistanceTool":	
					switch (e.ToolStatus)
					{		
						case MapInfo.Tools.ToolStatus.Start:
							dblDistance = 0;
							dptStart = e.MapCoordinate ;						    
							break;
						case MapInfo.Tools.ToolStatus.InProgress:
							dblDistance += MapInfo.Geometry.CoordSys.Distance(MapInfo.Geometry.DistanceType.Spherical ,mapMain.Map.Zoom.Unit,mapMain.Map.GetDisplayCoordSys(),dptStart,e.MapCoordinate);
							dptStart = e.MapCoordinate;
							break;
						case MapInfo.Tools.ToolStatus.End:
							dblDistance += MapInfo.Geometry.CoordSys.Distance(MapInfo.Geometry.DistanceType.Spherical ,mapMain.Map.Zoom.Unit,mapMain.Map.GetDisplayCoordSys(),dptStart,e.MapCoordinate);
							MessageBox.Show ("总长度为:"+string.Format ("{0:F3}",dblDistance)+" "+ MapInfo.Geometry.CoordSys.DistanceUnitAbbreviation(mapMain.Map.Zoom.Unit).ToString());
							mapMain.Map.Invalidate(true);
								
							break;		
						default:
							break;
					}					
						break;

					case "AreaTool":	
					switch (e.ToolStatus)
					{							
						case MapInfo.Tools.ToolStatus.Start:
							arrlstPoints.Clear();
							dptFirstPoint=e.MapCoordinate;
							arrlstPoints.Add (e.MapCoordinate);						    
							break;
						case MapInfo.Tools.ToolStatus.InProgress:
							arrlstPoints.Add (e.MapCoordinate);
							break;
						case MapInfo.Tools.ToolStatus.End:
							//构造一个闭合环
							arrlstPoints.Add (e.MapCoordinate);
							int intCount=arrlstPoints.Count ;
							if (intCount<=3 )
							{
								MessageBox.Show("请画3个以上的点形成面来测量你所要的面积");
								return;
							}
							MapInfo.Geometry.DPoint[] dptPoints=new DPoint[intCount];
							for (int i=0;i<=intCount-1 ;i++)
							{
								dptPoints[i]=(MapInfo.Geometry.DPoint)arrlstPoints[i];
							
							}
							dptPoints[intCount-1]=dptFirstPoint ;

							//用闭合的环构造一个面		
							MapInfo.Geometry.AreaUnit costAreaUnit;
							costAreaUnit=MapInfo.Geometry.CoordSys.GetAreaUnitCounterpart(mapMain.Map.Zoom.Unit);
							MapInfo.Geometry.CoordSys objCoordSys=this.mapMain.Map.GetDisplayCoordSys();
							MapInfo.Geometry.Polygon  objPolygon=new Polygon(objCoordSys,MapInfo.Geometry.CurveSegmentType.Linear,dptPoints);
							if (objPolygon==null)
								return;														
							MessageBox.Show ("总面积为:"+string.Format ("{0:F3}",objPolygon.Area(costAreaUnit))+"  "+MapInfo.Geometry.CoordSys.AreaUnitAbbreviation (costAreaUnit));
							mapMain.Map.Invalidate(true);								
							break;		
						default:
							break;
					}
						break;
				    //可以添加其他的用户自定义Tool
					default:
						break;
				}
			}
			catch(Exception Ex)
			{
				MessageBox.Show ("测量错误:"+Ex.Message.ToString());
			}			
		}		
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -