首页 > 开发 > 综合 > 正文

在C#中应用MapObjects(渲染效果)

2024-07-21 02:19:07
字体:
来源:转载
供稿:网友
注册会员,创建你的web开发资料库,以前用mapx,里面有专题图,挺好。经过几天的试验,我把mapobjects中的各种渲染效果(类似mapx专题图,但不一样)都试了试,下面附上源代码,如果那位有不懂的地方请自己琢磨,哈哈…



代码如下:



using system;

using system.drawing;

using system.collections;

using system.componentmodel;

using system.windows.forms;

using system.data;



namespace dbinmo

{

/// <summary>

/// form1 的摘要说明。

/// </summary>

public class 渲染示例 : system.windows.forms.form

{

private axmapobjects2.axmap map;

private system.windows.forms.button 直方图;

private system.windows.forms.button 饼图;

private system.windows.forms.button 点密度;

private system.windows.forms.button 按值渲染;

private system.windows.forms.button 分类渲染;

private system.windows.forms.button z值渲染;

private system.windows.forms.button 组渲染;

private system.windows.forms.button 退出;

private system.windows.forms.panel 操作板;

private system.windows.forms.button 删除所有渲染;

private system.windows.forms.panel panel1;

private system.windows.forms.button 全图;

private system.windows.forms.button 拖动;

private system.windows.forms.button 缩小;

private system.windows.forms.button 放大;

private system.windows.forms.panel 分隔符;

private system.windows.forms.button 标注渲染;

/// <summary>

/// 必需的设计器变量。

/// </summary>

private system.componentmodel.container components = null;



public 渲染示例()

{

//

// windows 窗体设计器支持所必需的

//

initializecomponent();



//

// todo: 在 initializecomponent 调用后添加任何构造函数代码

//

}



/// <summary>

/// 清理所有正在使用的资源。

/// </summary>

protected override void dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.dispose();

}

}

base.dispose( disposing );

}



#region windows 窗体设计器生成的代码

/// <summary>

/// 设计器支持所需的方法 - 不要使用代码编辑器修改

/// 此方法的内容。

/// </summary>

private void initializecomponent()

