C#+MO实现一个道路编辑软件(刚开始)_C#教程-查字典教程网
C#+MO实现一个道路编辑软件(刚开始)
C#+MO实现一个道路编辑软件(刚开始)
发布时间:2016-12-28 来源:查字典编辑
摘要://**********************************************************//******主窗...

//**********************************************************

//******主窗口程序

//********************************************************

usingSystem;

usingSystem.Collections.Generic;

usingSystem.ComponentModel;

usingSystem.Data;

usingSystem.Drawing;

usingSystem.Text;

usingSystem.Windows.Forms;

namespaceIRND_DPT

{

publicpartialclassfrmMain:Form

{

IRND_DPT.layerVariablepubLayerVariable=newlayerVariable();

publicfrmMain()

{

InitializeComponent();

}

//添加图层

privatevoidtlbAddLayer_Click(objectsender,EventArgse)

{

IRND_DPT.OpenFileAddFile=newOpenFile();

AddFile.OpenShapeFiles(CD1,axMap1);

objectrefMap=(object)this.axMap1;

boolrefTrue=true;

shortrefShort=0;

axlegend1.setMapSource(refrefMap);

axlegend1.ShowAllLegend();

axlegend1.LoadLegend(refrefTrue);

axlegend1.set_Active(refrefShort,true);

axMap1.Refresh();

axlegend1.Refresh();

//AddFile(CD1,axMap1);

}

//删除图层

privatevoidtoolStripButton4_Click(objectsender,EventArgse)

{

if(pubLayerVariable.MapLayerIndex>=0)

{

for(inti=0;i<=axMap1.Layers.Count-1;i++)

{

axMap1.Layers.Remove(pubLayerVariable.MapLayerIndex);

break;

}

boolrefTrue=true;

axMap1.Refresh();

axlegend1.LoadLegend(refrefTrue);

}

}

privatevoidaxlegend1_AfterSetLayerVisible(objectsender,AxSampleLegendControl.__legend_AfterSetLayerVisibleEvente)

{

axMap1.Refresh();

}

privatevoidaxlegend1_LayerDblClick(objectsender,AxSampleLegendControl.__legend_LayerDblClickEvente)

{

}

privatevoidaxlegend1_MouseDownEvent(objectsender,AxSampleLegendControl.__legend_MouseDownEvente)

{

if(e.index>=0)

{

MapObjects2.MapLayerlayer=(MapObjects2.MapLayer)axMap1.Layers.Item(e.index);

pubLayerVariable.MapLayerName=layer.Name;

pubLayerVariable.MapLayerIndex=e.index;

}

axMap1.TrackingLayer.Refresh(true,axMap1.Extent);

}

//放大

privatevoidtlb_ZoomIn_Click(objectsender,EventArgse)

{

axMap1.MousePointer=MapObjects2.MousePointerConstants.moZoomIn;

}

//缩小

privatevoidtlb_ZoomOut_Click(objectsender,EventArgse)

{

axMap1.MousePointer=MapObjects2.MousePointerConstants.moZoomOut;

}

//漫游

privatevoidtlb_Pan_Click(objectsender,EventArgse)

{

axMap1.MousePointer=MapObjects2.MousePointerConstants.moPan;

}

//全图

privatevoidtbl_Full_Click(objectsender,EventArgse)

{

axMap1.Extent=axMap1.FullExtent;

axMap1.MousePointer=MapObjects2.MousePointerConstants.moArrow;

}

//逐渐放大

privatevoidtbl_SmallIn_Click(objectsender,EventArgse)

{

MapObjects2.Rectangler=axMap1.Extent;

r.ScaleRectangle(0.9);

axMap1.Extent=r;

}

//逐渐缩小

privatevoidtbl_SmallOut_Click(objectsender,EventArgse)

{

MapObjects2.Rectangler=axMap1.Extent;

r.ScaleRectangle(1.1);

axMap1.Extent=r;

}

//选择查询

privatevoidtbl_Identify_Click(objectsender,EventArgse)

{

axMap1.MousePointer=MapObjects2.MousePointerConstants.moIdentify;

}

//属性浏览

privatevoidtoolStripButton2_Click(objectsender,EventArgse)

{

if(pubLayerVariable.MapLayerIndex>=0&pubLayerVariable.MapLayerName!=null)

{

MapObjects2.MapLayerlyr=(MapObjects2.MapLayer)axMap1.Layers.Item(pubLayerVariable.MapLayerName);

IRND_DPT.frmBrowseAttrfrmBrowset=newfrmBrowseAttr();

frmBrowset.IniListview(lyr);

frmBrowset.ShowDialog(this);

}

}

//地图响应事件

privatevoidaxMap1_MouseDownEvent(objectsends,AxMapObjects2._DMapEvents_MouseDownEvente)

{

MapObjects2.Rectanglerect;

MapObjects2.Pointcurp;

MapObjects2.MapLayerlyr;

MapObjects2.Recordsetrest;

switch(axMap1.MousePointer)

{

//放大

caseMapObjects2.MousePointerConstants.moZoomIn:

{

rect=axMap1.TrackRectangle();

if(rect.Width==0||rect.Height==0)

{

rect=axMap1.Extent;

rect.ScaleRectangle(0.5);

}

axMap1.Extent=rect;

break;

}

//缩小

caseMapObjects2.MousePointerConstants.moZoomOut:

{

MapObjects2.RectangleTempr;

Tempr=axMap1.Extent;

rect=axMap1.TrackRectangle();

doubleNewSR;

if(rect.Width!=0||rect.Height!=0)

{

if(axMap1.Extent.Width/rect.Width>axMap1.Extent.Height/rect.Height)

{

NewSR=axMap1.Extent.Height/rect.Height;

}

else

{

NewSR=axMap1.Extent.Width/rect.Width;

}

Tempr.ScaleRectangle(NewSR);

}

else

{

Tempr.ScaleRectangle(2.0);

}

axMap1.Extent=Tempr;

break;

}

//漫游

caseMapObjects2.MousePointerConstants.moPan:

{

axMap1.Pan();

break;

}

//选择查询

caseMapObjects2.MousePointerConstants.moIdentify:

{

if(pubLayerVariable.MapLayerIndex>=0&&pubLayerVariable.MapLayerName!="")

{

rect=axMap1.TrackRectangle();

lyr=(MapObjects2.MapLayer)axMap1.Layers.Item(pubLayerVariable.MapLayerIndex);

if(rect.Width==0)

{

curp=axMap1.ToMapPoint(e.x,e.y);

rest=lyr.SearchByDistance(curp,(double)axMap1.ToMapDistance((float)Screen.PrimaryScreen.WorkingArea.X*5),"");

}

else

{

rest=lyr.SearchShape(rect,MapObjects2.SearchMethodConstants.moAreaIntersect,"");

}

if(rest.EOF!=true)

{

axMap1.FlashShape(rest.Fields.Item("shape").Value,2);

IRND_DPT.frmIdentifyFunctionClass=newfrmIdentify();

////FunctionClass.Close();

FunctionClass.CurRecordSet(rest);

FunctionClass.IniTvFeat(rest,lyr.Name);

FunctionClass.IniLvwAttr(rest);

FunctionClass.Show(this);

}

}

break;

}

}

}

////////

///////////////////////////////////////////////////////////////////////////////////////////////

}

}

