C#+MO实现一个道路编辑软件(刚开始)

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

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

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

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace IRND_DPT
{
    public partial class frmMain : Form
    {
        IRND_DPT.layerVariable pubLayerVariable = new layerVariable ();
        public frmMain()
        {
            InitializeComponent();
        }
        //添加图层
        private void tlbAddLayer_Click(object sender, EventArgs e)
        {
            IRND_DPT.OpenFile AddFile = new OpenFile();
            AddFile.OpenShapeFiles(CD1,axMap1 );

object refMap = (object)this.axMap1;
            bool refTrue = true;
            short refShort = 0;
            axlegend1.setMapSource(ref refMap);
            axlegend1.ShowAllLegend();
            axlegend1.LoadLegend(ref refTrue);
            axlegend1.set_Active(ref refShort, true);
            axMap1.Refresh();
            axlegend1.Refresh();

//AddFile(CD1, axMap1);
        }
        //删除图层
        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            if (pubLayerVariable.MapLayerIndex >= 0)
            {
                for (int i = 0; i <= axMap1.Layers.Count - 1; i++)
                {
                    axMap1.Layers.Remove(pubLayerVariable.MapLayerIndex);
                    break;
                }
                bool refTrue = true;
                axMap1.Refresh();
                axlegend1.LoadLegend(ref refTrue);
            }

}
        private void axlegend1_AfterSetLayerVisible(object sender,AxSampleLegendControl.__legend_AfterSetLayerVisibleEvent e)
        {
            axMap1.Refresh();            
        }

private void axlegend1_LayerDblClick(object sender, AxSampleLegendControl.__legend_LayerDblClickEvent e)
        {

}

private void axlegend1_MouseDownEvent(object sender, AxSampleLegendControl.__legend_MouseDownEvent e)
        {

if (e.index >= 0)
            {
                MapObjects2.MapLayer layer= (MapObjects2.MapLayer )axMap1.Layers.Item(e.index );
                pubLayerVariable.MapLayerName = layer.Name;
                pubLayerVariable.MapLayerIndex = e.index;                
            }
            axMap1.TrackingLayer.Refresh(true, axMap1.Extent);
        }
        //放大
        private void tlb_ZoomIn_Click(object sender, EventArgs e)
        {
            axMap1.MousePointer = MapObjects2.MousePointerConstants.moZoomIn;
        }
        //缩小
        private void tlb_ZoomOut_Click(object sender, EventArgs e)
        {
            axMap1.MousePointer = MapObjects2.MousePointerConstants.moZoomOut;
        }
        //漫游
        private void tlb_Pan_Click(object sender, EventArgs e)
        {
            axMap1.MousePointer = MapObjects2.MousePointerConstants.moPan;
        }
        //全图
        private void tbl_Full_Click(object sender, EventArgs e)
        {
            axMap1.Extent = axMap1.FullExtent;
            axMap1.MousePointer = MapObjects2.MousePointerConstants.moArrow;
        }
        //逐渐放大
        private void tbl_SmallIn_Click(object sender, EventArgs e)
        {
            MapObjects2.Rectangle r = axMap1.Extent;
            r.ScaleRectangle(0.9);
            axMap1.Extent = r;
        }
        //逐渐缩小
        private void tbl_SmallOut_Click(object sender, EventArgs e)
        {
            MapObjects2.Rectangle r = axMap1.Extent;
            r.ScaleRectangle(1.1);
            axMap1.Extent = r;
        }
        //选择查询
        private void tbl_Identify_Click(object sender, EventArgs e)
        {
            axMap1.MousePointer = MapObjects2.MousePointerConstants.moIdentify;
        }
        //属性浏览
        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            if (pubLayerVariable.MapLayerIndex >= 0 & pubLayerVariable.MapLayerName != null)
            {
                MapObjects2.MapLayer lyr = (MapObjects2.MapLayer )axMap1.Layers.Item(pubLayerVariable.MapLayerName);
                IRND_DPT.frmBrowseAttr frmBrowset = new frmBrowseAttr();                
                frmBrowset.IniListview(lyr);
                frmBrowset.ShowDialog(this); 
            }
        }
        //地图响应事件
        private void axMap1_MouseDownEvent(object sends, AxMapObjects2._DMapEvents_MouseDownEvent e)
        {
            MapObjects2.Rectangle rect;
            MapObjects2.Point curp;
            MapObjects2.MapLayer lyr;
            MapObjects2.Recordset rest;

switch (axMap1.MousePointer)
            {
                //放大                    
                case MapObjects2.MousePointerConstants.moZoomIn:
                {                    
                    rect = axMap1.TrackRectangle();
                    if (rect.Width == 0 || rect.Height == 0)
                    {
                        rect = axMap1.Extent;
                        rect.ScaleRectangle(0.5);
                    }
                    axMap1.Extent = rect;
                    break;
                }
                //缩小
                case MapObjects2.MousePointerConstants.moZoomOut:
                {
                    MapObjects2.Rectangle Tempr;
                    Tempr = axMap1.Extent;
                    rect = axMap1.TrackRectangle();
                    double NewSR;
                    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;
                }
                //漫游
                case MapObjects2.MousePointerConstants.moPan:
                {
                    axMap1.Pan();
                    break;
                }
                //选择查询
            case MapObjects2.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.frmIdentify FunctionClass = new frmIdentify();
                            ////FunctionClass.Close();  
                            FunctionClass.CurRecordSet(rest);
                            FunctionClass.IniTvFeat(rest, lyr.Name);
                            FunctionClass.IniLvwAttr(rest);
                            FunctionClass.Show(this);
                        }
                    }
                    break;
                }
            }
        }

