- 积分
- 9268
- 明经币
- 个
- 注册时间
- 2004-6-10
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
本帖最后由 pmq 于 2025-4-19 15:48 编辑
VB.NET [源码] CAD顶部下拉菜单,可以自定义配置文件
停靠面板 侧边栏菜单
开发环境 Microsoft Visual Studio 2022, AutoCAD2026
也可以用其它版本,只是引用对应的CAD的三个运行库
accoremgd、acdbmgd、acmgd
抖音搜索用户 GCPX515 查看使用视频:
2024.11.08更新
 - Imports Autodesk.AutoCAD.Runtime
- Imports Autodesk.AutoCAD.Windows
- Imports System.IO
- Imports System.Windows.Forms
- Public Class NestedToolbarMenu
- ' 定义命令和宏的配置类
- Public Class CommandConfig
- Public Property Title As String
- Public Property Macro As String
- Public Property SubCommands As List(Of CommandConfig) ' 子菜单
- End Class
- ' 主方法:创建嵌套菜单形式的 PaletteSet
- <CommandMethod("CreateNestedToolbar")>
- Public Sub CreateNestedToolbar()
- ' 配置文件路径
- Dim filePath As String = "C:\Asur\System\commands.txt"
- Dim commands As List(Of CommandConfig) = LoadNestedCommandsFromTextFile(filePath)
- If commands Is Nothing OrElse commands.Count = 0 Then
- MsgBox("未在配置文件中找到有效命令。")
- Exit Sub
- End If
- ' 创建 PaletteSet(可停靠窗口)
- Dim paletteSet As New PaletteSet("Custom Toolbar") With {
- .Style = PaletteSetStyles.ShowPropertiesMenu Or PaletteSetStyles.ShowAutoHideButton, ' 移除了无效的 Pinned 样式
- .MinimumSize = New Drawing.Size(1200, 25)
- }
- ' 创建一个 Panel 来容纳工具条
- Dim panel As New Panel() With {
- .Dock = DockStyle.Fill
- }
- ' 创建工具条
- Dim toolStrip As New ToolStrip() With {
- .Dock = DockStyle.Top
- }
- ' 遍历命令列表,添加到工具条
- For Each command In commands
- Dim dropDownButton As New ToolStripDropDownButton() With {
- .Text = command.Title
- }
- ' 如果有子命令,添加到下拉菜单中
- If command.SubCommands IsNot Nothing AndAlso command.SubCommands.Count > 0 Then
- For Each subCommand In command.SubCommands
- Dim subButton As New ToolStripMenuItem() With {
- .Text = subCommand.Title
- }
- ' 设置子命令的点击事件
- AddHandler subButton.Click, Sub(sender, e)
- Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.SendStringToExecute(subCommand.Macro & vbCr, True, False, False)
- End Sub
- dropDownButton.DropDownItems.Add(subButton)
- Next
- End If
- toolStrip.Items.Add(dropDownButton)
- Next
- ' 将工具条添加到 Panel
- panel.Controls.Add(toolStrip)
- ' 将 Panel 添加到 PaletteSet
- paletteSet.Add("Nested Toolbar", panel)
- paletteSet.Size = New Drawing.Size(1200, 25)
- paletteSet.Dock = DockSides.Top '面板停靠在窗口的顶部
- ' 显示 PaletteSet
- paletteSet.Visible = True
- End Sub
- ' 从文本文件加载嵌套命令配置
- Private Function LoadNestedCommandsFromTextFile(filePath As String) As List(Of CommandConfig)
- Dim commands As New List(Of CommandConfig)
- Dim currentParent As CommandConfig = Nothing
- Try
- If File.Exists(filePath) Then
- Dim lines = File.ReadAllLines(filePath)
- For Each line In lines
- ' 跳过空行和注释
- If String.IsNullOrWhiteSpace(line) OrElse line.StartsWith("#") Then Continue For
- ' 判断是否是子命令(通过缩进)
- If line.StartsWith(" ") Then ' 检测 4 个空格的缩进
- ' 子命令
- Dim parts = line.Trim().Split("|"c)
- If parts.Length = 2 AndAlso currentParent IsNot Nothing Then
- currentParent.SubCommands.Add(New CommandConfig() With {
- .Title = parts(0).Trim(),
- .Macro = parts(1).Trim()
- })
- End If
- Else
- ' 主命令
- Dim parts = line.Split("|"c)
- If parts.Length = 2 Then
- Dim parentCommand As New CommandConfig() With {
- .Title = parts(0).Trim(),
- .Macro = parts(1).Trim(),
- .SubCommands = New List(Of CommandConfig)()
- }
- commands.Add(parentCommand)
- currentParent = parentCommand
- End If
- End If
- Next
- Else
- MsgBox("未找到配置文件:" & filePath)
- End If
- Catch ex As Exception
- MsgBox("读取配置文件时出错:" & ex.Message)
- End Try
- Return commands
- End Function
- End Class
- 'commands.txt
- 配置文件格式
- '===========================
- # 主菜单: 绘图
- 绘图|
- 绘直线 Line|_.LINE
- 展点 ZD|_.POINT
- # 主菜单: 编辑
- 编辑|
- 打断 Br|_.BREAK
- 分解 EX|_.EXPLODE
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?注册
x
评分
-
查看全部评分
|