//*************************************************

//********属性查询

//************************************************

usingSystem;

usingSystem.Collections.Generic;

usingSystem.ComponentModel;

usingSystem.Data;

usingSystem.Drawing;

usingSystem.Text;

usingSystem.Windows.Forms;

namespaceIRND_DPT

{

publicpartialclassfrmIdentify:Form

{

MapObjects2.Recordsetmrs;

publicfrmIdentify()

{

InitializeComponent();

}

privatevoidfrmIdentify_Load(objectsender,System.EventArgse)

{

lvwAttr.View=View.Details;

lvwAttr.Columns.Add("字段",50,HorizontalAlignment.Left);

lvwAttr.Columns.Add("值",50,HorizontalAlignment.Center);

}

//

privatevoidtvFeat_NodeMouseClick(objectsender,System.Windows.Forms.TreeNodeMouseClickEventArgse)

{

stringID;

ListViewItemItem;

//if(e.Node.Parent==null){return;}

lvwAttr.Items.Clear();

ID=e.Node.Text;

mrs.MoveFirst();

while(mrs.EOF!=true)

{

if(ID==mrs.Fields.Item("FeatureID").ValueAsString)

{

for(shortfld=1;fld<mrs.TableDesc.FieldCount;fld++)

{

Item=lvwAttr.Items.Add(mrs.TableDesc.get_FieldName(fld));

Item.SubItems.Add(mrs.Fields.Item(Item.Text).ValueAsString);

}

lvwAttr.Refresh();

break;

}

mrs.MoveNext();

}

}

publicvoidCurRecordSet(MapObjects2.RecordsetvNewValue)

{

mrs=vNewValue;

}

publicvoidIniLvwAttr(MapObjects2.Recordsetrst)

{

TreeNoden=null;

ListViewItemItem;

stringID;

lvwAttr.Items.Clear();

if(tvFeat.Nodes.Count>=2)

{

inti=0;

foreach(TreeNodetnintvFeat.Nodes)

{

i=i+1;

if(i==2)

{

n=tn;

break;

}

}

if(n.Text!=null)

{

rst.MoveFirst();

ID=rst.Fields.Item("FeatureID").ValueAsString;

while(rst.EOF!=true)

{

if(ID==n.Text)

{

for(shortfld=1;fld<rst.TableDesc.FieldCount;fld++)

{

Item=lvwAttr.Items.Add(rst.TableDesc.get_FieldName(fld));

Item.SubItems.Add(rst.Fields.Item(Item.Text).ValueAsString);

}

lvwAttr.Refresh();

break;

}

}

}

}

}

//将选择的记录集加入到树型控件中进行显示

publicvoidIniTvFeat(MapObjects2.RecordsetmoRs_in,stringlyrName_in)

{

TreeNoden;

stringID;

if(moRs_in.EOF!=true)

{

moRs_in.MoveFirst();

tvFeat.Nodes.Clear();

n=tvFeat.Nodes.Add(lyrName_in);

n=n.Parent;

while(moRs_in.EOF!=true)

{

ID=moRs_in.Fields.Item("FeatureID").ValueAsString;

n=tvFeat.Nodes.Add(ID);

moRs_in.MoveNext();

}

}

}

///////zuihou////////////////////////////////////////////////////////////////////////////

}

}

