首页 / 浏览问题 / 组件GIS / 问题详情
怎么在workspacetree的数据源节点添加右键菜单,节点名怎么确定?
3EXP 2021年02月02日
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using SuperMap.Data;     //引入SuperMap控件
using SuperMap.Mapping;
using SuperMap.UI;

namespace Spatial_Data_management
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            workspaceTree.Workspace = workspace1;
        }


        //打开工作空间
        private void mnuopenWks_Click_1(object sender, EventArgs e)
        {
            this.openFileDialog.Title = "打开工作空间";
            this.openFileDialog.FileName = "";
            this.openFileDialog.Filter = "工作空间文件(*.smwu)|*.smwu|工作空间文件(*.sxwu)|*.sxwu";
            if (this.openFileDialog.ShowDialog() == DialogResult.OK)
            {
                workspace1.Close();
                string fileName = openFileDialog.FileName;
                WorkspaceConnectionInfo wsinfo = new WorkspaceConnectionInfo(fileName);
                workspace1.Open(wsinfo);
                workspaceTree.Refresh();
            }
        }

        //保存工作空间
        private void mnusaveWks_Click_1(object sender, EventArgs e)
        {
            if (workspace1.Save())
            {
                MessageBox.Show("工作空间保存成功!", "提示");
            }
            else
            {
                MessageBox.Show("工作空间保存失败!", "提示");
                return;
            }
        }

        //另存工作空间
        private void mnusaveWksAs_Click_1(object sender, EventArgs e)
        {
            this.saveFileDialog1.Title = "保存工作空间";
            this.saveFileDialog1.Filter = "工作空间文件(*.smwu)|*.smwu|工作空间文件(*.sxwu)|*.sxwu";
            if (this.saveFileDialog1.FileName != null && this.saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string name = System.IO.Path.GetFileNameWithoutExtension(this.saveFileDialog1.FileName);
                WorkspaceConnectionInfo conInfo = new WorkspaceConnectionInfo();
                conInfo.Server = this.saveFileDialog1.FileName;
                conInfo.Password = "";
                if (this.saveFileDialog1.FilterIndex == 1)
                {
                    conInfo.Type = WorkspaceType.SMWU;
                }
                else
                {
                    conInfo.Type = WorkspaceType.SXWU;
                }
                workspace1.Caption = name;
                if (workspace1.SaveAs(conInfo))
                {
                    MessageBox.Show("工作空间另存成功!");
                }
                else
                {
                    MessageBox.Show("另存工作空间失败!", "提示");
                    return;
                }
            }
        }
       
        //创建数据源
        private void mnucreateDS_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Title = "新建文件型数据源";
            saveFileDialog.CheckPathExists = true;
            saveFileDialog.AddExtension = true;
            saveFileDialog.Filter = "文件型数据库(*.udb)|*.udb";
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                String name = Path.GetFileNameWithoutExtension(saveFileDialog.FileName);
                for (int m = 0; m < workspace1.Datasources.Count; m++)
                {
                    bool test = (String.Compare(name, workspace1.Datasources[m].Alias) == 0);
                    if (test)
                    {
                        MessageBox.Show("指定数据源名称已存在!");
                        return;
                    }
                }
                DatasourceConnectionInfo conInfo = new DatasourceConnectionInfo(name, name, "");
                conInfo.Server = saveFileDialog.FileName;
                workspace1.Datasources.Create(conInfo);
            }
        }
        //打开文件型数据源
        private void mnuopenFileDS_Click_1(object sender, EventArgs e)
        {
            if (workspace1 != null)
            {
                OpenFileDialog openFileDialog1= new OpenFileDialog();
                openFileDialog1.Filter = "文件型数据源(*.udb)|*.udb";
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    DatasourceConnectionInfo m_datasourceInfo = new DatasourceConnectionInfo();
                    m_datasourceInfo.Server = openFileDialog1.FileName;
                    m_datasourceInfo.Alias = System.IO.Path.GetFileNameWithoutExtension(openFileDialog.FileName);
                    bool test = workspace1.Datasources.Contains(m_datasourceInfo.Alias);
                    if (test)
                    {
                        MessageBox.Show("该数据源已打开!");
                        return;
                    }
                    m_datasourceInfo.Password = "";
                    m_datasourceInfo.EngineType = EngineType.UDB;
                    workspace1.Datasources.Open(m_datasourceInfo);
                    workspaceTree.Refresh();
                }
            }
        }
        //打开数据库型数据源
        private void mnuopenDBDS_Click_1(object sender, EventArgs e)
        {
            if (workspace1 != null)
            {
                DatasourceConnectionInfo info = new DatasourceConnectionInfo();
                info.EngineType = EngineType.PostgreSQL;
                info.Server = "localhost";
                info.Database = "jiangsu";
                info.User = "postgres";
                info.Password = "111111";
                workspace1.Datasources.Open(info);
                workspaceTree.Refresh();
            }
        }
        //关闭数据源
        private void mnucloseDS_Click_1(object sender, EventArgs e)
        {
            if (workspaceTree.SelectedNode.Parent == workspaceTree.DatasourcesNode)
            {
                workspace1.Datasources[workspaceTree.SelectedNode.Text].Close();
                workspaceTree.Refresh();
            }
            else
            {
                MessageBox.Show("请先选中要关闭的数据源!");
            }
        }
        // 工作空间树节点添加右键菜单函数
        private void CreateContextMenuStrip()
        {
            // 为工作空间树中的矢量数据集节点指定一个右键菜单
            workspaceTree.NodeContextMenuStrips[WorkspaceTreeNodeDataType.DatasetVector] =??

        }
    

       
    }
}

1个回答

当您进行右键点击时,该wrokspacetree的SelectedNodes成员将获得选中的树节点集合。 

10,513EXP 2021年02月03日
...