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

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) { 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#中应用MapObjects(渲染效果))