//*************************************************************

//************加载图层函数

//*************************************************************

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Text;

usingSystem.Windows.Forms;

namespaceIRND_DPT

{

classOpenFile

{

//功能说明:

//从公共对话框中提取文件名,然后调用不同的子模块处理不同的文件加入图层的问题(支持打开多个文件)

publicvoidOpenShapeFiles(OpenFileDialogcomopenfile,AxMapObjects2.AxMapmap)

{

try

{

stringstrShape,strCov,strCAD,strVPF,strImage,strMilImage;

//将过滤器设置为可支持的文件

strShape="ESRIShapeFiles(*.shp)|*.shp";

strCov="ESRICoverages(*.adf,*.tat,*.pat,*.rat)|aat.adf;pat.adf;nat.adf;txt.adf;*.tat;*.pat;*.rat";

strCAD="AutoCADFile(*.dwg,*.dxf)|*.dwg;*.dxf";

strImage="AllImage(*.bmp,*.dib,*.tif,*.jpg,*.jff,*.bil,*.bip,*.bsq,*.gis,*.lan,*.rlc,*.sid,*.sun,*.rs,*.ras,*.svf,*.img,*.gif)|*.bmp,*.dib,*.tif,*.jpg,*.jff,*.bil,*.bip,*.bsq,*.gis,*.lan,*.rlc,*.sid,*.sun,*.rs,*.ras,*.svf,*.img,*.gif";

comopenfile.CheckFileExists=true;

//设置过滤

comopenfile.Filter=strShape+"|"+strCov+"|"+strCAD+"|"+strImage;

comopenfile.Title="添加图层";

//允许选择多个文件,并允许长文件名

comopenfile.Multiselect=true;

if(comopenfile.ShowDialog()==DialogResult.OK)

{

foreach(stringstrFilenameincomopenfile.FileNames)

{

stringsPath=strFilename.Substring(0,strFilename.LastIndexOf("")+1);

stringsFile=strFilename.Substring(strFilename.LastIndexOf("")+1,strFilename.Length-strFilename.LastIndexOf("")-1);

stringnametype=sFile.Substring(sFile.LastIndexOf(".")+1,sFile.Length-sFile.LastIndexOf(".")-1);

open_file(sPath,sFile,nametype,map);

}

}

}

catch

{

MessageBox.Show("图层添加有错!");

}

}

//功能说明:

//测试文件类型,一便调用不同的子模块.处理不同图层加入的问题

privatevoidopen_file(stringPath,stringfilename,stringfiletype,AxMapObjects2.AxMapmapobject)

{

stringLayerName=filename.Substring(0,filename.LastIndexOf("."));

switch(filetype)

{

case"shp":

shpAdd(Path,filename,mapobject,LayerName,true);

break;

}

}

//功能说明:

//添加shape格式的文件

publicvoidshpAdd(stringdatabasepath,stringfilename,AxMapObjects2.AxMapmapobject,stringLayerName,boolHide)

{

MapObjects2.DataConnectiondCon=newMapObjects2.DataConnection();

//MapObjects2.GeoDatasetgSet=newMapObjects2.GeoDataset();

MapObjects2.MapLayernewLayer=newMapObjects2.MapLayer();

longi=0;

try

{

dCon.Database=databasepath;

if(dCon.Connect())

{

MapObjects2.GeoDatasetgSet=dCon.FindGeoDataset(filename);

if(gSet==null)

{

return;

}

else

{

//查看当前是否有重复的图层名,以确保所有的图层名都是唯一的

if(FindMapLayerName(mapobject,LayerName)==true)

{

stringtempstr=LayerName;

while(FindMapLayerName(mapobject,tempstr+"-"+i)==true)

{

i++;

}

LayerName=LayerName+"-"+i;

}

newLayer.GeoDataset=gSet;

if(Hide)

{

newLayer.Visible=true;

}

else

{

newLayer.Visible=false;

}

//使用默认颜色和样式设置图层

switch(newLayer.shapeType)

{

caseMapObjects2.ShapeTypeConstants.moShapeTypePoint:

newLayer.Symbol.Color=11513775;

newLayer.Symbol.Size=5;

break;

caseMapObjects2.ShapeTypeConstants.moShapeTypeLine:

newLayer.Symbol.Color=11513775;

newLayer.Symbol.Size=1;

break;

caseMapObjects2.ShapeTypeConstants.moShapeTypePolygon:

newLayer.Symbol.SymbolType=0;

newLayer.Symbol.Style=8;

newLayer.Symbol.Color=12566463;

newLayer.Symbol.Outline=true;

newLayer.Symbol.OutlineColor=(int)MapObjects2.ColorConstants.moBlack;

break;

}

mapobject.Layers.Add(newLayer);

newLayer.Name=LayerName;

newLayer.Tag=databasepath+filename;

}

}

}

catch

{

//MessageBox("加载图层出错!");

}

}

//功能说明:

//查找map中的图层AxMapObjects2.AxMapmapobject,stringLayerName

publicboolFindMapLayerName(AxMapObjects2.AxMapmapobject,stringLayerName)

{

for(inti=0;i<mapobject.Layers.Count;i++)

{

if(mapobject.Layers.Item(i).ToString()==LayerName)

{

returntrue;

}

}

returnfalse;

}

}

}

相关阅读
推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
  • 大家都在看
  • 小编推荐
  • 猜你喜欢
  • 最新C#教程学习
    热门C#教程学习
    编程开发子分类