运行环境 vs2022 c# cad2016 调试成功
一、代码说明
1. 类成员变量声明:
List<ObjectId> objectIds = new List<ObjectId>(); // 用于存储AutoCAD实体对象的ObjectId列表
private static Autodesk.AutoCAD.Windows.PaletteSet _ps2; // 自定义浮动面板集实例
private CustomPaletteControl _customCtrl; // 定制控件实例,包含ListBox及事件处理程序
private static System.Windows.Forms.Panel panel; // 面板容器
// 其他已导入命名空间下的类型(略)
2. CreatePalette() 方法: 这个方法负责创建和配置自定义浮动面板。
- 创建并初始化一个 PaletteSet 实例,设置其名称和最小尺寸。
- 创建文本框
textBox
并设置其属性如是否多行、位置、大小等。 - 创建按钮
button1
,设置文本、位置、大小,并为 Click 事件绑定Button1_Click
处理程序。 - 初始化或复用
_customCtrl
控件,并将其添加到 Panel 中。 - 将文本框、按钮和自定义控件添加至 Panel 中。
- 将 Panel 添加到 PaletteSet 的指定区域,并显示整个 PaletteSet。
// 声明成员变量:存储对象ID的列表
List<ObjectId> objectIds = new List<ObjectId>();
// 创建PaletteSet实例
private static Autodesk.AutoCAD.Windows.PaletteSet _ps2;
// 创建CustomPaletteControl实例(假设这是一个包含ListBox的自定义控件)
private CustomPaletteControl _customCtrl;
// 创建Panel容器实例
private static System.Windows.Forms.Panel panel;
// 创建并配置自定义浮动面板的方法
public void CreatePalette()
{
// 初始化 PaletteSet,并设置其名称和最小尺寸
_ps2 = new PaletteSet("我的窗体");
_ps2.MinimumSize = new System.Drawing.Size(300, 300);
// 创建并配置文本框控件
TextBox textBox = new TextBox();
textBox.Multiline = false;
textBox.Location = new Point(10, 10);
textBox.Size = new Size(240, 20);
textBox.Text = "403";
// 创建并配置按钮控件
Button button1 = new Button();
button1.Text = "查找";
button1.Location = new Point(10, 40);
button1.Size = new Size(80, 25);
// 给按钮添加Click事件处理程序
button1.Click += new EventHandler(Button1_Click);
// 初始化或复用_customCtrl,并设置位置与大小
if (_customCtrl == null)
{
_customCtrl = new CustomPaletteControl(ListBoxItemSelected);
}
_customCtrl.Location = new Point(10, 70);
_customCtrl.Size = new Size(280, 250);
// 示例性地向ListBox添加一个项目
_customCtrl.ListBox1.Items.Add(new CommandItem("00", "00"));
// 创建Panel并添加控件
System.Windows.Forms.Panel localPanel = new System.Windows.Forms.Panel(); // 注意这里的panel是局部变量
localPanel.Controls.Add(textBox