C#+MO implementation of a road editing software (just started) _c# tutorial
Last Update:2017-01-18
Source: Internet
Author: User
//**********************************************************
Main Window Program
//********************************************************
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 ();
}
Add Layers
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);
}
Remove Layers
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);
}
Amplification
private void Tlb_zoomin_click (object sender, EventArgs e)
{
Axmap1.mousepointer = MapObjects2.MousePointerConstants.moZoomIn;
}
Narrow
private void Tlb_zoomout_click (object sender, EventArgs e)
{
Axmap1.mousepointer = MapObjects2.MousePointerConstants.moZoomOut;
}
Roaming
private void Tlb_pan_click (object sender, EventArgs e)
{
Axmap1.mousepointer = MapObjects2.MousePointerConstants.moPan;
}
Full picture
private void Tbl_full_click (object sender, EventArgs e)
{
Axmap1.extent = axmap1.fullextent;
Axmap1.mousepointer = MapObjects2.MousePointerConstants.moArrow;
}
Gradually enlarge
private void Tbl_smallin_click (object sender, EventArgs e)
{
Mapobjects2.rectangle r = axmap1.extent;
R.scalerectangle (0.9);
Axmap1.extent = R;
}
Gradually shrinking
private void Tbl_smallout_click (object sender, EventArgs e)
{
Mapobjects2.rectangle r = axmap1.extent;
R.scalerectangle (1.1);
Axmap1.extent = R;
}
Select query
private void Tbl_identify_click (object sender, EventArgs e)
{
Axmap1.mousepointer = MapObjects2.MousePointerConstants.moIdentify;
}
Property browsing
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);
}
}
Map Response Events
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)
{
Amplification
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
}
Narrow
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
}
Roaming
Case MapObjects2.MousePointerConstants.moPan:
{
Axmap1.pan ();
Break
}
Select query
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
}
}
}
////////
///////////////////////////////////////////////////////////////////////////////////////////////
}
}
//*************************************************
Property Query
//************************************************
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 ("field", 50,horizontalalignment.left);
LVWATTR.COLUMNS.ADD ("value", 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
}
}
}
}
}
Add the selected recordset to the tree control for display
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////////////////////////////////////////////////////////////////////////////
}
}
//*************************************************************
Load Layer Functions
//*************************************************************
Using System;
Using System.Collections.Generic;
Using System.Text;
Using System.Windows.Forms;
Namespace IRND_DPT
{
Class OpenFile
{
Function Description:
Extract the file name from the Public dialog box, and then call a different child module to handle the problem of adding layers to different files (support for opening multiple files)
public void Openshapefiles (OpenFileDialog comopenfile,axmapobjects2.axmap map)
{
Try
{
String Strshape, Strcov, Strcad, STRVPF, Strimage, strmilimage;
To set the filter to a file that can be supported
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;
Set Filter
Comopenfile. Filter = Strshape + "|" + Strcov + "|" + Strcad + "|" + strimage;
Comopenfile. Title = "Add layer";
Allow multiple files to be selected and allow long file names
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 ("Layer add error!");
}
}
Function Description:
Test the file type and call a different child module at a single point. Dealing with different layer joins
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
}
}
Function Description:
Add a file in shape format
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
{
See if there is currently a duplicate layer name to make sure all the layer names are unique
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;
}
Use default colors and styles to set layers
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 ("Load layer Error!");
}
}
Function Description:
Find a layer in a 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;
}
}
}