C#中调用mapx5.0

发表于:2007-05-25来源:作者:点击数: 标签:mapx5.0我在这是.NET环境
这是 我在 .net 环境中调用mapx5.0控件 的一次test,前几次网上没有发现这方面的文章,所以自己 把几个代码拿出 来:这里有些代码是自动生成的,但只要把几篇文章的代码直接拷贝到工程里 就 能直接运行的。 using System.Drawing; using System.Collections;
这是 我在.net环境中调用mapx5.0控件 的一次test,前几次网上没有发现这方面的文章,所以自己
把几个代码拿出 来:这里有些代码是自动生成的,但只要把几篇文章的代码直接拷贝到工程里 就
能直接运行的。


using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;


namespace gistestbymyself
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class FrmMain : System.Windows.Forms.Form
{


#region 私有变量的声明
private AxMapXLib.AxMap axMap1;
private AxMapXLib.AxMap axMap2;


private double zoom;
private double centerx;
private double centery;
#endregion


#region Windows 窗体上拖入窗体的控件
/// <summary>
///
/// 窗体信息。
/// </summary>




private System.Windows.Forms.ToolBar toolBar2;
private System.Windows.Forms.ToolBarButton tbfangda;
private System.Windows.Forms.ToolBarButton tbbianxiao;
private System.Windows.Forms.ToolBarButton tbmanyou;
private System.Windows.Forms.ToolBarButton tbbiaozhu;
private System.Windows.Forms.ToolBarButton tbjuli;
private System.Windows.Forms.ToolBarButton tbshuaxin;
private System.Windows.Forms.ToolBarButton tbxiangxi;
private System.Windows.Forms.ToolBarButton tbzuiduan;
private System.Windows.Forms.ToolBarButton tbmianji;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.ToolBarButton tbxuanze;
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.MenuItem menuItem3;
private System.Windows.Forms.MenuItem mutuceng;
private System.Windows.Forms.ContextMenu comu;
private System.Windows.Forms.MenuItem menuItem4;
private System.Windows.Forms.MenuItem menuItem5;
private System.Windows.Forms.MenuItem menuItem6;
private System.Windows.Forms.MenuItem menuItem7;
private System.Windows.Forms.ContextMenu comu1;
private System.Windows.Forms.MenuItem menuItem8;
private System.Windows.Forms.MenuItem menuItem9;
private System.Windows.Forms.ToolBarButton tbtianjiafuhao;
private System.Windows.Forms.MenuItem menuItem10;
private System.Windows.Forms.MenuItem menuItem11;
private System.Windows.Forms.MenuItem menuItem12;
private System.Windows.Forms.MenuItem menuItem13;
private System.Windows.Forms.MenuItem menuItem14;
private System.Windows.Forms.MenuItem menuItem15;
private System.Windows.Forms.MenuItem menuItem16;
private System.Windows.Forms.MenuItem menuItem17;
private System.Windows.Forms.MenuItem menuItem18;
private System.Windows.Forms.MenuItem menuItem19;
private System.Windows.Forms.MenuItem menuItem20;
private System.Windows.Forms.MenuItem menuItem21;
private System.Windows.Forms.MenuItem menuItem22;
private System.Windows.Forms.MenuItem menuItem23;
private System.Windows.Forms.MenuItem menuItem24;
private System.Windows.Forms.MenuItem menuItem25;
private System.Windows.Forms.MenuItem menuItem26;
private System.Windows.Forms.Splitter splitter1;
private System.Windows.Forms.MenuItem menuItem27;
private System.Windows.Forms.ToolBar toolBar1;
private System.Windows.Forms.ToolBarButton tbadddian;
private System.Windows.Forms.ToolBarButton tbaddxian;
private System.Windows.Forms.ToolBarButton tbaddquxian;
private System.Windows.Forms.ToolBarButton tbaddqumian;
private System.Windows.Forms.ToolBarButton tbshezhi;
private System.Windows.Forms.ToolBarButton tbline;
private System.Windows.Forms.MenuItem menuItem28;
private System.Windows.Forms.MenuItem menuItem29;
private System.Windows.Forms.MenuItem menuItem30;
private System.Windows.Forms.MenuItem menuItem31;
private System.Windows.Forms.MenuItem menuItem32;
private System.Windows.Forms.MenuItem menuItem33;
private System.Windows.Forms.MenuItem menuItem34;
private System.Windows.Forms.MenuItem menuItem35;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;


#endregion


#region FrmMain类的初始化和Form_Load时发生时的动作


public FrmMain()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();


//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
this.zoom=axMap1.Zoom; //取得初始化时的地图比例
this.centerx=axMap1.CenterX;
this.centery=axMap1.CenterY; //取得初始化时的地图坐标,供刷新时使用


axMap1.CreateCustomTool(10,MapXLib.ToolTypeConstants.miToolTypePoly,MapXLib.CursorConstants.miCrossCursor,null,null,false);
//创建距离工具


axMap1.CreateCustomTool(11,MapXLib.ToolTypeConstants.miToolTypePolygon,MapXLib.CursorConstants.miCrossCursor,null,null,false);
//创建面积工具


}
//*********************************
private void Form1_Load(object sender, System.EventArgs e)
{

axMap1.GeoSet=@"D:\....\CHINA.GST";

axMap2.GeoSet= Directory.GetCurrentDirectory()+ @"\....\*.gst";


axMap1.TitleText="中国";
axMap2.TitleText="***";
}