{

system.resources.resourcemanager resources = new system.resources.resourcemanager(typeof(渲染示例));

this.map = new axmapobjects2.axmap();

this.操作板 = new system.windows.forms.panel();

this.直方图 = new system.windows.forms.button();

this.饼图 = new system.windows.forms.button();

this.点密度 = new system.windows.forms.button();

this.按值渲染 = new system.windows.forms.button();

this.分类渲染 = new system.windows.forms.button();

this.z值渲染 = new system.windows.forms.button();

this.组渲染 = new system.windows.forms.button();

this.退出 = new system.windows.forms.button();

this.标注渲染 = new system.windows.forms.button();

this.删除所有渲染 = new system.windows.forms.button();

this.panel1 = new system.windows.forms.panel();

this.全图 = new system.windows.forms.button();

this.拖动 = new system.windows.forms.button();

this.缩小 = new system.windows.forms.button();

this.放大 = new system.windows.forms.button();

this.分隔符 = new system.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 = new system.drawing.point(0, 0);

this.map.name = "map";

this.map.ocxstate = ((system.windows.forms.axhost.state)(resources.getobject("map.ocxstate")));

this.map.size = new system.drawing.size(616, 429);

this.map.tabindex = 0;

this.map.mousedownevent += new axmapobjects2._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 = new system.drawing.point(496, 0);

this.操作板.name = "操作板";

this.操作板.size = new system.drawing.size(120, 429);

this.操作板.tabindex = 1;

//

// 直方图

//

this.直方图.dock = system.windows.forms.dockstyle.top;

this.直方图.location = new system.drawing.point(0, 0);

this.直方图.name = "直方图";

this.直方图.size = new system.drawing.size(116, 24);

this.直方图.tabindex = 0;

this.直方图.text = "直方图";

this.直方图.click += new system.eventhandler(this.直方图_click);

//

// 饼图

//

this.饼图.dock = system.windows.forms.dockstyle.top;

this.饼图.location = new system.drawing.point(0, 24);

this.饼图.name = "饼图";

this.饼图.size = new system.drawing.size(116, 24);

this.饼图.tabindex = 1;

this.饼图.text = "饼图";

this.饼图.click += new system.eventhandler(this.饼图_click);

//

// 点密度

//

this.点密度.dock = system.windows.forms.dockstyle.top;

this.点密度.location = new system.drawing.point(0, 48);

this.点密度.name = "点密度";

this.点密度.size = new system.drawing.size(116, 23);

this.点密度.tabindex = 2;

this.点密度.text = "点密度";

this.点密度.click += new system.eventhandler(this.点密度_click);

//

// 按值渲染

//

this.按值渲染.dock = system.windows.forms.dockstyle.top;

this.按值渲染.location = new system.drawing.point(0, 71);

this.按值渲染.name = "按值渲染";

this.按值渲染.size = new system.drawing.size(116, 23);

this.按值渲染.tabindex = 3;

this.按值渲染.text = "按值渲染";

this.按值渲染.click += new system.eventhandler(this.按值渲染_click);

//

// 分类渲染

//

this.分类渲染.dock = system.windows.forms.dockstyle.top;

this.分类渲染.location = new system.drawing.point(0, 94);

this.分类渲染.name = "分类渲染";

this.分类渲染.size = new system.drawing.size(116, 23);

this.分类渲染.tabindex = 4;

this.分类渲染.text = "分类渲染";

this.分类渲染.click += new system.eventhandler(this.分类渲染_click);

//

// z值渲染

//

this.z值渲染.dock = system.windows.forms.dockstyle.top;

this.z值渲染.location = new system.drawing.point(0, 117);

this.z值渲染.name = "z值渲染";

this.z值渲染.size = new system.drawing.size(116, 23);

this.z值渲染.tabindex = 5;

this.z值渲染.text = "z值渲染";

this.z值渲染.click += new system.eventhandler(this.z值渲染_click);

//

// 组渲染

//

this.组渲染.dock = system.windows.forms.dockstyle.top;

this.组渲染.location = new system.drawing.point(0, 140);

this.组渲染.name = "组渲染";

this.组渲染.size = new system.drawing.size(116, 23);

this.组渲染.tabindex = 6;

this.组渲染.text = "组渲染";

this.组渲染.click += new system.eventhandler(this.组渲染_click);

//

// 退出

//

this.退出.dock = system.windows.forms.dockstyle.bottom;

this.退出.location = new system.drawing.point(0, 402);

this.退出.name = "退出";

this.退出.size = new system.drawing.size(116, 23);

this.退出.tabindex = 7;

this.退出.text = "退出";

this.退出.click += new system.eventhandler(this.退出_click);

//

// 标注渲染

//

this.标注渲染.dock = system.windows.forms.dockstyle.top;

this.标注渲染.location = new system.drawing.point(0, 163);

this.标注渲染.name = "标注渲染";

this.标注渲染.size = new system.drawing.size(116, 23);

this.标注渲染.tabindex = 7;

this.标注渲染.text = "标注渲染";

this.标注渲染.click += new system.eventhandler(this.标注渲染_click);

//

// 删除所有渲染

//

this.删除所有渲染.dock = system.windows.forms.dockstyle.top;

this.删除所有渲染.location = new system.drawing.point(0, 319);

this.删除所有渲染.name = "删除所有渲染";

this.删除所有渲染.size = new system.drawing.size(116, 23);

this.删除所有渲染.tabindex = 22;

this.删除所有渲染.text = "删除所有渲染";

this.删除所有渲染.click += new system.eventhandler(this.删除所有渲染_click);

//

// panel1

//

this.panel1.dock = system.windows.forms.dockstyle.top;

this.panel1.location = new system.drawing.point(0, 299);

this.panel1.name = "panel1";

this.panel1.size = new system.drawing.size(116, 20);

this.panel1.tabindex = 21;

//

// 全图

//

this.全图.dock = system.windows.forms.dockstyle.top;

this.全图.location = new system.drawing.point(0, 276);

this.全图.name = "全图";

this.全图.size = new system.drawing.size(116, 23);

this.全图.tabindex = 20;

this.全图.text = "全图";

this.全图.click += new system.eventhandler(this.全图_click);

//

// 拖动

//

this.拖动.dock = system.windows.forms.dockstyle.top;

this.拖动.location = new system.drawing.point(0, 253);

this.拖动.name = "拖动";

this.拖动.size = new system.drawing.size(116, 23);

this.拖动.tabindex = 19;

this.拖动.text = "拖动";

this.拖动.click += new system.eventhandler(this.拖动_click);

//

// 缩小

//

this.缩小.dock = system.windows.forms.dockstyle.top;

this.缩小.location = new system.drawing.point(0, 230);

this.缩小.name = "缩小";

this.缩小.size = new system.drawing.size(116, 23);

this.缩小.tabindex = 18;

this.缩小.text = "缩小";

this.缩小.click += new system.eventhandler(this.缩小_click);

//

// 放大

//

this.放大.dock = system.windows.forms.dockstyle.top;

this.放大.location = new system.drawing.point(0, 207);

this.放大.name = "放大";

this.放大.size = new system.drawing.size(116, 23);

this.放大.tabindex = 17;

this.放大.text = "放大";

this.放大.click += new system.eventhandler(this.放大_click);

//

// 分隔符

//

this.分隔符.dock = system.windows.forms.dockstyle.top;

this.分隔符.location = new system.drawing.point(0, 186);

this.分隔符.name = "分隔符";

this.分隔符.size = new system.drawing.size(116, 21);

this.分隔符.tabindex = 16;

//

// 渲染示例

//

this.autoscalebasesize = new system.drawing.size(6, 14);

this.clientsize = new system.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]

