posts - 77, comments - 54, trackbacks - 0, articles - 0
  IT博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

显示多行InfoTips

Posted on 2006-06-01 11:51 东人EP 阅读(684) 评论(1)  编辑 收藏 引用 所属分类: MapInfo

把鼠标放在图元上点击一下,就能出现tips,但是这个tips的内容是系统生成的,并且只能显示一行,也不知道能不能编程修改它的显示方式,所以我就写了几行代码,来实现自定义的多行tips显示。

步骤:
1)在Winform中加入MapControl和一个TextBox
2)将TextBox的属性MultiLine设为True, Visable设为False,BorderStyle设为FixSingle
3)捕获MapControl1的MouseDown和MouseUp事件,函数内容如下:

        private void mapControl1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            //如果没有转载任何tab文件,则返回
           if(mapControl1.Map.Layers.Count == 0 || this.currentLayerName=="")//currentLayerName是顶层Layer的名字,同时也是Table的名字
                return;
           MapInfo.Engine.Selection selection=MapInfo.Engine.Session.Current.Selections.DefaultSelection;
           MapInfo.Data.Table table=MapInfo.Engine.Session.Current.Catalog.GetTable(currentLayerName);
           MapInfo.Data.IResultSetFeatureCollectionfeatureCollection=selection[table];
            //如果没有选中任何图元,或者选中多个图元,则返回
            if(featureCollection==null||featureCollection.Count!=1)
                return;

            //取第一个图元
            MapInfo.Geometry.Geometry geometry=featureCollection[0].Geometry;
            //创建连接和命令来查询table中的数据
            MapInfo.Data.MIConnection connection=new MapInfo.Data.MIConnection();
            MapInfo.Data.MICommand command=connection.CreateCommand();
            command.CommandText="SELECT * FROM "+currentLayerName+" WHERE Objwithin @featureObject";
            command.Parameters.Add("@featureObject",geometry);
            connection.Open();
            MapInfo.Data.MIDataReader dataReader=command.ExecuteReader();
            int n=dataReader.FieldCount;
            this.textBoxInfoTips.Clear();
            //把查询到的内容放进tips中
            for(int i=0;i<n;i++)
            {
              this.textBoxInfoTips.AppendText(dataReader.GetName(i)+"\t");
            }
            this.textBoxInfoTips.AppendText("\n");
            while(dataReader.Read())
            {
                for(int i=0;i<n;i++)
                {
                    object o=dataReader.GetValue(i);
                    if(o==DBNull.Value)
                      this.textBoxInfoTips.AppendText("null\t");
                    else
                      this.textBoxInfoTips.AppendText(o.ToString()+"\t");
                }
                this.textBoxInfoTips.AppendText("\n");
            }
            //改变tips的显示位置
            this.textBoxInfoTips.Location=new System.Drawing.Point(e.X,e.Y+50);
            this.textBoxInfoTips.Visible=true;
        }

        private void mapControl1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            this.textBoxInfoTips.Visible=false;
        }

=====
代码已测试通过。

Feedback

# re: 显示多行InfoTips  回复  更多评论   

2009-09-30 13:48 by LiWeiJiang
感谢分享你的成功!:)
只有注册用户登录后才能发表评论。