.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

ContextMenu from Statusbar

1 REPLY 1
Reply
Message 1 of 2
s.jabs
1130 Views, 1 Reply

ContextMenu from Statusbar

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}
1 REPLY 1
Message 2 of 2
Anonymous
in reply to: s.jabs

Just a little bump...  I have the exact same problem with AutoCAD 2011 and c#.

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Windows;
using System.Drawing;

namespace CAD_Integrator
{
    class clsStatusBar
    {
        private Icon mCADiIconObj = new System.Drawing.Icon("D:\\Icons\\ico-files\\ix_ap_all_add2.ico");

        private Pane mCADiPaneObj = null;
        private TrayItem mCADiTrayItem = null;
        private TrayItemBubbleWindow mCADiTrayItemBubble = null;

        private System.Windows.Forms.ContextMenu mLabelContextMenu = null;
        
        private System.Windows.Forms.MenuItem mMenuItem_ToggleCatalogueLabels = null;
        private System.Windows.Forms.MenuItem mMenuItem_ToggleOrgLabels = null;
        private System.Windows.Forms.MenuItem mMenuItem_ToggleRoomLabels = null;

        //private Icon mMenuItemIcon_ToggleCatalogueLabels = new System.Drawing.Icon("Images\\CreateCatalogueLabels_L.bmp");
        //private Icon mMenuItemIcon_ToggleOrgLabels = new System.Drawing.Icon("Images\\CreateOrganizationLabels_L.bmp");
        //private Icon mMenuItemIcon_ToggleRoomLabels = new System.Drawing.Icon("Images\\CreateRoomLabels_L.bmp");

        public clsStatusBar()
        {
            //*******************************************************
            mCADiPaneObj = new Pane();
            mCADiPaneObj.Text = "CAD Integrator";
            mCADiPaneObj.ToolTipText = "CAD Integrator";
            mCADiPaneObj.Style = PaneStyles.NoBorders;
            mCADiPaneObj.Icon = mCADiIconObj;
            //*******************************************************
            mCADiPaneObj.MouseDown += new StatusBarMouseDownEventHandler(vMyPaneObj_MouseDown);
            //*******************************************************
            Application.StatusBar.Panes.Add(mCADiPaneObj);
            //*******************************************************

            //*******************************************************
            /*
            mMenuItem_ToggleCatalogueLabels = new System.Windows.Forms.MenuItem("Display Catalogue Labels");
            mMenuItem_ToggleCatalogueLabels
            mMenuItem_ToggleOrgLabels = new System.Windows.Forms.MenuItem("Display Organization Labels");
            mMenuItem_ToggleOrgLabels.Click += new EventHandler(mMenuItem_ToggleOrgLabels_Click);
            mMenuItem_ToggleRoomLabels = new System.Windows.Forms.MenuItem("Display Room Labels");
            mMenuItem_ToggleRoomLabels.Click += new EventHandler(mMenuItem_ToggleRoomLabels_Click);
             * */
            //*******************************************************
            mLabelContextMenu = new System.Windows.Forms.ContextMenu();
            mMenuItem_ToggleCatalogueLabels = mLabelContextMenu.MenuItems.Add("Display Catalogue Labels");
            mMenuItem_ToggleCatalogueLabels.Click += new EventHandler(mMenuItem_ToggleCatalogueLabels_Click);
            mMenuItem_ToggleCatalogueLabels.Select += new EventHandler(mMenuItem_ToggleCatalogueLabels_Click);
            mMenuItem_ToggleCatalogueLabels.Disposed += new EventHandler(mMenuItem_ToggleCatalogueLabels_Click);
            mMenuItem_ToggleOrgLabels = mLabelContextMenu.MenuItems.Add("Display Organization Labels");
            mMenuItem_ToggleOrgLabels.Click += new EventHandler(mMenuItem_ToggleOrgLabels_Click);
            mMenuItem_ToggleRoomLabels = mLabelContextMenu.MenuItems.Add("Display Room Labels");
            mMenuItem_ToggleRoomLabels.Click += new EventHandler(mMenuItem_ToggleRoomLabels_Click);
            //*******************************************************

            //*******************************************************
            mCADiTrayItem = new TrayItem();
            mCADiTrayItem.ToolTipText = "CADi Tray Item";
            mCADiTrayItem.Icon = mCADiIconObj;
            //*******************************************************
            mCADiTrayItem.MouseDown += new StatusBarMouseDownEventHandler(vMyTrayItem_MouseDown);
            //*******************************************************
            Application.StatusBar.TrayItems.Add(mCADiTrayItem);
            //*******************************************************

            //*******************************************************
            mCADiTrayItemBubble = new TrayItemBubbleWindow();
            mCADiTrayItemBubble.Text = "CADi Bubble Text";
            mCADiTrayItemBubble.Title = "CADi Bubble Title";
            mCADiTrayItemBubble.HyperLink = "http://adn.autodesk.com";
            mCADiTrayItemBubble.HyperText = "CADi Hyperlink";
            //*******************************************************
            mCADiTrayItemBubble.Closed += new TrayItemBubbleWindowClosedEventHandler(vMyTrayItemBubble_Closed);
            //*******************************************************
            mCADiTrayItem.ShowBubbleWindow(mCADiTrayItemBubble);
            //*******************************************************

        }

        private void vMyTrayItemBubble_Closed(object sender, TrayItemBubbleWindowClosedEventArgs args)
        {

        }

        private void vMyTrayItem_MouseDown(object sender, StatusBarMouseDownEventArgs args)
        {

        }

        private void vMyPaneObj_MouseDown(object sender, StatusBarMouseDownEventArgs args)
        {
            mCADiPaneObj.DisplayContextMenu(mLabelContextMenu, new Point(args.X, args.Y));
        }

        private void mMenuItem_ToggleCatalogueLabels_Click(object sender, EventArgs args)
        {
            System.Windows.Forms.MenuItem vMI = (System.Windows.Forms.MenuItem)sender;
            vMI.Checked = !vMI.Checked;
            //(System.Windows.Forms.MenuItem)sender.Checked = !mMenuItem_ToggleCatalogueLabels.Checked;
        }

        private void mMenuItem_ToggleOrgLabels_Click(object sender, EventArgs args)
        {
            //mMenuItem_ToggleOrgLabels.Checked = !mMenuItem_ToggleOrgLabels.Checked;
        }

        private void mMenuItem_ToggleRoomLabels_Click(object sender, EventArgs args)
        {
            //mMenuItem_ToggleRoomLabels.Checked = !mMenuItem_ToggleRoomLabels.Checked;
        }
    }
}

Anyone has an ideas?

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report