static void main()

{

application.run(new 渲染示例());

}



/// <summary>

/// 退出系统

/// </summary>

private void 退出_click(object sender, system.eventargs e)

{

this.close();

}



/// <summary>

/// 图表渲染,值方图

/// </summary>

private void 直方图_click(object sender, system.eventargs e)

{

//直方图的例子中使用lakers图层

//

//定义图标渲染变量

mapobjects2.chartrenderer cr = new mapobjects2.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.maplayer lyr = (mapobjects2.maplayer)(map.layers.item("lakes"));

lyr.renderer = cr;

//使用refresh()刷新老也是刷新不好,正没办法。。。,设为全图倒是不错的注意,^_^

map.refresh();

}

/// <summary>

/// 全图视野

/// </summary>

private void 全图_click(object sender, system.eventargs e)

{

map.extent = map.fullextent;

}

/// <summary>

/// 拖动,只设置图标

/// </summary>

private void 拖动_click(object sender, system.eventargs e)

{

map.mousepointer = mapobjects2.mousepointerconstants.mopan;

}

/// <summary>

/// 缩小,只设置图标

/// </summary>

private void 缩小_click(object sender, system.eventargs e)

{

map.mousepointer = mapobjects2.mousepointerconstants.mozoomout;

}

/// <summary>

/// 放大,只设置图标

/// </summary>

private void 放大_click(object sender, system.eventargs e)

{

map.mousepointer = mapobjects2.mousepointerconstants.mozoomin;

}

/// <summary>

/// 鼠标按下时间,处理地图放大、缩小、拖动等。

/// </summary>

private void map_mousedownevent(object sender, axmapobjects2._dmapevents_mousedownevent e)

{

mapobjects2.rectangle rect;

mapobjects2.point pt = map.tomappoint(e.x,e.y);



if(e.button == 2)//右键点击取消

{

map.mousepointer = mapobjects2.mousepointerconstants.modefault;

}



if(map.mousepointer == mapobjects2.mousepointerconstants.mopan)//拖动

{

map.pan();

}

else if(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;

}

else if(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

{

double drate = map.extent.width / rect.width * 10;

rect.scalerectangle(drate);

}



map.extent = rect;

}

}

/// <summary>

/// 图表渲染,使用饼图

/// </summary>

private void 饼图_click(object sender, system.eventargs e)

{

//饼图的例子中使用lakers图层

//过程和直方图一样

//

//定义图标渲染变量

mapobjects2.chartrenderer cr = new mapobjects2.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.maplayer lyr = (mapobjects2.maplayer)(map.layers.item("lakes"));

lyr.renderer = cr;

//刷新

map.refresh();

}

/// <summary>

/// 点密度渲染

/// </summary>

private void 点密度_click(object sender, system.eventargs e)

{

//点密度的例子中使用lakes图层

//

//定义点密度渲染变量

mapobjects2.dotdensityrenderer ddr = new mapobjects2.dotdensityrendererclass();

//设置渲染的数据字段,请参看lakes.dbf

ddr.field = "depth";

//点大小

ddr.dotsize = 3;

//点颜色

ddr.dotcolor = (uint)mapobjects2.colorconstants.moblue;

ddr.dotvalue = 1;

ddr.drawbackground = true;

//添加到地图上

mapobjects2.maplayer lyr = (mapobjects2.maplayer)(map.layers.item("lakes"));

lyr.renderer = ddr;

//刷新

map.refresh();

}

/// <summary>

/// 按值渲染

/// </summary>

private void 按值渲染_click(object sender, system.eventargs e)

{

//按值渲染的例子中使用country图层,这里利用按值渲染,将每个国家用不同的颜色显示

//

//字符串s变量,很像delphi中的tstringlist,^_^

mapobjects2.strings strs = new mapobjects2.stringsclass();

//图层变量,引用country图层

mapobjects2.maplayer lyr = (mapobjects2.maplayer)map.layers.item("country");

//recordset对象,我将在以后的文章中陆续讲解关于mapobjects2.2中使用数据集变量的内容!!!

//使用方法大致与vb中对数据表的操作差不多。

mapobjects2.recordset rc = lyr.records;

//这个循环获取所有国家的简称代码列表

while(!rc.eof)

{

strs.add(rc.fields.item("fips_code").valueasstring);

rc.movenext();

}

//定义按值渲染对象

mapobjects2.valuemaprenderer vmr = new mapobjects2.valuemaprendererclass();

//将其添加到图层

lyr.renderer = vmr;

//设置渲染使用的字段,请参看country.dbf文件

vmr.field = "fips_code";

//渲染的个数

vmr.valuecount = strs.count;

//缺省值

vmr.usedefault = true;

mapobjects2.symbol sym = vmr.defaultsymbol;

sym.color = (uint)mapobjects2.colorconstants.moyellow;//缺省颜色为黄色

//渲染过程

for(int i=0;i<vmr.valuecount;i++)

{

vmr.set_value((short)i,strs.item((short)i));

}

//刷新

map.refresh();

}