#endregion
//*****************************


#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}



private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(FrmMain));
this.axMap1 = new AxMapXLib.AxMap();
this.axMap2 = new AxMapXLib.AxMap();
this.toolBar2 = new System.Windows.Forms.ToolBar();
this.tbxuanze = new System.Windows.Forms.ToolBarButton();
this.tbfangda = new System.Windows.Forms.ToolBarButton();
this.tbbianxiao = new System.Windows.Forms.ToolBarButton();
this.tbmanyou = new System.Windows.Forms.ToolBarButton();
this.tbbiaozhu = new System.Windows.Forms.ToolBarButton();
this.tbtianjiafuhao = new System.Windows.Forms.ToolBarButton();
this.tbjuli = new System.Windows.Forms.ToolBarButton();
this.tbmianji = new System.Windows.Forms.ToolBarButton();
this.tbshuaxin = new System.Windows.Forms.ToolBarButton();
this.tbxiangxi = new System.Windows.Forms.ToolBarButton();
this.tbzuiduan = new System.Windows.Forms.ToolBarButton();
this.label1 = new System.Windows.Forms.Label();
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem20 = new System.Windows.Forms.MenuItem();
this.menuItem21 = new System.Windows.Forms.MenuItem();
this.menuItem22 = new System.Windows.Forms.MenuItem();
this.menuItem23 = new System.Windows.Forms.MenuItem();
this.menuItem24 = new System.Windows.Forms.MenuItem();
this.menuItem25 = new System.Windows.Forms.MenuItem();
this.menuItem26 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.menuItem3 = new System.Windows.Forms.MenuItem();
this.menuItem29 = new System.Windows.Forms.MenuItem();
this.menuItem28 = new System.Windows.Forms.MenuItem();
this.menuItem27 = new System.Windows.Forms.MenuItem();
this.menuItem15 = new System.Windows.Forms.MenuItem();
this.menuItem32 = new System.Windows.Forms.MenuItem();
this.menuItem31 = new System.Windows.Forms.MenuItem();
this.mutuceng = new System.Windows.Forms.MenuItem();
this.menuItem30 = new System.Windows.Forms.MenuItem();
this.menuItem16 = new System.Windows.Forms.MenuItem();
this.menuItem10 = new System.Windows.Forms.MenuItem();
this.menuItem11 = new System.Windows.Forms.MenuItem();
this.menuItem12 = new System.Windows.Forms.MenuItem();
this.menuItem13 = new System.Windows.Forms.MenuItem();
this.menuItem14 = new System.Windows.Forms.MenuItem();
this.menuItem17 = new System.Windows.Forms.MenuItem();
this.menuItem18 = new System.Windows.Forms.MenuItem();
this.menuItem19 = new System.Windows.Forms.MenuItem();
this.comu = new System.Windows.Forms.ContextMenu();
this.menuItem4 = new System.Windows.Forms.MenuItem();
this.menuItem5 = new System.Windows.Forms.MenuItem();
this.menuItem6 = new System.Windows.Forms.MenuItem();
this.menuItem7 = new System.Windows.Forms.MenuItem();
this.comu1 = new System.Windows.Forms.ContextMenu();
this.menuItem8 = new System.Windows.Forms.MenuItem();
this.menuItem9 = new System.Windows.Forms.MenuItem();
this.splitter1 = new System.Windows.Forms.Splitter();
this.toolBar1 = new System.Windows.Forms.ToolBar();
this.tbadddian = new System.Windows.Forms.ToolBarButton();
this.tbaddxian = new System.Windows.Forms.ToolBarButton();
this.tbaddquxian = new System.Windows.Forms.ToolBarButton();
this.tbaddqumian = new System.Windows.Forms.ToolBarButton();
this.tbline = new System.Windows.Forms.ToolBarButton();
this.tbshezhi = new System.Windows.Forms.ToolBarButton();
this.menuItem33 = new System.Windows.Forms.MenuItem();
this.menuItem34 = new System.Windows.Forms.MenuItem();
this.menuItem35 = new System.Windows.Forms.MenuItem();
((System.ComponentModel.ISupportInitialize)(this.axMap1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.axMap2)).BeginInit();
this.SuspendLayout();
//
// axMap1
//
this.axMap1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.axMap1.Enabled = true;
this.axMap1.Location = new System.Drawing.Point(8, 120);
this.axMap1.Name = "axMap1";
this.axMap1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axMap1.OcxState")));
this.axMap1.Size = new System.Drawing.Size(424, 339);
this.axMap1.TabIndex = 0;
this.axMap1.PolyToolUsed += new AxMapXLib.CMapXEvents_PolyToolUsedEventHandler(this.mapMain_PolyToolUsed);
this.axMap1.MouseUpEvent += new AxMapXLib.CMapXEvents_MouseUpEventHandler(this.axMap1_MouseUpEvent);
this.axMap1.ThemeModifyRequested += new AxMapXLib.CMapXEvents_ThemeModifyRequestedEventHandler(this.axMap1_ThemeModifyRequested);
this.axMap1.MouseDownEvent += new AxMapXLib.CMapXEvents_MouseDownEventHandler(this.axMap1_MouseDownEvent);
this.axMap1.MapViewChanged += new System.EventHandler(this.axMap1_MapViewChanged);
//
// axMap2
//
this.axMap2.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.axMap2.Enabled = true;
this.axMap2.Location = new System.Drawing.Point(472, 136);
this.axMap2.Name = "axMap2";
this.axMap2.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axMap2.OcxState")));
this.axMap2.Size = new System.Drawing.Size(240, 203);
this.axMap2.TabIndex = 1;
//
// toolBar2
//
this.toolBar2.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
this.tbxuanze,
this.tbfangda,
this.tbbianxiao,
this.tbmanyou,
this.tbbiaozhu,
this.tbtianjiafuhao,
this.tbjuli,
this.tbmianji,
this.tbshuaxin,
this.tbxiangxi,
this.tbzuiduan});
this.toolBar2.ButtonSize = new System.Drawing.Size(55, 35);
this.toolBar2.DropDownArrows = true;
this.toolBar2.Location = new System.Drawing.Point(0, 0);
this.toolBar2.Name = "toolBar2";
this.toolBar2.ShowToolTips = true;
this.toolBar2.Size = new System.Drawing.Size(776, 41);
this.toolBar2.TabIndex = 3;
this.toolBar2.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar2_ButtonClick);
//
// tbxuanze
//
this.tbxuanze.Text = "选择";
//
// tbfangda
//
this.tbfangda.Text = "放大";
this.tbfangda.ToolTipText = "放大";
//
// tbbianxiao
//
this.tbbianxiao.Text = "缩小";
//
// tbmanyou
//
this.tbmanyou.Text = "漫游";
//
// tbbiaozhu
//
this.tbbiaozhu.Text = "标注";
//
// tbtianjiafuhao
//
this.tbtianjiafuhao.Text = "添加符号";
//
// tbjuli
//
this.tbjuli.Text = "测距";
//
// tbmianji
//
this.tbmianji.Text = "面积";
//
// tbshuaxin
//
this.tbshuaxin.Text = "刷新";
//
// tbxiangxi
//
this.tbxiangxi.Text = "详细";
//
// tbzuiduan
//
this.tbzuiduan.Text = "最短实体";
//
// label1
//
this.label1.Location = new System.Drawing.Point(48, 104);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(208, 16);
this.label1.TabIndex = 4;
this.label1.Text = "label1";
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1,
this.menuItem3,
this.menuItem10,
this.menuItem17,
this.menuItem18});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem20,
this.menuItem21,
this.menuItem22,
this.menuItem23,
this.menuItem24,
this.menuItem25,
this.menuItem26,
this.menuItem33,
this.menuItem35,
this.menuItem34,
this.menuItem2});
this.menuItem1.Text = "文件";
//
// menuItem20
//
this.menuItem20.Index = 0;
this.menuItem20.Text = "打开地图...";
this.menuItem20.Click += new System.EventHandler(this.menuItem20_Click);
//
// menuItem21
//
this.menuItem21.Index = 1;
this.menuItem21.Text = "添加图层...";
this.menuItem21.Click += new System.EventHandler(this.menuItem21_Click);
//
// menuItem22
//
this.menuItem22.Index = 2;
this.menuItem22.Text = "-";
//
// menuItem23
//
this.menuItem23.Index = 3;
this.menuItem23.Text = "保存";
this.menuItem23.Click += new System.EventHandler(this.menuItem23_Click);
//
// menuItem24
//
this.menuItem24.Index = 4;
this.menuItem24.Text = "另存为...";
this.menuItem24.Click += new System.EventHandler(this.menuItem24_Click);
//
// menuItem25
//
this.menuItem25.Index = 5;
this.menuItem25.Text = "导出..";
this.menuItem25.Click += new System.EventHandler(this.menuItem25_Click);
//
// menuItem26
//
this.menuItem26.Index = 6;
this.menuItem26.Text = "-";
//
// menuItem2
//
this.menuItem2.Index = 10;
this.menuItem2.Text = "退出";
//
// menuItem3
//
this.menuItem3.Index = 1;
this.menuItem3.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem29,
this.menuItem28,
this.menuItem27,
this.menuItem15,
this.menuItem32,
this.menuItem31,
this.mutuceng,
this.menuItem30,
this.menuItem16});
this.menuItem3.Text = "地图";
//
// menuItem29
//
this.menuItem29.Index = 0;
this.menuItem29.Text = "坐标变换(投影)";
this.menuItem29.Click += new System.EventHandler(this.menuItem29_Click);
//
// menuItem28
//
this.menuItem28.Index = 1;
this.menuItem28.Text = "-";
//
// menuItem27
//
this.menuItem27.Index = 2;
this.menuItem27.Text = "编辑";
this.menuItem27.Click += new System.EventHandler(this.menuItem27_Click);
//
// menuItem15
//
this.menuItem15.Index = 3;
this.menuItem15.Text = "-";
//
// menuItem32
//
this.menuItem32.Index = 4;
this.menuItem32.Text = "缩放图层";
this.menuItem32.Click += new System.EventHandler(this.menuItem32_Click);
//
// menuItem31
//
this.menuItem31.Index = 5;
this.menuItem31.Text = "选择图层";
this.menuItem31.Click += new System.EventHandler(this.menuItem31_Click);
//
// mutuceng
//
this.mutuceng.Index = 6;
this.mutuceng.Text = "图层控制";
this.mutuceng.Click += new System.EventHandler(this.mutuceng_Click);
//
// menuItem30
//
this.menuItem30.Index = 7;
this.menuItem30.Text = "-";
//
// menuItem16
//
this.menuItem16.Index = 8;
this.menuItem16.Text = "属性";
this.menuItem16.Click += new System.EventHandler(this.menuItem16_Click);
//
// menuItem10
//
this.menuItem10.Index = 2;
this.menuItem10.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem11,
this.menuItem12,
this.menuItem13,
this.menuItem14});
this.menuItem10.Text = "注释";
//
// menuItem11
//
this.menuItem11.Index = 0;
this.menuItem11.Text = "添加符号";
this.menuItem11.Click += new System.EventHandler(this.menuItem11_Click);
//
// menuItem12
//
this.menuItem12.Index = 1;
this.menuItem12.Text = "添加文本";
this.menuItem12.Click += new System.EventHandler(this.menuItem12_Click);
//
// menuItem13
//
this.menuItem13.Index = 2;
this.menuItem13.Text = "移除";
this.menuItem13.Click += new System.EventHandler(this.menuItem13_Click);
//
// menuItem14
//
this.menuItem14.Index = 3;
this.menuItem14.Text = "设置";
this.menuItem14.Click += new System.EventHandler(this.menuItem14_Click);
//
// menuItem17
//
this.menuItem17.Index = 3;
this.menuItem17.Text = "窗口";
//
// menuItem18
//
this.menuItem18.Index = 4;
this.menuItem18.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem19});
this.menuItem18.Text = "帮助";
//
// menuItem19
//
this.menuItem19.Index = 0;
this.menuItem19.Text = "关于MapX";
this.menuItem19.Click += new System.EventHandler(this.menuItem19_Click);
//
// comu
//
this.comu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem4,
this.menuItem5,
this.menuItem6,
this.menuItem7});
//
// menuItem4
//
this.menuItem4.Index = 0;
this.menuItem4.Text = "普通";
this.menuItem4.Click += new System.EventHandler(this.menuItem4_Click);
//
// menuItem5
//
this.menuItem5.Index = 1;
this.menuItem5.Text = "矩形";
this.menuItem5.Click += new System.EventHandler(this.menuItem5_Click);
//
// menuItem6
//
this.menuItem6.Index = 2;
this.menuItem6.Text = "圆形";
this.menuItem6.Click += new System.EventHandler(this.menuItem6_Click);
//
// menuItem7
//
this.menuItem7.Index = 3;
this.menuItem7.Text = "多边形";
this.menuItem7.Click += new System.EventHandler(this.menuItem7_Click);
//
// comu1
//
this.comu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem8,
this.menuItem9});
//
// menuItem8
//
this.menuItem8.Index = 0;
this.menuItem8.Text = "添加符号";
this.menuItem8.Click += new System.EventHandler(this.menuItem8_Click);
//
// menuItem9
//
this.menuItem9.Index = 1;
this.menuItem9.Text = "添加";
this.menuItem9.Click += new System.EventHandler(this.menuItem9_Click);
//
// splitter1
//
this.splitter1.Location = new System.Drawing.Point(0, 41);
this.splitter1.Name = "splitter1";
this.splitter1.Size = new System.Drawing.Size(3, 432);
this.splitter1.TabIndex = 5;
this.splitter1.TabStop = false;
//
// toolBar1
//
this.toolBar1.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
this.tbadddian,
this.tbaddxian,
this.tbaddquxian,
this.tbaddqumian,
this.tbline,
this.tbshezhi});
this.toolBar1.Dock = System.Windows.Forms.DockStyle.None;
this.toolBar1.DropDownArrows = true;
this.toolBar1.Location = new System.Drawing.Point(0, 40);
this.toolBar1.Name = "toolBar1";
this.toolBar1.ShowToolTips = true;
this.toolBar1.Size = new System.Drawing.Size(488, 28);
this.toolBar1.TabIndex = 6;
this.toolBar1.TextAlign = System.Windows.Forms.ToolBarTextAlign.Right;
this.toolBar1.Visible = false;
this.toolBar1.Wrappable = false;
this.toolBar1.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar1_ButtonClick);
//
// tbadddian
//
this.tbadddian.Text = "点 ";
this.tbadddian.Visible = false;
//
// tbaddxian
//
this.tbaddxian.Text = "线 ";
this.tbaddxian.Visible = false;
//
// tbaddquxian
//
this.tbaddquxian.Text = "曲线 ";
this.tbaddquxian.Visible = false;
//
// tbaddqumian
//
this.tbaddqumian.Text = "面 ";
this.tbaddqumian.Visible = false;
//
// tbline
//
this.tbline.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
//
// tbshezhi
//
this.tbshezhi.Text = "设置 ";
this.tbshezhi.Visible = false;
//
// menuItem33
//
this.menuItem33.Index = 7;
this.menuItem33.Text = "打印设置";
//
// menuItem34
//
this.menuItem34.Index = 9;
this.menuItem34.Text = "-";
//
// menuItem35
//
this.menuItem35.Index = 8;
this.menuItem35.Text = "打印...";
//
// FrmMain
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(776, 473);
this.Controls.Add(this.toolBar1);
this.Controls.Add(this.splitter1);
this.Controls.Add(this.label1);
this.Controls.Add(this.toolBar2);
this.Controls.Add(this.axMap2);
this.Controls.Add(this.axMap1);
this.Menu = this.mainMenu1;
this.Name = "FrmMain";
this.Text = "FrmMain";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.axMap1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.axMap2)).EndInit();
this.ResumeLayout(false);


}
#endregion


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


