usingSystem;
usingSystem.Drawing;
usingSystem.Collections;
usingSystem.ComponentModel;
usingSystem.Windows.Forms;
usingSystem.Data;
namespaceDbInMo
{
///<summary>
///Form1的摘要说明。
///</summary>
publicclass渲染示例:System.Windows.Forms.Form
{
privateAxMapObjects2.AxMapmap;
privateSystem.Windows.Forms.Button直方图;
privateSystem.Windows.Forms.Button饼图;
privateSystem.Windows.Forms.Button点密度;
privateSystem.Windows.Forms.Button按值渲染;
privateSystem.Windows.Forms.Button分类渲染;
privateSystem.Windows.Forms.ButtonZ值渲染;
privateSystem.Windows.Forms.Button组渲染;
privateSystem.Windows.Forms.Button退出;
privateSystem.Windows.Forms.Panel操作板;
privateSystem.Windows.Forms.Button删除所有渲染;
privateSystem.Windows.Forms.Panelpanel1;
privateSystem.Windows.Forms.Button全图;
privateSystem.Windows.Forms.Button拖动;
privateSystem.Windows.Forms.Button缩小;
privateSystem.Windows.Forms.Button放大;
privateSystem.Windows.Forms.Panel分隔符;
privateSystem.Windows.Forms.Button标注渲染;
///<summary>
///必需的设计器变量。
///</summary>
privateSystem.ComponentModel.Containercomponents=null;
public渲染示例()
{
//
//Windows窗体设计器支持所必需的
//
InitializeComponent();
//
//TODO:在InitializeComponent调用后添加任何构造函数代码
//
}
///<summary>
///清理所有正在使用的资源。
///</summary>
protectedoverridevoidDispose(booldisposing)
{
if(disposing)
{
if(components!=null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
#regionWindows窗体设计器生成的代码
///<summary>
///设计器支持所需的方法-不要使用代码编辑器修改
///此方法的内容。
///</summary>
privatevoidInitializeComponent()
{
System.Resources.ResourceManagerresources=newSystem.Resources.ResourceManager(typeof(渲染示例));
this.map=newAxMapObjects2.AxMap();
this.操作板=newSystem.Windows.Forms.Panel();
this.直方图=newSystem.Windows.Forms.Button();
this.饼图=newSystem.Windows.Forms.Button();
this.点密度=newSystem.Windows.Forms.Button();
this.按值渲染=newSystem.Windows.Forms.Button();
this.分类渲染=newSystem.Windows.Forms.Button();
this.Z值渲染=newSystem.Windows.Forms.Button();
this.组渲染=newSystem.Windows.Forms.Button();
this.退出=newSystem.Windows.Forms.Button();
this.标注渲染=newSystem.Windows.Forms.Button();
this.删除所有渲染=newSystem.Windows.Forms.Button();
this.panel1=newSystem.Windows.Forms.Panel();
this.全图=newSystem.Windows.Forms.Button();
this.拖动=newSystem.Windows.Forms.Button();
this.缩小=newSystem.Windows.Forms.Button();
this.放大=newSystem.Windows.Forms.Button();
this.分隔符=newSystem.Windows.Forms.Panel();
((System.ComponentModel.ISupportInitialize)(this.map)).BeginInit();
this.操作板.SuspendLayout();
this.SuspendLayout();
//
//map
//
this.map.Dock=System.Windows.Forms.DockStyle.Fill;
this.map.Location=newSystem.Drawing.Point(0,0);
this.map.Name="map";
this.map.OcxState=((System.Windows.Forms.AxHost.State)(resources.GetObject("map.OcxState")));
this.map.Size=newSystem.Drawing.Size(616,429);
this.map.TabIndex=0;
this.map.MouseDownEvent+=newAxMapObjects2._DMapEvents_MouseDownEventHandler(this.map_MouseDownEvent);
//
//操作板
//
this.操作板.BorderStyle=System.Windows.Forms.BorderStyle.Fixed3D;
this.操作板.Controls.Add(this.删除所有渲染);
this.操作板.Controls.Add(this.panel1);
this.操作板.Controls.Add(this.全图);
this.操作板.Controls.Add(this.拖动);
this.操作板.Controls.Add(this.缩小);
this.操作板.Controls.Add(this.放大);
this.操作板.Controls.Add(this.分隔符);
this.操作板.Controls.Add(this.标注渲染);
this.操作板.Controls.Add(this.退出);
this.操作板.Controls.Add(this.组渲染);
this.操作板.Controls.Add(this.Z值渲染);
this.操作板.Controls.Add(this.分类渲染);
this.操作板.Controls.Add(this.按值渲染);
this.操作板.Controls.Add(this.点密度);
this.操作板.Controls.Add(this.饼图);
this.操作板.Controls.Add(this.直方图);
this.操作板.Dock=System.Windows.Forms.DockStyle.Right;
this.操作板.Location=newSystem.Drawing.Point(496,0);
this.操作板.Name="操作板";
this.操作板.Size=newSystem.Drawing.Size(120,429);
this.操作板.TabIndex=1;
//
//直方图
//
this.直方图.Dock=System.Windows.Forms.DockStyle.Top;
this.直方图.Location=newSystem.Drawing.Point(0,0);
this.直方图.Name="直方图";
this.直方图.Size=newSystem.Drawing.Size(116,24);
this.直方图.TabIndex=0;
this.直方图.Text="直方图";
this.直方图.Click+=newSystem.EventHandler(this.直方图_Click);
//
//饼图
//
this.饼图.Dock=System.Windows.Forms.DockStyle.Top;
this.饼图.Location=newSystem.Drawing.Point(0,24);
this.饼图.Name="饼图";
this.饼图.Size=newSystem.Drawing.Size(116,24);
this.饼图.TabIndex=1;
this.饼图.Text="饼图";
this.饼图.Click+=newSystem.EventHandler(this.饼图_Click);
//
//点密度
//
this.点密度.Dock=System.Windows.Forms.DockStyle.Top;
this.点密度.Location=newSystem.Drawing.Point(0,48);
this.点密度.Name="点密度";
this.点密度.Size=newSystem.Drawing.Size(116,23);
this.点密度.TabIndex=2;
this.点密度.Text="点密度";
this.点密度.Click+=newSystem.EventHandler(this.点密度_Click);
//
//按值渲染
//
this.按值渲染.Dock=System.Windows.Forms.DockStyle.Top;
this.按值渲染.Location=newSystem.Drawing.Point(0,71);
this.按值渲染.Name="按值渲染";
this.按值渲染.Size=newSystem.Drawing.Size(116,23);
this.按值渲染.TabIndex=3;
this.按值渲染.Text="按值渲染";
this.按值渲染.Click+=newSystem.EventHandler(this.按值渲染_Click);
//
//分类渲染
//
this.分类渲染.Dock=System.Windows.Forms.DockStyle.Top;
this.分类渲染.Location=newSystem.Drawing.Point(0,94);
this.分类渲染.Name="分类渲染";
this.分类渲染.Size=newSystem.Drawing.Size(116,23);
this.分类渲染.TabIndex=4;
this.分类渲染.Text="分类渲染";
this.分类渲染.Click+=newSystem.EventHandler(this.分类渲染_Click);
//
//Z值渲染
//
this.Z值渲染.Dock=System.Windows.Forms.DockStyle.Top;
this.Z值渲染.Location=newSystem.Drawing.Point(0,117);
this.Z值渲染.Name="Z值渲染";
this.Z值渲染.Size=newSystem.Drawing.Size(116,23);
this.Z值渲染.TabIndex=5;
this.Z值渲染.Text="Z值渲染";
this.Z值渲染.Click+=newSystem.EventHandler(this.Z值渲染_Click);
//
//组渲染
//
this.组渲染.Dock=System.Windows.Forms.DockStyle.Top;
this.组渲染.Location=newSystem.Drawing.Point(0,140);
this.组渲染.Name="组渲染";
this.组渲染.Size=newSystem.Drawing.Size(116,23);
this.组渲染.TabIndex=6;
this.组渲染.Text="组渲染";
this.组渲染.Click+=newSystem.EventHandler(this.组渲染_Click);
//
//退出
//
this.退出.Dock=System.Windows.Forms.DockStyle.Bottom;
this.退出.Location=newSystem.Drawing.Point(0,402);
this.退出.Name="退出";
this.退出.Size=newSystem.Drawing.Size(116,23);
this.退出.TabIndex=7;
this.退出.Text="退出";
this.退出.Click+=newSystem.EventHandler(this.退出_Click);
//
//标注渲染
//
this.标注渲染.Dock=System.Windows.Forms.DockStyle.Top;
this.标注渲染.Location=newSystem.Drawing.Point(0,163);
this.标注渲染.Name="标注渲染";
this.标注渲染.Size=newSystem.Drawing.Size(116,23);
this.标注渲染.TabIndex=7;
this.标注渲染.Text="标注渲染";
this.标注渲染.Click+=newSystem.EventHandler(this.标注渲染_Click);
//
//删除所有渲染
//
this.删除所有渲染.Dock=System.Windows.Forms.DockStyle.Top;
this.删除所有渲染.Location=newSystem.Drawing.Point(0,319);
this.删除所有渲染.Name="删除所有渲染";
this.删除所有渲染.Size=newSystem.Drawing.Size(116,23);
this.删除所有渲染.TabIndex=22;
this.删除所有渲染.Text="删除所有渲染";
this.删除所有渲染.Click+=newSystem.EventHandler(this.删除所有渲染_Click);
//
//panel1
//
this.panel1.Dock=System.Windows.Forms.DockStyle.Top;
this.panel1.Location=newSystem.Drawing.Point(0,299);
this.panel1.Name="panel1";
this.panel1.Size=newSystem.Drawing.Size(116,20);
this.panel1.TabIndex=21;
//
//全图
//
this.全图.Dock=System.Windows.Forms.DockStyle.Top;
this.全图.Location=newSystem.Drawing.Point(0,276);
this.全图.Name="全图";
this.全图.Size=newSystem.Drawing.Size(116,23);
this.全图.TabIndex=20;
this.全图.Text="全图";
this.全图.Click+=newSystem.EventHandler(this.全图_Click);
//
//拖动
//
this.拖动.Dock=System.Windows.Forms.DockStyle.Top;
this.拖动.Location=newSystem.Drawing.Point(0,253);
this.拖动.Name="拖动";
this.拖动.Size=newSystem.Drawing.Size(116,23);
this.拖动.TabIndex=19;
this.拖动.Text="拖动";
this.拖动.Click+=newSystem.EventHandler(this.拖动_Click);
//
//缩小
//
this.缩小.Dock=System.Windows.Forms.DockStyle.Top;
this.缩小.Location=newSystem.Drawing.Point(0,230);
this.缩小.Name="缩小";
this.缩小.Size=newSystem.Drawing.Size(116,23);
this.缩小.TabIndex=18;
this.缩小.Text="缩小";
this.缩小.Click+=newSystem.EventHandler(this.缩小_Click);
//
//放大
//
this.放大.Dock=System.Windows.Forms.DockStyle.Top;
this.放大.Location=newSystem.Drawing.Point(0,207);
this.放大.Name="放大";
this.放大.Size=newSystem.Drawing.Size(116,23);
this.放大.TabIndex=17;
this.放大.Text="放大";
this.放大.Click+=newSystem.EventHandler(this.放大_Click);
//
//分隔符
//
this.分隔符.Dock=System.Windows.Forms.DockStyle.Top;
this.分隔符.Location=newSystem.Drawing.Point(0,186);
this.分隔符.Name="分隔符";
this.分隔符.Size=newSystem.Drawing.Size(116,21);
this.分隔符.TabIndex=16;
//
//渲染示例
//
this.AutoScaleBaseSize=newSystem.Drawing.Size(6,14);
this.ClientSize=newSystem.Drawing.Size(616,429);
this.Controls.Add(this.操作板);
this.Controls.Add(this.map);
this.Name="渲染示例";
this.Text="渲染示例";
this.WindowState=System.Windows.Forms.FormWindowState.Maximized;
((System.ComponentModel.ISupportInitialize)(this.map)).EndInit();
this.操作板.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
///<summary>
///应用程序的主入口点。
///</summary>
[STAThread]
staticvoidMain()
{
Application.Run(new渲染示例());
}
///<summary>
///退出系统
///</summary>
privatevoid退出_Click(objectsender,System.EventArgse)
{
this.Close();
}
///<summary>
///图表渲染,值方图
///</summary>
privatevoid直方图_Click(objectsender,System.EventArgse)
{
//直方图的例子中使用Lakers图层
//
//定义图标渲染变量
MapObjects2.ChartRenderercr=newMapObjects2.ChartRendererClass();
//设置渲染类型为直方图
cr.ChartType=MapObjects2.ChartTypeConstants.moBar;
//设置直方图显示的字段个数为两个,就是直方图显示两个柱子
cr.FieldCount=2;
//设置第一个字段,请参看lakes.dbf
cr.set_Field(0,"SURF_ELEV");
//设置这个直方图的柱子颜色为红色
cr.set_Color(0,(uint)MapObjects2.ColorConstants.moRed);
//设置第二个柱子
cr.set_Field(1,"DEPTH");
cr.set_Color(1,(uint)MapObjects2.ColorConstants.moGreen);
MapObjects2.MapLayerlyr=(MapObjects2.MapLayer)(map.Layers.Item("lakes"));
lyr.Renderer=cr;
//使用Refresh()刷新老也是刷新不好,正没办法。。。,设为全图倒是不错的注意,^_^
map.Refresh();
}
///<summary>
///全图视野
///</summary>
privatevoid全图_Click(objectsender,System.EventArgse)
{
map.Extent=map.FullExtent;
}
///<summary>
///拖动,只设置图标
///</summary>
privatevoid拖动_Click(objectsender,System.EventArgse)
{
map.MousePointer=MapObjects2.MousePointerConstants.moPan;
}
///<summary>
///缩小,只设置图标
///</summary>
privatevoid缩小_Click(objectsender,System.EventArgse)
{
map.MousePointer=MapObjects2.MousePointerConstants.moZoomOut;
}
///<summary>
///放大,只设置图标
///</summary>
privatevoid放大_Click(objectsender,System.EventArgse)
{
map.MousePointer=MapObjects2.MousePointerConstants.moZoomIn;
}
///<summary>
///鼠标按下时间,处理地图放大、缩小、拖动等。
///</summary>
privatevoidmap_MouseDownEvent(objectsender,AxMapObjects2._DMapEvents_MouseDownEvente)
{
MapObjects2.Rectanglerect;
MapObjects2.Pointpt=map.ToMapPoint(e.x,e.y);
if(e.button==2)//右键点击取消
{
map.MousePointer=MapObjects2.MousePointerConstants.moDefault;
}
if(map.MousePointer==MapObjects2.MousePointerConstants.moPan)//拖动
{
map.Pan();
}
elseif(map.MousePointer==MapObjects2.MousePointerConstants.moZoomIn)//放大
{
rect=map.TrackRectangle();
if(rect==null||(rect.Width<0.00005)||(rect.Height<0.00005))
{
rect=map.Extent;
rect.ScaleRectangle(0.6667);
rect.Offset(-(rect.Center.X-pt.X),-(rect.Center.Y-pt.Y));
}
map.Extent=rect;
}
elseif(map.MousePointer==MapObjects2.MousePointerConstants.moZoomOut)//缩小
{
rect=map.TrackRectangle();
if((null==rect)||(rect.Width<0.00005)||(rect.Height<0.00005))
{
rect=map.Extent;
rect.ScaleRectangle(1.5);
rect.Offset(-(rect.Center.X-pt.X),-(rect.Center.Y-pt.Y));
}
else
{
doubledRate=map.Extent.Width/rect.Width*10;
rect.ScaleRectangle(dRate);
}
map.Extent=rect;
}
}
///<summary>
///图表渲染,使用饼图
///</summary>
privatevoid饼图_Click(objectsender,System.EventArgse)
{
//饼图的例子中使用Lakers图层
//过程和直方图一样
//
//定义图标渲染变量
MapObjects2.ChartRenderercr=newMapObjects2.ChartRendererClass();
//设置渲染类型为饼图
cr.ChartType=MapObjects2.ChartTypeConstants.moPie;
//设置饼图显示的字段个数为两个,就是饼图显示两半
cr.FieldCount=2;
//设置第一个字段,请参看lakes.dbf
cr.set_Field(0,"SURF_ELEV");
//设置这个饼图的一半颜色为红色
cr.set_Color(0,(uint)MapObjects2.ColorConstants.moRed);
//设置第二半
cr.set_Field(1,"DEPTH");
cr.set_Color(1,(uint)MapObjects2.ColorConstants.moGreen);
//添加到地图上
MapObjects2.MapLayerlyr=(MapObjects2.MapLayer)(map.Layers.Item("lakes"));
lyr.Renderer=cr;
//刷新
map.Refresh();
}
///<summary>
///点密度渲染
///</summary>
privatevoid点密度_Click(objectsender,System.EventArgse)
{
//点密度的例子中使用lakes图层
//
//定义点密度渲染变量
MapObjects2.DotDensityRendererddr=newMapObjects2.DotDensityRendererClass();
//设置渲染的数据字段,请参看lakes.dbf
ddr.Field="DEPTH";
//点大小
ddr.DotSize=3;
//点颜色
ddr.DotColor=(uint)MapObjects2.ColorConstants.moBlue;
ddr.DotValue=1;
ddr.DrawBackground=true;
//添加到地图上
MapObjects2.MapLayerlyr=(MapObjects2.MapLayer)(map.Layers.Item("lakes"));
lyr.Renderer=ddr;
//刷新
map.Refresh();
}
///<summary>
///按值渲染
///</summary>
privatevoid按值渲染_Click(objectsender,System.EventArgse)
{
//按值渲染的例子中使用country图层,这里利用按值渲染,将每个国家用不同的颜色显示
//
//字符串s变量,很像Delphi中的TStringList,^_^
MapObjects2.Stringsstrs=newMapObjects2.StringsClass();
//图层变量,引用country图层
MapObjects2.MapLayerlyr=(MapObjects2.MapLayer)map.Layers.Item("country");
//Recordset对象,我将在以后的文章中陆续讲解关于MapObjects2.2中使用数据集变量的内容!!!
//使用方法大致与VB中对数据表的操作差不多。
MapObjects2.Recordsetrc=lyr.Records;
//这个循环获取所有国家的简称代码列表
while(!rc.EOF)
{
strs.Add(rc.Fields.Item("FIPS_CODE").ValueAsString);
rc.MoveNext();
}
//定义按值渲染对象
MapObjects2.ValueMapRenderervmr=newMapObjects2.ValueMapRendererClass();
//将其添加到图层
lyr.Renderer=vmr;
//设置渲染使用的字段,请参看country.dbf文件
vmr.Field="FIPS_CODE";
//渲染的个数
vmr.ValueCount=strs.Count;
//缺省值
vmr.UseDefault=true;
MapObjects2.Symbolsym=vmr.DefaultSymbol;
sym.Color=(uint)MapObjects2.ColorConstants.moYellow;//缺省颜色为黄色
//渲染过程
for(inti=0;i<vmr.ValueCount;i++)
{
vmr.set_Value((short)i,strs.Item((short)i));
}
//刷新
map.Refresh();
}
///<summary>
///分类渲染
///</summary>
privatevoid分类渲染_Click(objectsender,System.EventArgse)
{
//分类渲染的例子使用country图层
//
//图层变量引用cities层
MapObjects2.MapLayerlyr=(MapObjects2.MapLayer)(map.Layers.Item("cities"));
//定义分类渲染变量
MapObjects2.ClassBreaksRenderercbr=newMapObjects2.ClassBreaksRendererClass();
//添加到地图
lyr.Renderer=cbr;
//设置符号类型为点,也可使用其他类型,请自己试验,方法大概都差不多
cbr.SymbolType=MapObjects2.SymbolTypeConstants.moPointSymbol;
//设置字段,请参看cities.dbf文件
cbr.Field="POPULATION";
//Statistics对象经常配合分类渲染使用,他表示用一个Recordset对象的CalculateStatistics方法
//对一个数值字段的统计计算结果
MapObjects2.Statisticsstat=lyr.Records.CalculateStatistics("POPULATION");
//Mean——平均值,StdDev——均方差
doublefbv=stat.Mean-(stat.StdDev*3);
for(inti=0;i<6;i++)
{
if((fbv>=stat.Min)&&fbv<=stat.Max)
{
//分类个数,依次加一
cbr.BreakCount=(short)(cbr.BreakCount+1);
//设置分类
cbr.set_Break((short)(cbr.BreakCount-1),fbv);
}
fbv+=stat.StdDev;
}
//SizeSymbols方法制定头尾的大小用来表现点特征大小渐进效果
cbr.SizeSymbols(3,8);
//RampColors方法设置颜色从白色到红色的渐进显示
cbr.RampColors((uint)MapObjects2.ColorConstants.moWhite,(uint)MapObjects2.ColorConstants.moRed);
//刷新
map.Refresh();
}
///<summary>
///Z值渲染
///</summary>
privatevoidZ值渲染_Click(objectsender,System.EventArgse)
{
//本来想好好搞搞,可是我找不到带有Z值的地图,哈哈,认了吧,下面给出基本的语句,有兴趣的
//兄弟姐妹们可以自己试试
//MapObjects2.ZRendererzr=newMapObjects2.ZRendererClass();
////Z值分成几部分?
//zr.BreakCount=2;
////设置各个部分的分界线
//zr.set_Break(0,100);
//zr.set_Break(1,1000);
////设置特征类型
//zr.SymbolType=MapObjects2.SymbolTypeConstants.moPointSymbol;
//MapObjects2.Symbolsym;
//for(shorti=0;i<zr.BreakCount;i++)
//{
////获取特征
//sym=zr.get_Symbol(i);
////设置特征属性,定义一个颜色数组,将不同特征设置不同颜色,效果会明显些
//sym.Color=(uint)MapObjects2.ColorConstants.moRed;
//sym.Size=6;
//sym.Style=MapObjects2.MarkerStyleConstants.moTriangleMarker;
//}
////添加到地图
//MapObjects2.MapLayerlyr=(MapObjects2.MapLayer)map.Layers.Item("带有Z值的图层名称");
//lyr.Renderer=zr;
////刷新
//map.Refresh();
}
///<summary>
///组渲染
///</summary>
privatevoid组渲染_Click(objectsender,System.EventArgse)
{
//组渲染其实就是组合渲染,一般定义多个渲染方式,然后使用如下的语句进行组合
MapObjects2.MapLayerlyr=(MapObjects2.MapLayer)map.Layers.Item("country");
//标注渲染
MapObjects2.LabelRendererlr=newMapObjects2.LabelRendererClass();
lr.Field="NAME";
lr.get_Symbol(0).Color=(uint)MapObjects2.ColorConstants.moBlack;
lr.get_Symbol(0).Font.Size=8;
//按值渲染
MapObjects2.Stringsstrs=newMapObjects2.StringsClass();
MapObjects2.Recordsetrc=lyr.Records;
while(!rc.EOF)
{
strs.Add(rc.Fields.Item("FIPS_CODE").ValueAsString);
rc.MoveNext();
}
MapObjects2.ValueMapRenderervmr=newMapObjects2.ValueMapRendererClass();
vmr.Field="FIPS_CODE";
vmr.ValueCount=strs.Count;
vmr.UseDefault=true;
MapObjects2.Symbolsym=vmr.DefaultSymbol;
sym.Color=(uint)MapObjects2.ColorConstants.moYellow;
for(inti=0;i<vmr.ValueCount;i++)
{
vmr.set_Value((short)i,strs.Item((short)i));
}
//将二者组合,添加到图层
MapObjects2.GroupRenderergr=newMapObjects2.GroupRendererClass();
gr.Add(vmr);
gr.Add(lr);
lyr.Renderer=gr;
map.Refresh();
}
///<summary>
///删除所有渲染
///</summary>
privatevoid删除所有渲染_Click(objectsender,System.EventArgse)
{
MapObjects2.MapLayerlyr;
for(inti=0;i<map.Layers.Count;i++)
{
lyr=(MapObjects2.MapLayer)map.Layers.Item(i);
lyr.Renderer=newMapObjects2.LabelRendererClass();
map.Refresh();
}
}
///<summary>
///标注渲染
///</summary>
privatevoid标注渲染_Click(objectsender,System.EventArgse)
{
MapObjects2.MapLayerlyr=(MapObjects2.MapLayer)map.Layers.Item("cities");
MapObjects2.LabelRendererlr=newMapObjects2.LabelRendererClass();
lr.Field="NAME";
lr.get_Symbol(0).Color=(uint)MapObjects2.ColorConstants.moBlack;
lr.get_Symbol(0).Font.Size=8;
lyr.Renderer=lr;
map.Refresh();
}
}
}