Mapwingis开发03(图层数据获取)

一.弹框显示图层数据

 private ToolStripStatusLabel statusStrip1 = new ToolStripStatusLabel();
        private ToolStripStatusLabel m_label = null;
        public Form1()
        {
            InitializeComponent();
            ShowAttributes(axMap1, @"", statusStrip1);
        }
        public void ShowAttributes(AxMap axMap1, string dataPath, ToolStripStatusLabel label)
        {
           axMap1.Projection = tkMapProjection.PROJECTION_WGS84;
            string filename = dataPath + "tantou5.shp";
            Shapefile sf = new Shapefile();
            if (sf.Open(filename))
            {
                int m_layerHandle = axMap1.AddLayer(sf, true);
                sf = axMap1.get_Shapefile(m_layerHandle);
                //
                if (!sf.Table.StartEditingTable(null))
                {
                    MessageBox.Show("无法打开编辑模式" + sf.Table.ErrorMsg[sf.LastErrorCode]);
                }
                else
                {
                    提示框
                    //string expression = "";
                    //for (int i = 0; i < sf.NumFields; i++)
                    //{
                    //    expression += "[" + sf.Field[i].Name + "]";
                    //    if (i != sf.NumFields - 1)
                    //    {
                    //        const string endLine = "\"\n\"";
                    //        expression += string.Format("+ {0} +", endLine);
                    //    }
                    //}
                    //sf.Labels.Generate(expression, tkLabelPositioning.lpCentroid, false);
                    sf.Labels.TextRenderingHint = tkTextRenderingHint.SystemDefault;
                    axMap1.SendMouseDown = true;
                    axMap1.CursorMode = tkCursorMode.cmIdentify;
                    axMap1.ShapeIdentified += AxMap1ShapeIdentified;  // change MapEvents to axMap1
                    m_label = label;

                }
            }
            else
            {
                MessageBox.Show("打开图层失败,请检查该节点下是否存在该图层");
            }
        }
        //鼠标点击触发函数
        void AxMap1ShapeIdentified(object sender, _DMapEvents_ShapeIdentifiedEvent e)
        {
            Shapefile sf = axMap1.get_Shapefile(e.layerHandle);
            if (sf != null)
            {
                //object result = null;
                //int[] shapes = result as int[];
                //弹出框的设置
                Form form = new Form();
                for (int i = 0; i < sf.NumFields; i++)//为弹出框添加标签和文本内容
                {
                    System.Windows.Forms.Label label = new System.Windows.Forms.Label();
                    label.Left = 15;
                    label.Top = i * 30 + 5;
                    label.Text = sf.Field[i].Name;
                    label.Width = 60;
                    form.Controls.Add(label);
                    TextBox box = new TextBox();
                    box.Left = 80;
                    box.Top = label.Top;
                    box.Width = 80;
                    box.Text = sf.get_CellValue(i, e.shapeIndex).ToString();
                    box.Name = sf.Table.Field[i].Name;
                    box.ReadOnly = false;

                    form.Controls.Add(box);
                }
                Button btn = new Button
                {
                    Text = "Ok",
                    Top = sf.NumFields * 30 + 10,
                    Left = 20,
                    Width = 70,
                    Height = 25
                };
                btn.Click += BtnClick;
                form.Controls.Add(btn);
                btn = new Button
                {
                    Text = "Cancel",
                    Top = sf.NumFields * 30 + 10,
                    Left = 100,
                    Width = 70,
                    Height = 25
                };
                btn.Click += BtnClick;
                form.Controls.Add(btn);

                form.Width = 200;
                form.Height = sf.NumFields * 30 + 90;
                form.Text = "标题:" + "属性信息";
                form.ShowInTaskbar = false;
                form.StartPosition = FormStartPosition.CenterParent;
                form.FormBorderStyle = FormBorderStyle.FixedDialog;
                form.MaximizeBox = false;
                form.MinimizeBox = false;
                form.ShowDialog(axMap1.Parent);
                //   MessageBox.Show(s, "属性信息", MessageBoxButtons.OKCancel);
                // m_label.Text = s;
            }
            // sf.StartEditingTable(null);
            //sf.StopEditingShapes(true, true, null);
        }
        //按钮设置
        void BtnClick(object sender, EventArgs e)
        {
            Form form = (sender as Control).Parent as Form;
            if (form == null) return;
            Button btn = sender as Button;
            if (btn != null)
            {
                int layerHandle = axMap1.get_LayerHandle(0);
                Shapefile sf = axMap1.get_Shapefile(layerHandle);
                if (sf != null)
                {
                    if (btn.Text == "Ok")
                    {
                        // now we shall find the selected shape, the one being edited
                        // in real-world application would be better of course to store the index of this shape in private variable
                        int shapeIndex = -1;
                        for (int i = 0; i < sf.NumShapes; i++)
                        {
                            if (sf.ShapeSelected[i])
                            {
                                shapeIndex = i;
                                break;
                            }
                        }
                        if (shapeIndex != -1)
                        {
                            foreach (Control control in form.Controls)
                            {
                                if (control is TextBox)
                                {
                                    int fieldIndex = sf.Table.FieldIndexByName[control.Name];
                                    if (fieldIndex != -1)
                                        sf.EditCellValue(fieldIndex, shapeIndex, control.Text);
                                }
                            }
                            sf.Labels.Expression = sf.Labels.Expression;    // update the labels
                            axMap1.Redraw();
                        }
                    }
                }
            }
            form.Close();
        }

在这里插入图片描述
二.利用textbox控件显示属性(该方法比较简单)

public Form1()
        {
            InitializeComponent();
            showattribut();//展示信息
}
 public void showattribut()
        {
            axMap1.SendMouseDown = true;
            axMap1.CursorMode = tkCursorMode.cmIdentify;
            axMap1.ShapeIdentified += axmapshapeidentified;
        }
 void axmapshapeidentified(object sender, _DMapEvents_ShapeIdentifiedEvent e)
        {
            object value = sf.get_CellValue(0, e.shapeIndex);
            if (value != null)
            {
                label12_ID.Text = sf.get_CellValue(0, e.shapeIndex).ToString();
                label10_NAME.Text = sf.get_CellValue(1, e.shapeIndex).ToString();
                label10_LNG.Text = sf.get_CellValue(2, e.shapeIndex).ToString();
                label12_LAT.Text = sf.get_CellValue(3, e.shapeIndex).ToString();
                label9_TYPE.Text = sf.get_CellValue(4, e.shapeIndex).ToString();
                label8_MODEL.Text = sf.get_CellValue(5, e.shapeIndex).ToString();
            }
        }

在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值