//****************************************
#region axMap1对象的属性操作,一般都在其他窗口里被使用
/// <summary>
/// 在这里适用属性主要是为了axMap1这个私有的对象能在其他窗体中获得一些需要的改变
/// </summary>
///
//属性1
public MapXLib.Style SyStyle //属性操作,设置或取得符号的样式,在 Frmannotation 中使用了该属性,
{
set
{
axMap1.DefaultStyle=value;
}
get
{
return axMap1.DefaultStyle;
}
}
//属性2
public MapXLib.Style editStyle //属性操作,设置或取得在编辑图层时,改变的点,线和曲面的样式 在Frmeditable
{
set //中使用了该属性,
{
axMap1.AutoRedraw=true;
axMap1.Layers.InsertionLayer.Style=value;
}
get
{
axMap1.Layers.InsertionLayer.OverrideStyle=true;
axMap1.AutoRedraw=false;
return axMap1.Layers.InsertionLayer.Style;
}
}
//属性3
public MapXLib.Layers Layer //属性操作,通过该属性,图层选择显示窗口(FrmViewLyr)可以操作axMap1的Layers属性,
{ //在FrmViewLyr_Load 时可以得到 Layers
get
{
return axMap1.Layers;
}


}
//属性4
public string LyrName //属性操作,通过该属性,得到图层选择显示窗口(FrmViewLyr)中cobLyr.text中的值
{ //来显示那个图层将被显示
set
{
if(value=="All Layers")
axMap1.CtlBounds=axMap1.Layers.Bounds; //注意,这里使用CtlBounds属性(MapX的属性),而不是Bounds(.net本身的属性)在.net
else
axMap1.CtlBounds=axMap1.Layers[value].Bounds; //环境下,有点改变,


}
}


