Message 1 of 2
ContextMenu from Statusbar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I'm developing for AutoCAD Architecture 2010 in .NET 3.5, and noticed that they updated the object model (since ACA 2008) with the DisplayContextMenu method of the StatusbarItem object. I managed to create a System.Windows.Forms.ContextMenu and attach it to the StatusbarItem, and can get the ContextMenu to pop up, but I can't figure out how to get the event handler to fire on an individual MenuItem. I also tried it on vanilla AutoCAD 2010, and can't get it to work there either.
Here's the code for my CustomPanes class (I've modified the code so you don't have to filter out irrelevant methods). The custom panes get created by calling the InsertCustomPanes method.
{code}
Imports System
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Interop
Imports Autodesk.AutoCAD.Windows
Imports AcApp = Autodesk.AutoCAD.ApplicationServices
Imports Forms = System.Windows.Forms
Imports Exception = System.Exception
Namespace S2
Public Class CustomPanes
Private Shared _pDrawingUnits As Pane()
Friend Shared WithEvents _cmDrawingUnits As Forms.ContextMenu = New Forms.ContextMenu()
Protected Friend Shared ReadOnly Property DrawingUnits() As Pane()
Get
Return _pDrawingUnits
End Get
End Property
' helper function for creating context menu items
Private Shared Sub AddDwgUnitsContextMenuItem(ByVal strCaption As String, Optional ByVal blnChecked As Boolean = False)
Dim miItem As Forms.MenuItem = New Forms.MenuItem
miItem.Text = strCaption
miItem.Checked = blnChecked
_cmDrawingUnits.MenuItems.Add(miItem)
AddHandler miItem.Click, AddressOf OnDwgUnitsMenuItemClick ' should add event handler to MenuItem
End Sub
Private Shared Function CreateDrawingUnitsPanes() As Pane()
Dim pSpacer As Pane = New Pane()
Dim pUnits As Pane = New Pane()
Dim pReturn(1) As Pane
With pUnits
.Enabled = True
.Visible = True
.Text = "Unknown"
.ToolTipText = "Drawing Units"
.Style = PaneStyles.PopUp
AddHandler .MouseDown, New StatusBarMouseDownEventHandler(AddressOf OnDwgUnitsMouseDown)
End With
pReturn(0) = pUnits
With pSpacer
.Enabled = False
.Visible = True
.Text = ""
.Style = PaneStyles.NoBorders
End With
pReturn(1) = pSpacer
Return pReturn
End Function
Protected Friend Shared Sub InsertCustomPanes()
Dim intStatusBar As Integer = AcApp.Application.GetSystemVariable("STATUSBAR")
Dim sbApp As StatusBar = AcApp.Application.StatusBar
If _pDrawingUnits Is Nothing Then
_pDrawingUnits = CreateDrawingUnitsPanes()
End If
' add panes to status bars
InsertDrawingUnitsPanes(AcApp.Application.DocumentManager.MdiActiveDocument.StatusBar, _pDrawingUnits)
End Sub
Private Shared Sub InsertDrawingUnitsPanes(ByVal sbItem As StatusBar, ByVal pPanes As Pane())
Dim blnFound As Boolean
Dim intIndex As Integer
Dim pTemp As Pane
' find index of "Lock/Unlock Viewport" pane on application statusbar
For Each pTemp In sbItem.Panes
If pTemp.ToolTipText = "Lock/Unlock Viewport" Then
intIndex = sbItem.Panes.IndexOf(pTemp)
blnFound = True
Exit For
End If
Next
If blnFound Then
' check if pane before "Lock/Unlock Viewport" pane is Drawing Units pane
' if not, insert it
pTemp = sbItem.Panes(intIndex - 1)
If Not pTemp.ToolTipText = "Drawing Units" Then
For intI = pPanes.Count - 1 To 0 Step -1
sbItem.Panes.Insert(intIndex, pPanes(intI))
Next
End If
End If
End Sub
Protected Friend Shared Sub OnDwgUnitsMenuItemClick(ByVal sender As Object, ByVal e As EventArgs)
Dim edCmdLine As Editor = AcApp.Application.DocumentManager.MdiActiveDocument.Editor
Dim miItem As Forms.MenuItem = CType(sender, Forms.MenuItem)
edCmdLine.WriteMessage("Units changed to: " & miItem.Text)
End Sub
Protected Friend Shared Sub OnDwgUnitsMouseDown(ByVal sender As Object, ByVal e As StatusBarMouseDownEventArgs)
Dim sbItem As StatusBarItem = CType(sender, StatusBarItem)
_cmDrawingUnits.MenuItems.Clear()
AddDwgUnitsContextMenuItem("Inches")
AddDwgUnitsContextMenuItem("Feet")
AddDwgUnitsContextMenuItem("Millimeters", True) ' hardcode the checked status for testing purposes
AddDwgUnitsContextMenuItem("Centimeters")
AddDwgUnitsContextMenuItem("Decimeters")
AddDwgUnitsContextMenuItem("Meters")
sbItem.DisplayContextMenu(_cmDrawingUnits, New System.Drawing.Point(e.X, e.Y))
End Sub
End Class
End Namespace
{code}
Here's the code for my CustomPanes class (I've modified the code so you don't have to filter out irrelevant methods). The custom panes get created by calling the InsertCustomPanes method.
{code}
Imports System
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Interop
Imports Autodesk.AutoCAD.Windows
Imports AcApp = Autodesk.AutoCAD.ApplicationServices
Imports Forms = System.Windows.Forms
Imports Exception = System.Exception
Namespace S2
Public Class CustomPanes
Private Shared _pDrawingUnits As Pane()
Friend Shared WithEvents _cmDrawingUnits As Forms.ContextMenu = New Forms.ContextMenu()
Protected Friend Shared ReadOnly Property DrawingUnits() As Pane()
Get
Return _pDrawingUnits
End Get
End Property
' helper function for creating context menu items
Private Shared Sub AddDwgUnitsContextMenuItem(ByVal strCaption As String, Optional ByVal blnChecked As Boolean = False)
Dim miItem As Forms.MenuItem = New Forms.MenuItem
miItem.Text = strCaption
miItem.Checked = blnChecked
_cmDrawingUnits.MenuItems.Add(miItem)
AddHandler miItem.Click, AddressOf OnDwgUnitsMenuItemClick ' should add event handler to MenuItem
End Sub
Private Shared Function CreateDrawingUnitsPanes() As Pane()
Dim pSpacer As Pane = New Pane()
Dim pUnits As Pane = New Pane()
Dim pReturn(1) As Pane
With pUnits
.Enabled = True
.Visible = True
.Text = "Unknown"
.ToolTipText = "Drawing Units"
.Style = PaneStyles.PopUp
AddHandler .MouseDown, New StatusBarMouseDownEventHandler(AddressOf OnDwgUnitsMouseDown)
End With
pReturn(0) = pUnits
With pSpacer
.Enabled = False
.Visible = True
.Text = ""
.Style = PaneStyles.NoBorders
End With
pReturn(1) = pSpacer
Return pReturn
End Function
Protected Friend Shared Sub InsertCustomPanes()
Dim intStatusBar As Integer = AcApp.Application.GetSystemVariable("STATUSBAR")
Dim sbApp As StatusBar = AcApp.Application.StatusBar
If _pDrawingUnits Is Nothing Then
_pDrawingUnits = CreateDrawingUnitsPanes()
End If
' add panes to status bars
InsertDrawingUnitsPanes(AcApp.Application.DocumentManager.MdiActiveDocument.StatusBar, _pDrawingUnits)
End Sub
Private Shared Sub InsertDrawingUnitsPanes(ByVal sbItem As StatusBar, ByVal pPanes As Pane())
Dim blnFound As Boolean
Dim intIndex As Integer
Dim pTemp As Pane
' find index of "Lock/Unlock Viewport" pane on application statusbar
For Each pTemp In sbItem.Panes
If pTemp.ToolTipText = "Lock/Unlock Viewport" Then
intIndex = sbItem.Panes.IndexOf(pTemp)
blnFound = True
Exit For
End If
Next
If blnFound Then
' check if pane before "Lock/Unlock Viewport" pane is Drawing Units pane
' if not, insert it
pTemp = sbItem.Panes(intIndex - 1)
If Not pTemp.ToolTipText = "Drawing Units" Then
For intI = pPanes.Count - 1 To 0 Step -1
sbItem.Panes.Insert(intIndex, pPanes(intI))
Next
End If
End If
End Sub
Protected Friend Shared Sub OnDwgUnitsMenuItemClick(ByVal sender As Object, ByVal e As EventArgs)
Dim edCmdLine As Editor = AcApp.Application.DocumentManager.MdiActiveDocument.Editor
Dim miItem As Forms.MenuItem = CType(sender, Forms.MenuItem)
edCmdLine.WriteMessage("Units changed to: " & miItem.Text)
End Sub
Protected Friend Shared Sub OnDwgUnitsMouseDown(ByVal sender As Object, ByVal e As StatusBarMouseDownEventArgs)
Dim sbItem As StatusBarItem = CType(sender, StatusBarItem)
_cmDrawingUnits.MenuItems.Clear()
AddDwgUnitsContextMenuItem("Inches")
AddDwgUnitsContextMenuItem("Feet")
AddDwgUnitsContextMenuItem("Millimeters", True) ' hardcode the checked status for testing purposes
AddDwgUnitsContextMenuItem("Centimeters")
AddDwgUnitsContextMenuItem("Decimeters")
AddDwgUnitsContextMenuItem("Meters")
sbItem.DisplayContextMenu(_cmDrawingUnits, New System.Drawing.Point(e.X, e.Y))
End Sub
End Class
End Namespace
{code}