C# CAD交互界面-模态窗体与非模态窗体调用方式

运行环境Visual Studio 2022 c# cad2016

一、模态窗体调用方式

当一个模态窗体打开时,它会阻塞主窗体的所有输入,直到关闭该模态窗体为止。例如,弹出一个对话框让用户必须完成某些操作后才能继续使用主程序。

cs 复制代码
[CommandMethod("Caidan")]
public void Caidan()
{
     ShowModalLayerSelectionForm();//模态窗体

}
//模态窗体
 private void ShowModalLayerSelectionForm()
 {
     using (var layerForm = new Form1())
     {
         DialogResult result = layerForm.ShowDialog(); // 使用ShowDialog()方法打开模态窗体

         if (result == DialogResult.OK || result == DialogResult.Yes) // 根据需要处理结果
         {
             // 处理用户选择图层的操作...
         }
     }
 }

二、非模态窗体调用方式

非模态窗体打开时,用户可以同时与主窗体和其他非模态窗体进行交互。通常用作工具栏、辅助信息显示窗口等。

cs 复制代码
[CommandMethod("Caidan")]
public void Caidan()
{
    Form1 选择图层 = new Form1(); //非模态窗体
    选择图层.Show();
}

三、窗体程序

cs 复制代码
     this.MaximizeBox = false;
     this.MinimizeBox = false;
    // 初始化DataGridView
    DataGridView dataGridView = new DataGridView();// 创建一个新的DataGridView控件实例
    dataGridView.AllowUserToAddRows = false;// 禁止用户通过DataGridView界面直接添加新行
    dataGridView.AllowUserToDeleteRows = false;// 禁止用户通过DataGridView界面删除现有行
    dataGridView.BackgroundColor = Color.White;// 设置DataGridView的背景颜色为白色
    dataGridView.CellBorderStyle = DataGridViewCellBorderStyle.Single;// 设置单元格边框样式为单线边框
    dataGridView.DefaultCellStyle.SelectionBackColor = Color.LightBlue;// 设置选中单元格时的背景色为浅蓝色
   dataGridView.DefaultCellStyle.SelectionForeColor = Color.Black;// 设置选中单元格时的前景色(文本颜色)为黑色


    // 添加图层名称列
    DataGridViewTextBoxColumn layerNameColumn = new DataGridViewTextBoxColumn();
    layerNameColumn.HeaderText = "图层名称";
    layerNameColumn.ReadOnly = true;
    dataGridView.Columns.Add(layerNameColumn);

    // 假设GetLayerList返回CAD图层的名称列表
    //List<string> layerNames = GetLayerList();

    foreach (LayerTableRecord layer in GetLayerList())
    {
        dataGridView.Rows.Add(layer.Name);
    }

    // 添加DataGridView到窗体
    this.Controls.Add(dataGridView);
    dataGridView.Dock = DockStyle.Fill;
    dataGridView.CellDoubleClick += DataGridView_CellDoubleClick;
}

private void DataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
    if (sender is DataGridView dgv && e.RowIndex >= 0) // 检查是否在有效的行上双击
    {
        string selectedLayerName = dgv.Rows[e.RowIndex].Cells[0].Value.ToString(); // 获取选中的图层名称

        // 处理双击选定图层的逻辑
        MessageBox.Show($"双击了图层:{selectedLayerName}");

        // 根据需求执行其他操作...
    }
}


private IEnumerable<LayerTableRecord> GetLayerList()
{
    List<LayerTableRecord> layers = new List<LayerTableRecord>();

    using (Transaction tr = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction())
    {
        LayerTable layerTable = (LayerTable)tr.GetObject(HostApplicationServices.WorkingDatabase.LayerTableId, OpenMode.ForRead);

        foreach (ObjectId id in layerTable)
        {
            LayerTableRecord layer = (LayerTableRecord)tr.GetObject(id, OpenMode.ForRead);
            layers.Add(layer);
        }

        tr.Commit();
    }

    return layers;
}

//感谢大家的点赞,收藏,转发,关注

相关推荐
今天背单词了吗98018 分钟前
算法学习笔记:8.Bellman-Ford 算法——从原理到实战,涵盖 LeetCode 与考研 408 例题
java·开发语言·后端·算法·最短路径问题
CoderPractice24 分钟前
C#控制台小项目-飞行棋
开发语言·c#·小游戏·飞行棋
手握风云-28 分钟前
优选算法的链脉之韵:链表专题
数据结构·算法·链表
Coding小公仔32 分钟前
LeetCode 151. 反转字符串中的单词
开发语言·c++·算法
稳兽龙32 分钟前
P1098 [NOIP 2007 提高组] 字符串的展开
c++·算法·模拟
G.E.N.38 分钟前
开源!RAG竞技场(2):标准RAG算法
大数据·人工智能·深度学习·神经网络·算法·llm·rag
写个博客42 分钟前
暑假算法日记第三天
算法
ajassi20001 小时前
开源 C# .net mvc 开发(八)IIS Express轻量化Web服务器的配置和使用
linux·开源·c#·mvc·.net
✿ ༺ ོIT技术༻1 小时前
剑指offer第2版:动态规划+记忆化搜索
算法·动态规划·记忆化搜索
oioihoii2 小时前
C++11标准库算法:深入理解std::none_of
java·c++·算法