////////
        ///////////////////////////////////////////////////////////////////////////////////////////////
    }
}

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

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

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

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace IRND_DPT
{
    public partial class frmIdentify : Form
    {
        MapObjects2.Recordset mrs;

public frmIdentify()
        {
            InitializeComponent();
        }
        private void frmIdentify_Load(object sender, System.EventArgs e)
        {
            lvwAttr.View = View.Details; 
            lvwAttr.Columns.Add("字段",50,HorizontalAlignment.Left );
            lvwAttr.Columns.Add ("值",50,HorizontalAlignment.Center );

}

//

private void tvFeat_NodeMouseClick(object sender, System.Windows.Forms.TreeNodeMouseClickEventArgs e)
        {
            string ID;
            ListViewItem Item;

//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 (short fld = 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();
            }     
        }

public void CurRecordSet(MapObjects2.Recordset vNewValue)
        {
            mrs = vNewValue;
        }
        public void IniLvwAttr(MapObjects2.Recordset rst)
        {
            TreeNode n=null ;
            ListViewItem Item;
            string ID;

lvwAttr.Items.Clear();
            if (tvFeat.Nodes.Count >= 2)
            {
                int i = 0;
                foreach (TreeNode tn in tvFeat.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 (short fld = 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;
                        }
                    }
                }
            }

}
        //将选择的记录集加入到树型控件中进行显示
        public void IniTvFeat(MapObjects2.Recordset moRs_in, string lyrName_in)
        {
            TreeNode n;            
            string ID;
            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////////////////////////////////////////////////////////////////////////////
    }
}

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

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

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

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace IRND_DPT
{
    class OpenFile
    {   
        //功能说明:
        //    从公共对话框中提取文件名,然后调用不同的子模块处理不同的文件加入图层的问题(支持打开多个文件)
        public void  OpenShapeFiles(OpenFileDialog comopenfile,AxMapObjects2.AxMap map)
        {
            try
            {
                string strShape, strCov, strCAD, strVPF, strImage, strMilImage;

//将过滤器设置为可支持的文件
                strShape = "ESRI ShapeFiles(*.shp)|*.shp";
                strCov = "ESRI Coverages(*.adf,*.tat,*.pat,*.rat)|aat.adf;pat.adf;nat.adf;txt.adf;*.tat;*.pat;*.rat";
                strCAD = "AutoCAD File (*.dwg,*.dxf)|*.dwg;*.dxf";
                strImage ="All Image(*.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 (string strFilename in comopenfile.FileNames)
                    {
                        string sPath = strFilename.Substring(0, strFilename.LastIndexOf("\\") + 1);
                        string sFile = strFilename.Substring(strFilename.LastIndexOf("\\") + 1, strFilename.Length - strFilename.LastIndexOf("\\") - 1);
                        string nametype = sFile.Substring(sFile.LastIndexOf(".") + 1, sFile.Length - sFile.LastIndexOf(".") - 1);
                        open_file(sPath, sFile, nametype, map);
                    }
                }
            }
            catch
            {
                MessageBox.Show("图层添加有错!");
            }         
        }
        //功能说明:
        //    测试文件类型,一便调用不同的子模块.处理不同图层加入的问题
        private void open_file(string Path,string filename,string filetype,AxMapObjects2.AxMap mapobject)
        {
            string LayerName = filename.Substring(0, filename.LastIndexOf("."));
            switch (filetype)
            {
                case "shp":
                    shpAdd(Path, filename, mapobject, LayerName, true);
                    break;

}
        }
        //功能说明:
        //添加shape格式的文件
        public void   shpAdd(string databasepath, string filename,AxMapObjects2.AxMap mapobject, string LayerName, bool Hide)
        { 
            MapObjects2.DataConnection dCon=new MapObjects2.DataConnection() ;
           // MapObjects2.GeoDataset gSet=new MapObjects2.GeoDataset ();
            MapObjects2.MapLayer newLayer = new MapObjects2.MapLayer();     
            long i=0;

try
            {
                dCon.Database = databasepath;
                if (dCon.Connect())
                {
                    MapObjects2.GeoDataset gSet = dCon.FindGeoDataset(filename);
                    if (gSet == null)
                    {

return ;
                    }
                    else
                    { 
                        //查看当前是否有重复的图层名,以确保所有的图层名都是唯一的
                        if (FindMapLayerName(mapobject,LayerName)==true )
                        {
                            string tempstr = 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)
                        { 
                            case MapObjects2.ShapeTypeConstants.moShapeTypePoint :
                                newLayer.Symbol.Color=11513775;
                                newLayer.Symbol.Size=5;
                                break;
                            case MapObjects2.ShapeTypeConstants .moShapeTypeLine :
                                newLayer.Symbol.Color = 11513775;
                                newLayer.Symbol.Size = 1;
                                break;
                            case MapObjects2.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.AxMap mapobject,string LayerName 
        public bool FindMapLayerName(AxMapObjects2.AxMap mapobject, string LayerName)
        {

for (int i=0;i<mapobject.Layers.Count ;i++)
            {
                if (mapobject.Layers.Item(i).ToString() ==LayerName )
                {
                    return true;

}
            }     
           return false ;

}
    }
}

(0)

相关推荐

  • C#实现自定义定时组件的方法

    本文实例展示的是一个自定义的定时器组件,有别于.NET Framework里面提供的几个Timer.首先说说该组件开发背景,发现现在手头上的工作离不开定时,定时做一个任务,什么都是定时去做什么什么,隔某段时间干某件事情,都离不开"定时"这个词.众所周知这个要用到多线程,在多篇关于多线程的文章里面有提过做一些周期性的操作时最好用Timer,当然这个Timer肯定是Threading.Timer,并不是WinForm控件里面的Timer.但我觉得在我的应用中Timer不够满足需求. 1.T

  • 深入探讨C#中的const、readonly关键字

    首先不可否认,这些在面试上会经常被面试官问起,但是你回答的让面试官满意吗?当然如果你知道了这些原理,或许你就不 怕了.既然说到了原理,我们还是从MSDN说起. 一:值得推敲的几个地方 1.先来看看msdn上面对const是怎么说的,我们会看到.不能修改,编译时常量这些关键性信息. Q:  const为什么不能被修改. A:这个很简单,很多教科书上面都说,当编译器编译时,会将常量的值保存在该程序集的元数据中,下面我们做个实例 看一看. ①:新建一个projectA. 复制代码 代码如下: // P

  • c#使用windows服务更新站点地图的详细示例

    由于公司平台访问人数逐渐增多,公司项目的数据库已经几次出现宕机现象.为减轻数据库压力,我上个月对公司项目做了下调整.把新闻板块提取出来单独一个站点,单独一个数据库.减少了主站点和数据库的负担和压力. 但放在线上一个月,新闻新的发布数量最少已经有500篇左右.百度只收录了70个左右,于是想到可能是没有站点地图造成的.但怎么定时更新站点地图呢? 我尝试使用windows服务来定时更新站点地图. 首先需要了解下几个问题. 1.百度收录的站点地图(sitemap)的格式.详情请查看该链接:查看 目前我只

  • C# 打开电子邮件软件的具体方法

    使用客户端打开指定的URL使用Process.Start方法可以在浏览器打开指定的URL.代码如下所示. [C#]//使用客户端打开"http://www.jb51.net" System.Diagnostics.Process.Start(www.jb51.net);打开电子邮件软件使用Process.Start方法还可以打开电子邮件软件(Mail). 下面的代码是打开Mail向邮箱地址lxc880615@163.com"送信的例子. [C#]//打开邮件客户端设定&quo

  • C# 定时器定时更新的简单实例

    如下所示: 复制代码 代码如下: class Program     { static void Main(string[] args)         {             //for (int i = 0; i < 100; i++)             //{ //    SendMessage("13161626306", "13161626306");             //}             System.Timers.Ti

  • 下载软件后使用c#获取文件的md5码示例

    很多朋友在下载文件的时候,经常会发现网站提供了MD5校验码,其实这个MD5码的作用就是当你下载文件好了之后,拿你下载好的文件的MD5校验码,跟下载网站提供的进行比较,如果完全一致,说明你下载中文件没问题,如果校验码不一致,说明下载过程中你的文件出错了,或者是你的文件下载出错了,反正就是跟原始文件不一致.而且只要是文件不一样,MD5码肯定不一样,这个是不会重复的,那么到底如何获取文件的MD5码呢?下面就使用C#代码进行讲解. 复制代码 代码如下: //-----------------------

  • 采用C#实现软件自动更新的方法

    本文实例分析了采用C#实现软件自动更新的方法,是非常实用的功能,值得学习和借鉴.具体如下: 1.问题概述: 长期以来,广大程序员为到底是使用Client/Server,还是使用Browser/Server结构争论不休,在这些争论当中,C/S结构的程序可 维护性差,布置困难,升级不方便,维护成本高就是一个相当重要的因素.有很多企业用户就是因为这个原因而放弃使用C/S.然而当一个应用必须要使用C/S 结构才能很好的实现其功能的时候,我们该如何解决客户端的部署与自动升级问题?部署很简单,只要点击安装程

  • C#网络编程基础之进程和线程详解

    在C#的网络编程中,进程和线程是必备的基础知识,同时也是一个重点,所以我们要好好的掌握一下. 一:概念 首先我们要知道什么是"进程",什么是"线程",好,查一下baike. 进程:是一个具有一定独立功能的程序关于某个数据集合的一次活动.它是操作系统动态执行的基本单元, 在传统的操作系统中,进程既是基本的分配单元,也是基本的执行单元. 线程:是"进程"中某个单一顺序的控制流. 关于这两个概念,大家稍微有个印象就行了,防止以后被面试官问到. 二:进程

  • C#自动设置IE代理服务器(翻墙软件)代码实现

    C#自动设置IE代理服务器代码如下: 复制代码 代码如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using Microsoft.Win32; using System.Diagnostics; namesp

  • c# 调用Surfer软件,添加引用的具体操作方法

    c#调用surfer软件,添加应用的方法: 1.在项目文件上右键->添加引用 2.选择COM标签页 3.找到Surfer 9 type library 4.添加 如图所示:

  • C#软件注册码的实现代码

    第一步.根据卷标,CPU序列号,生成机器码 复制代码 代码如下: // 取得设备硬盘的卷标号        public static string GetDiskVolumeSerialNumber()        {            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");            ManagementObject disk = new Mana

随机推荐