/// <summary>

/// 分类渲染

/// </summary>

private void 分类渲染_click(object sender, system.eventargs e)

{

//分类渲染的例子使用country图层

//

//图层变量引用cities层

mapobjects2.maplayer lyr = (mapobjects2.maplayer)(map.layers.item("cities"));

//定义分类渲染变量

mapobjects2.classbreaksrenderer cbr = new mapobjects2.classbreaksrendererclass();

//添加到地图

lyr.renderer = cbr;

//设置符号类型为点,也可使用其他类型,请自己试验,方法大概都差不多

cbr.symboltype = mapobjects2.symboltypeconstants.mopointsymbol;

//设置字段,请参看cities.dbf文件

cbr.field = "population";

//statistics对象经常配合分类渲染使用,他表示用一个recordset对象的calculatestatistics方法

// 对一个数值字段的统计计算结果

mapobjects2.statistics stat = lyr.records.calculatestatistics("population");

//mean——平均值,stddev——均方差

double fbv = stat.mean - (stat.stddev * 3);

for(int i=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>

private void z值渲染_click(object sender, system.eventargs e)

{

//本来想好好搞搞,可是我找不到带有z值的地图,哈哈,认了吧,下面给出基本的语句,有兴趣的

//兄弟姐妹们可以自己试试

// mapobjects2.zrenderer zr = new mapobjects2.zrendererclass();

// //z值分成几部分?

// zr.breakcount = 2;

// //设置各个部分的分界线

// zr.set_break(0,100);

// zr.set_break(1,1000);

// //设置特征类型

// zr.symboltype = mapobjects2.symboltypeconstants.mopointsymbol;

// mapobjects2.symbol sym;

// for(short i=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.maplayer lyr = (mapobjects2.maplayer)map.layers.item("带有z值的图层名称");

// lyr.renderer = zr;

// //刷新

// map.refresh();

}

/// <summary>

/// 组渲染

/// </summary>

private void 组渲染_click(object sender, system.eventargs e)

{

//组渲染其实就是组合渲染,一般定义多个渲染方式,然后使用如下的语句进行组合



mapobjects2.maplayer lyr = (mapobjects2.maplayer)map.layers.item("country");



//标注渲染

mapobjects2.labelrenderer lr = new mapobjects2.labelrendererclass();

lr.field = "name";

lr.get_symbol(0).color = (uint)mapobjects2.colorconstants.moblack;

lr.get_symbol(0).font.size = 8;



//按值渲染

mapobjects2.strings strs = new mapobjects2.stringsclass();

mapobjects2.recordset rc = lyr.records;

while(!rc.eof)

{

strs.add(rc.fields.item("fips_code").valueasstring);

rc.movenext();

}

mapobjects2.valuemaprenderer vmr = new mapobjects2.valuemaprendererclass();

vmr.field = "fips_code";

vmr.valuecount = strs.count;

vmr.usedefault = true;

mapobjects2.symbol sym = vmr.defaultsymbol;

sym.color = (uint)mapobjects2.colorconstants.moyellow;

for(int i=0;i<vmr.valuecount;i++)

{

vmr.set_value((short)i,strs.item((short)i));

}



//将二者组合,添加到图层

mapobjects2.grouprenderer gr = new mapobjects2.grouprendererclass();

gr.add(vmr);

gr.add(lr);

lyr.renderer = gr;

map.refresh();



}

/// <summary>

/// 删除所有渲染

/// </summary>

private void 删除所有渲染_click(object sender, system.eventargs e)

{

mapobjects2.maplayer lyr;



for(int i=0;i<map.layers.count;i++)

{

lyr = (mapobjects2.maplayer)map.layers.item(i);

lyr.renderer = new mapobjects2.labelrendererclass();

map.refresh();

}

}

/// <summary>

/// 标注渲染

/// </summary>

private void 标注渲染_click(object sender, system.eventargs e)

{

mapobjects2.maplayer lyr = (mapobjects2.maplayer)map.layers.item("cities");

mapobjects2.labelrenderer lr = new mapobjects2.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();

}

}

}

上一篇:C# 绘图--金刚石

下一篇:My Adapter in C#

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表