#endregion


//***********************************
#region 工具条,里面包含选择、拖动漫游、放大缩小等一些工具


private void toolBar2_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
{
try
{


if(e.Button==tbfangda)
axMap1.CurrentTool=MapXLib.ToolConstants.miZoomInTool;//1003
if(e.Button==tbbianxiao)
axMap1.CurrentTool=MapXLib.ToolConstants.miZoomOutTool;//缩小地图
if(e.Button==tbmanyou)
axMap1.CurrentTool=MapXLib.ToolConstants.miPanTool;//漫游地图
if(e.Button==tbbiaozhu) //标注地图
{
axMap1.CurrentTool=MapXLib.ToolConstants.miLabelTool;


//Point p=new Point(20+55*5,20);
//comu1.Show(toolBar2,p);


}
if(e.Button==tbtianjiafuhao)
axMap1.CurrentTool=MapXLib.ToolConstants.miSymbolTool;
if(e.Button==tbjuli)
axMap1.CurrentTool=( MapXLib.ToolConstants ) 10;
if(e.Button==tbmianji)
axMap1.CurrentTool=(MapXLib.ToolConstants ) 11;
if(e.Button==tbshuaxin)
{
//刷新地图,回到初始状态
axMap1.ZoomTo(this.zoom,this.centerx,this.centery); //恢复初始时的比例
//axMap1.Annotations.RemoveAll();
axMap1.Refresh();
}
if(e.Button==tbxuanze)
{
Point p=new Point(20,20);
comu.Show(toolBar2,p); //显示选择工具菜单,它允许选择矩形,圆形,多边形等
}



}
catch(Exception e1)
{
MessageBox.Show(e1.Message);
}
}


#endregion






接着第一篇文章,继续,这是在FrMain.cs文件中除了第一篇文章里的代码外的第二部分:
//*******************************


#region 创建自定义工具,用来测量距离(可以折线)和测量面积
//****************************888
private void mapMain_PolyToolUsed(object sender, AxMapXLib.CMapXEvents_PolyToolUsedEvent e) //在创建好自己定义的工具后,会根据工具ID来选择执行


{


if(e.toolNum == 10)//测量距离,这是自定义工具,测量多点的距离


{


MapXLib.Points pts = (MapXLib.Points)e.points;


MapXLib.Point pt1,pt2;


double d=0.0;


//计算顺序两个点距离,累计得到总距离


for(int i=1;i<pts.Count;i++)


{


pt1=pts._Item(i);


pt2=pts._Item(i+1);


d += axMap1.Distance(pt1.X,pt1.Y,pt2.X,pt2.Y);


}


label1.Text="距离:" + d.ToString();


}


else if(e.toolNum == 11) //面积,这是自定义工具,这里借用了某位网友的文章,谢谢


{


MapXLib.Points pts = (MapXLib.Points)e.points;


MapXLib.FeatureFactory dd = axMap1.FeatureFactory;


MapXLib.Style style = axMap1.DefaultStyle;


label1.Text="面积:" + dd.CreateRegion(pts,style).Area.ToString();


}


}
#endregion
//*******************************


private void axMap1_MapViewChanged(object sender, System.EventArgs e) //在地图大小改变时,显示出比例
{
label1.Text=axMap1.Zoom.ToString();
}


//*******************************
private void mutuceng_Click(object sender, System.EventArgs e) //菜单按钮事件,显示图层控制面板
{
axMap1.Layers.LayersDlg(null,null);
}


private void menuItem31_Click(object sender, System.EventArgs e) //菜单按钮事件,显示图层图层选择显示窗口
{
FrmViewLyr frm=new FrmViewLyr();
frm.Owner=this;
frm.ShowDialog();
}
private void menuItem32_Click(object sender, System.EventArgs e) //菜单按钮事件,在新窗口中控制图层的显示比例等,
{
#region 新窗口中控制图层的显示比例,此处是省略代码
//***********此处省略,主要使用到的是 MapUnit 和MapZoom *******************
//此处是伪代码,是mapx5.0的示例程序的vb代码,和上面的大同小异
//
// ZoomText.Text = fMainForm.Map1.Zoom

// '' Set the unit combo box to the current unit

// '' Take a short cut: units # 0 through 13 correspond to combo list number

// If fMainForm.Map1.MapUnit < 14 Then

// UnitCombo.ListIndex = fMainForm.Map1.MapUnit


// Else


// Select Case fMainForm.Map1.MapUnit


// Case miUnitLink

// UnitCombo.ListIndex = 14

// Case miUnitChain


// UnitCombo.ListIndex = 15

// Case miUnitRod


// UnitCombo.ListIndex = 16


// End Select


// End If


// '' Save the current parameters so that the view can be


// '' restored if "cancel" is pressed


// OldZoom = fMainForm.Map1.Zoom

// OldUnit = fMainForm.Map1.MapUnit
#endregion

}
//*******************************
private void axMap1_ThemeModifyRequested(object sender, AxMapXLib.CMapXEvents_ThemeModifyRequestedEvent e)
{
//e.theme
}


private void axMap1_MouseDownEvent(object sender, AxMapXLib.CMapXEvents_MouseDownEvent e)
{
//e.button=
}


private void menuItem4_Click(object sender, System.EventArgs e) //菜单按钮事件,普通选择地图
{
axMap1.CurrentTool=MapXLib.ToolConstants.miSelectTool;
menuItem4.RadioCheck=true;
}


private void menuItem5_Click(object sender, System.EventArgs e) //菜单按钮事件,长方形选择地图
{
axMap1.CurrentTool=MapXLib.ToolConstants.miRectSelectTool;
menuItem5.RadioCheck=true;
}


private void menuItem6_Click(object sender, System.EventArgs e) //菜单按钮事件,圆形选择地图
{
axMap1.CurrentTool=MapXLib.ToolConstants.miRadiusSelectTool;
menuItem6.RadioCheck=true;
}


private void menuItem7_Click(object sender, System.EventArgs e)
{
axMap1.CurrentTool=MapXLib.ToolConstants.miPolygonSelectTool; //菜单按钮事件,多边形选择地图
menuItem7.RadioCheck=true;
}


private void menuItem8_Click(object sender, System.EventArgs e)
{
axMap1.CurrentTool=MapXLib.ToolConstants.miLabelTool; //菜单按钮事件,直线标注地图
}


private void menuItem9_Click(object sender, System.EventArgs e)
{
//axMap1.CurrentTool=MapXLib.ToolConstants. ; //菜单按钮事件,折线标注地图
}


private void menuItem11_Click(object sender, System.EventArgs e)
{
axMap1.CurrentTool=MapXLib.ToolConstants.miSymbolTool; //菜单按钮事件,创建工具,可以在地图上添加符号,其实是对 mapx 的 Annotations 操作
}


private void menuItem12_Click(object sender, System.EventArgs e)
{
axMap1.CurrentTool=MapXLib.ToolConstants.miTextTool; //菜单按钮事件,创建工具,可以在地图上添加文本,其实是对 mapx 的 Annotations 操作
}


private void menuItem13_Click(object sender, System.EventArgs e)
{
axMap1.Annotations.RemoveAll(); // 把注释信息全部移除, 其实是对 mapx 的 Annotations 操作
}


private void menuItem14_Click(object sender, System.EventArgs e)
{
gistestbymyself.Frmannotation frm=new Frmannotation( axMap1.DefaultStyle.Clone() );
frm.Owner=this;
frm.ShowDialog(); // //未完成的,参见D:\Program Files\MapInfo\MapX 5.0\Samples50\VisualBasic\VB5Sample
}


private void menuItem16_Click(object sender, System.EventArgs e) //菜单按钮事件,显示属性窗口
{
axMap1.PropertyPage();
}


private void menuItem19_Click(object sender, System.EventArgs e) //显示mapx的关于对话框
{
axMap1.AboutBox();
}


private void menuItem29_Click(object sender, System.EventArgs e) //菜单按钮事件,调用窗口,对地图的坐标进行改变,或叫投影
{
axMap1.DisplayCoordSys.PickCoordSys(null,null);
axMap1.NumericCoordSys=axMap1.DisplayCoordSys;
}


private void axMap1_MouseUpEvent(object sender, AxMapXLib.CMapXEvents_MouseUpEvent e) //鼠标事件,点击右键,弹出属性框
{
if(e.button==2)
axMap1.PropertyPage();
}





接下来:
//******************************
#region 文件/打开*另存为*导出*按钮的代码


private void menuItem20_Click(object sender, System.EventArgs e) //打开地图文件(*.gst)(模式对话框)
{
OpenFileDialog openFD=new OpenFileDialog();
openFD.Title="打开地图"; //
openFD.DefaultExt=".gst";
openFD.FileName="";
openFD.Filter="Geoset Files (*.gst)|*.gst";
if(openFD.ShowDialog()==DialogResult.OK)
if( (openFD.OpenFile() )!=null )
{
axMap1.GeoSet=openFD.FileName; //这里存在已各bug,就是当可编辑工具可见时,能打开地图,再点击编辑按钮时,
//会出现错误;回避这个错误,请先点击"取消编辑".
}

}
private void menuItem21_Click(object sender, System.EventArgs e) //打开图层文件(*.tab)(模式对话框)
{
OpenFileDialog openFD=new OpenFileDialog();
openFD.Title="打开图层";
openFD.DefaultExt=".tab";
openFD.FileName="";
openFD.Filter="MapInfo Tables (*.tab)|*.tab";
if(openFD.ShowDialog()==DialogResult.OK)
if( (openFD.OpenFile() )!=null )
{
axMap1.Layers.Add(openFD.FileName,0);

}

}
private void menuItem23_Click(object sender, System.EventArgs e) //保存按钮,保存为 *.gst 形式
{
if(MessageBox.Show("确定要保存吗?","OK or nor",MessageBoxButtons.YesNo)==DialogResult.OK)
axMap1.SaveMapAsGeoset(axMap1.TitleText,axMap1.GeoSet);

}


private void menuItem24_Click(object sender, System.EventArgs e) //另存为... *.gst 形式
{
SaveFileDialog saveFD =new SaveFileDialog();
saveFD.Title="保存地图";
saveFD.FileName="";
saveFD.Filter="Geoset Files (*.gst)|*.gst";
if(saveFD.ShowDialog()==DialogResult.OK)
if( (saveFD.OpenFile() )!=null )
{
try
{

axMap1.SaveMapAsGeoset("",saveFD.FileName); //这里有错误,不能保存!!!!!!!!!!!!
}
catch(Exception e1)
{
MessageBox.Show(e1.Message);
}

}

}


private void menuItem25_Click(object sender, System.EventArgs e) //到处地图,可以 以各种图像的形式到出地图
{
//string ExFmtStr1=.ToString();


SaveFileDialog exportFD =new SaveFileDialog();
exportFD.Title="保存地图";
exportFD.FileName="";
exportFD.Filter= "windows bitmap(*.bmp)|*.bmp";
if(exportFD.ShowDialog()==DialogResult.OK)
if( (exportFD.OpenFile() )!=null )
{
try
{ //MapXLib.ExportFormatConstants.miFormatBMP
axMap1.ExportMap(exportFD.FileName,MapXLib.ExportFormatConstants.miFormatBMP,axMap1.MapPaperWidth,axMap1.MapScreenHeight);
//这里有错误,不能保存!!!!!!!!!!!!
}
catch(Exception e1)
{
MessageBox.Show(e1.Message);
}

}

}
#endregion


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


//*********************************
#region 编辑菜单按钮事件,创建编辑地图工具栏
//*********************
private void menuItem27_Click(object sender, System.EventArgs e) //菜单按钮事件,点击后,增加可编辑图层
{
if(this.menuItem27.Text.Trim()=="编辑")
{
///******************
this.menuItem27.Text="取消编辑";
this.tbadddian.Visible = true;
this.tbaddxian.Visible = true;
this.tbaddquxian.Visible = true;
this.tbaddqumian.Visible = true;
this.tbline.Visible = true;
this.tbshezhi.Visible = true;
///***************
///添加信息,初始化可编辑图层
///
MapXLib.Layer newLayer;//=new MapXLib.LayerClass();
MapXLib.LayerInfoClass newLyInfo=new MapXLib.LayerInfoClass();
MapXLib.FieldsClass newField=new MapXLib.FieldsClass();

newField.AddStringField("GeoName",10,10);


newLyInfo.Type=MapXLib.LayerInfoTypeConstants.miLayerInfoTypeTemp;
newLyInfo.AddParameter("Name","Temporary Layer");
newLyInfo.AddParameter("Fields",newField);

newLayer=axMap1.Layers.Add(newLyInfo,1);
newLayer.Editable=true;
axMap1.Layers.InsertionLayer=newLayer;


///******************************
///结束
///


}
else if(this.menuItem27.Text.Trim()=="取消编辑")
{
this.tbadddian.Visible = false;
this.tbaddxian.Visible = false;
this.tbaddquxian.Visible = false;
this.tbaddqumian.Visible = false;
this.tbline.Visible = false;
this.tbshezhi.Visible = false;
this.menuItem27.Text="编辑";


///********************
///取消那个可编辑图层
///
axMap1.Layers.Remove(1);


}

}
#endregion


//*********************************
#region 可编辑地图的工具的工具栏


private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
{
if(e.Button==tbadddian)
axMap1.CurrentTool=MapXLib.ToolConstants.miAddPointTool; //菜单按钮事件,在有可编辑地图的图层上,添加点,使用“添加点工具”
if(e.Button==tbaddxian)
axMap1.CurrentTool=MapXLib.ToolConstants.miAddLineTool; //菜单按钮事件,在有可编辑地图的图层上,添加线,使用“添加线工具”
if(e.Button==tbaddquxian)
axMap1.CurrentTool=MapXLib.ToolConstants.miAddPolylineTool; //菜单按钮事件,在有可编辑地图的图层上,添加曲线,使用“添加曲线工具”
if(e.Button==tbaddqumian)
axMap1.CurrentTool=MapXLib.ToolConstants.miAddRegionTool; //菜单按钮事件,在有可编辑地图的图层上,添加区域,使用“添加区域工具”
if(e.Button==tbshezhi)
{
Frmeditable frm=new Frmeditable(); //惨淡按钮事件,是对点、线、区域的样式进行设置的窗口
frm.Owner=this;
frm.ShowDialog();
}


}
#endregion


//*****************************
}
}



原文转自:http://www.ltesting.net

评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)