📄 form1.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using ESRI.MapObjects2.Core;
namespace LinePrj
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private ESRI.MapObjects2.Core.AxMap axMap1;
private System.ComponentModel.IContainer components;
private ESRI.MapObjects2.Core.MapLayer m_pCountiesLyr;
private ESRI.MapObjects2.Core.MapLayer m_pCitiesLyr;
private System.Windows.Forms.SaveFileDialog dlgSave;
private ESRI.MapObjects2.Core.MapLayer layer;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private String m_dataPath = "D:/Program Files/ESRI/MapObjects2/Samples/Data/";
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
layer = new ESRI.MapObjects2.Core.MapLayerClass();
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.button1 = new System.Windows.Forms.Button();
this.axMap1 = new ESRI.MapObjects2.Core.AxMap();
this.dlgSave = new System.Windows.Forms.SaveFileDialog();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.axMap1)).BeginInit();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(472, 40);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "线条";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// axMap1
//
this.axMap1.Location = new System.Drawing.Point(16, 11);
this.axMap1.Name = "axMap1";
this.axMap1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axMap1.OcxState")));
this.axMap1.Size = new System.Drawing.Size(408, 208);
this.axMap1.TabIndex = 2;
this.axMap1.MouseDownEvent += new ESRI.MapObjects2.Core.MouseDownEventHandler(this.axMap1_MouseDownEvent);
this.axMap1.MouseMoveEvent += new ESRI.MapObjects2.Core.MouseMoveEventHandler(this.axMap1_MouseMoveEvent);
//
// label1
//
this.label1.Location = new System.Drawing.Point(32, 232);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(240, 32);
this.label1.TabIndex = 3;
this.label1.Text = "label1";
//
// label2
//
this.label2.Location = new System.Drawing.Point(352, 226);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(192, 40);
this.label2.TabIndex = 4;
this.label2.Text = "label2";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(568, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.label2,
this.label1,
this.axMap1,
this.button1});
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.axMap1)).EndInit();
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
if (CreateLineMapLayer())
SaveData();
this.axMap1.Layers.Add(layer);
layer.Symbol.Color = Convert.ToUInt32(ESRI.MapObjects2.Core.ColorConstants.moRed);
//this.axMap1.TrackingLayer.Refresh(true, null);
this.axMap1.TrackingLayer.get_Symbol(0).Color = Convert.ToUInt32(ESRI.MapObjects2.Core.ColorConstants.moRed);
//this.axMap1.TrackingLayer.Refresh(true, this.axMap1.TrackingLayer);
this.axMap1.CtlRefresh();
}
private void Form1_Load(object sender, System.EventArgs e)
{
this.axMap1.ScrollBars = true;
InitializeMapData();
InitializeTrackingLayer();
}
private void InitializeMapData()
{
try
{
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
ESRI.MapObjects2.Core.DataConnection dataConn = new ESRI.MapObjects2.Core.DataConnectionClass();
string dataPath = ReturnDataPath("Northeast");
dataConn.Database = dataPath;
dataConn.Connect();
if (dataConn.Connected)
{
this.m_pCountiesLyr = new ESRI.MapObjects2.Core.MapLayerClass();
ESRI.MapObjects2.Core.GeoDataset gds = dataConn.FindGeoDataset("Counties");
if (gds != null)
{
this.m_pCountiesLyr.GeoDataset = gds;
this.m_pCountiesLyr.Symbol.Color = Convert.ToUInt32(ESRI.MapObjects2.Core.ColorConstants.moPaleYellow);
this.axMap1.Layers.Add(this.m_pCountiesLyr);
}
this.m_pCitiesLyr = new ESRI.MapObjects2.Core.MapLayerClass();
gds = dataConn.FindGeoDataset("Necenter");
if (gds != null)
{
this.m_pCitiesLyr.GeoDataset = gds;
this.m_pCitiesLyr.Symbol.Color = Convert.ToUInt32(ESRI.MapObjects2.Core.ColorConstants.moBlue);
this.axMap1.Layers.Add(this.m_pCitiesLyr);
}
}
if (this.axMap1.Layers.Count < 2)
{
MessageBox.Show("加载地图文件出错","错误");
}
}
catch (System.Exception e)
{
MessageBox.Show(e.Message);
}
finally
{
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
}
}
private string ReturnDataPath(string dataPath)
{
/*string basePath = Application.ExecutablePath;
int index = basePath.LastIndexOf("Samples");
if (index > -1)
{
return basePath.Substring(0, index + 8) + "Data\\" + dataPath;
}
else
return null;*/
return this.m_dataPath + dataPath;
}
private void InitializeTrackingLayer()
{
stdole.StdFont fnt = new stdole.StdFontClass();
fnt.Name = "Wingdings";
fnt.Bold = false;
this.axMap1.TrackingLayer.SymbolCount = 2;
this.axMap1.TrackingLayer.get_Symbol(0).Color = (uint)ESRI.MapObjects2.Core.ColorConstants.moBlue;
this.axMap1.TrackingLayer.get_Symbol(0).Style = (short)ESRI.MapObjects2.Core.MarkerStyleConstants.moTrueTypeMarker;
this.axMap1.TrackingLayer.get_Symbol(0).Font = fnt;
this.axMap1.TrackingLayer.get_Symbol(0).Size = 16;
this.axMap1.TrackingLayer.get_Symbol(0).CharacterIndex = 88;
this.axMap1.TrackingLayer.get_Symbol(1).Color = (uint)ESRI.MapObjects2.Core.ColorConstants.moPurple;
this.axMap1.TrackingLayer.get_Symbol(1).Style = (short)ESRI.MapObjects2.Core.MarkerStyleConstants.moTrueTypeMarker;
this.axMap1.TrackingLayer.get_Symbol(1).Font = fnt;
this.axMap1.TrackingLayer.get_Symbol(1).Size = 16;
this.axMap1.TrackingLayer.get_Symbol(1).CharacterIndex = 88;
}
private bool CreateLineMapLayer()
{
bool result = false;
try
{
// Exports the layer (along with it's PRJ file) to the currently selected
// coordinate system.
this.dlgSave.Filter = "ESRI Shapefiles (*.shp)|*.shp";
this.dlgSave.Title = "Save projected layer as...";
if (dlgSave.ShowDialog(this) == DialogResult.OK)
{
if (dlgSave.FileName != null)
{
// Export the layer's records to the selected Coordinatesystem.
this.Cursor = System.Windows.Forms.Cursors.WaitCursor;
ESRI.MapObjects2.Core.DataConnection dc ;
dc = new ESRI.MapObjects2.Core.DataConnectionClass();
ESRI.MapObjects2.Core.GeoDataset gds;//= new ESRI.MapObjects2.Core.GeoDatasetClass();
//MessageBox.Show(dlgSave.FileName);
string path = dlgSave.FileName.Substring(0, dlgSave.FileName.Length - 7);
//MessageBox.Show(path);
dc.Database = path;
dc.Connect();
if (!dc.Connected)
{
MessageBox.Show("请指定已经有的文件");
}
else
{
ESRI.MapObjects2.Core.TableDesc tabDesc = new ESRI.MapObjects2.Core.TableDescClass();
tabDesc.FieldCount = 2;
//tabDesc.set_FieldLength(0,10);
tabDesc.set_FieldName(0, "X");
tabDesc.set_FieldType(0, ESRI.MapObjects2.Core.FieldTypeConstants.moDouble);
tabDesc.set_FieldPrecision(0, 10);
tabDesc.set_FieldScale(0, 3);
//tabDesc.set_FieldLength(1, 10);
tabDesc.set_FieldName(1, "Y");
tabDesc.set_FieldType(1, ESRI.MapObjects2.Core.FieldTypeConstants.moDouble);
tabDesc.set_FieldPrecision(1, 10);
tabDesc.set_FieldScale(1, 3);
String fileName = "xia";
gds = dc.AddGeoDataset(fileName, ESRI.MapObjects2.Core.ShapeTypeConstants.moShapeTypeLine,
tabDesc, null,null);
if (gds != null)
MessageBox.Show(gds.ToString());
layer.GeoDataset = gds;
dc.Disconnect();
result = true;
}
}
}
}
catch (System.Runtime.InteropServices.ExternalException COMEx)
{
MessageBox.Show(COMEx.ErrorCode + ": " + COMEx.Message);
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
this.Cursor = System.Windows.Forms.Cursors.Default;
}
return result;
}
private void SaveData()
{
try
{
ESRI.MapObjects2.Core.Field xField ;
//xField = new ESRI.MapObjects2.Core.FieldClass();
ESRI.MapObjects2.Core.Field yField;// = new ESRI.MapObjects2.Core.FieldClass();
ESRI.MapObjects2.Core.Field shapeField;// = new ESRI.MapObjects2.Core.FieldClass();
ESRI.MapObjects2.Core.Fields fields;// = new ESRI.MapObjects2.Core.FieldsClass();
ESRI.MapObjects2.Core.Recordset rs = layer.Records;
rs.AutoFlush = false;
fields = rs.Fields;
shapeField = fields.Item("Shape");
xField = fields.Item("X");
yField = fields.Item("Y");
ESRI.MapObjects2.Core.Line line = new ESRI.MapObjects2.Core.LineClass();
ESRI.MapObjects2.Core.Point pt = new ESRI.MapObjects2.Core.PointClass();
ESRI.MapObjects2.Core.Points pts = new ESRI.MapObjects2.Core.PointsClass();
//pt.X = this.axMap1.Extent.Left;
//pt.Y = (this.axMap1.Extent.Top - this.axMap1.Extent.Height) / 2;
pt.X = 1181604.156d;
pt.Y = 2298957.796d;
pts.Add(pt);
//pts = new ESRI.MapObjects2.Core.PointsClass();
//pt.X = this.axMap1.Extent.Left + this.axMap1.Extent.Width / 400;
//pt.Y = (this.axMap1.Extent.Top - this.axMap1.Extent.Height) / 20;
pt.X = 2394976.10558d;
pt.Y = 2574260.6747d;
pts.Add(pt);
pt.X = 2078887.6153d;
pt.Y = 2089931.5363d;
pts.Add(pt);
line.Parts.Add(pts);
rs.AddNew();
shapeField.Value = line;
xField.Value = pt.X;
yField.Value = pt.Y;
rs.Update();
/*pts = new ESRI.MapObjects2.Core.PointsClass();
pt = this.axMap1.Extent.Center;
pts.Add(pt);
line.Parts.Add(pts);
rs.AddNew();
shapeField.Value = line;
xField.Value = pt.X;
yField.Value = pt.Y;
rs.Update();
pts = new ESRI.MapObjects2.Core.PointsClass();
pt.X = this.axMap1.Extent.Center.X + this.axMap1.Extent.Width / 40;
pt.Y = (this.axMap1.Extent.Top - this.axMap1.Extent.Height / 4)/20;
pts.Add(pt);
line.Parts.Add(pts);
rs.AddNew();
shapeField.Value = line;
xField.Value = pt.X;
yField.Value = pt.Y;
rs.Update();
pts = new ESRI.MapObjects2.Core.PointsClass();
pt.X = this.axMap1.Extent.Right ;
pt.Y = this.axMap1.Extent.Height ;
pts.Add(pt);
line.Parts.Add(pts);
rs.AddNew();
shapeField.Value = line;
xField.Value = pt.X;
yField.Value = pt.Y;
rs.Update();*/
rs.AutoFlush = true;
}
catch (System.Runtime.InteropServices.COMException com)
{
MessageBox.Show("保存数据出错" + com.Message);
return;
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message );
return;
}
}
private void axMap1_MouseDownEvent(object sender, ESRI.MapObjects2.Core.MouseDownEventArgs e)
{
}
private void axMap1_MouseMoveEvent(object sender, ESRI.MapObjects2.Core.MouseMoveEventArgs e)
{
ESRI.MapObjects2.Core.Point pt = this.axMap1.ToMapPoint(e.x, e.y);
this.label1.Text = pt.X + " " + pt.Y;
this.label2.Text = e.x + " " + e.y;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -