Private _filter As MenuMsgFilter
<CommandMethod("TrayTest")> _
Public Sub TrayTest()
Autodesk.AutoCAD.ApplicationServices.Application.StatusBar.TrayItems.Add(Me.TrayItem)
Autodesk.AutoCAD.ApplicationServices.Application.StatusBar.Update()
_filter = New MenuMsgFilter
System.Windows.Forms.Application.AddMessageFilter(_filter)
End Sub
'<DebuggerBrowsable(DebuggerBrowsableState.Never)> _
Private m_TrayItem As Autodesk.AutoCAD.Windows.TrayItem = Nothing
Public ReadOnly Property TrayItem() As Autodesk.AutoCAD.Windows.TrayItem
Get
If Me.m_TrayItem Is Nothing Then
Me.m_TrayItem = New Autodesk.AutoCAD.Windows.TrayItem
Me.m_TrayItem.Icon = System.Drawing.SystemIcons.Asterisk
Me.m_TrayItem.ToolTipText = "Testing"
Me.m_TrayItem.Visible = True
AddHandler m_TrayItem.MouseDown, AddressOf callback_MouseDown
End If
Return Me.m_TrayItem
End Get
End Property
<StructLayout(LayoutKind.Sequential)> _
Private Structure MENUITEMINFO
Public cbSize As Integer
Public fMask As Integer
Public fType As Integer
Public fState As Integer
Public wID As Integer
Public hSubMenu As IntPtr
Public hbmpChecked As IntPtr
Public hbmpUnchecked As IntPtr
Public dwItemData As IntPtr
Public dwTypeData As String
Public cch As Integer
Public hbmpItem As IntPtr
End Structure
<DllImport("user32.dll")> _
Private Shared Function GetMenuItemInfo( _
ByVal hMenu As IntPtr, _
ByVal uItem As UInteger, _
ByVal fByPosition As Boolean, _
ByRef lpmii As MENUITEMINFO) As Boolean
End Function
'<DebuggerBrowsable(DebuggerBrowsableState.Never)> _
Private m_ContextMenu As System.Windows.Forms.ContextMenu = Nothing
Private m_MenuIds
As Dictionary(Of Integer,
String)
Private ReadOnly Property ContextMenu() As System.Windows.Forms.ContextMenu
Get
If Me.m_ContextMenu Is Nothing Then
Me.m_ContextMenu = New System.Windows.Forms.ContextMenu
Dim MenuItem1 As New System.Windows.Forms.MenuItem("MenuItem1")
Dim MenuItem2 As New System.Windows.Forms.MenuItem("MenuItem2...")
Dim MenuItem3 As New System.Windows.Forms.MenuItem("MenuItem3...")
'AddHandler MenuItem1.Click, AddressOf MenuItem1_Click
'AddHandler MenuItem2.Click, AddressOf MenuItem2_Click
'AddHandler MenuItem3.Click, AddressOf MenuItem3_Click
Me.m_ContextMenu.MenuItems.Add(MenuItem1)
Me.m_ContextMenu.MenuItems.Add(MenuItem2)
Me.m_ContextMenu.MenuItems.Add(New System.Windows.Forms.MenuItem("-"))
Me.m_ContextMenu.MenuItems.Add(MenuItem3)
Dim mif As MENUITEMINFO = New MENUITEMINFO()
mif.cbSize = Marshal.SizeOf(mif)
mif.fMask = &H2 'MIIM_ID
mif.fType = &H100
mif.cch = 256
GetMenuItemInfo(Me.m_ContextMenu.Handle, 0, True, mif)
m_MenuIds.Add(mif.wID, "MenuItem[1] Clicked!")
GetMenuItemInfo(Me.m_ContextMenu.Handle, 1, True, mif)
m_MenuIds.Add(mif.wID, "MenuItem[2] Clicked!")
GetMenuItemInfo(Me.m_ContextMenu.Handle, 3, True, mif)
m_MenuIds.Add(mif.wID, "MenuItem[3] Clicked!")
_filter.MenuIds = m_MenuIds
End If
Return Me.m_ContextMenu
End Get
End Property
Private Sub callback_MouseDown(ByVal sender As Object, ByVal e As Autodesk.AutoCAD.Windows.StatusBarMouseDownEventArgs)
Try
Select Case e.Button
Case Windows.Forms.MouseButtons.Left
WriteMsg("left button")
Case Windows.Forms.MouseButtons.Right
Dim ti As Autodesk.AutoCAD.Windows.TrayItem = CType(sender, Autodesk.AutoCAD.Windows.TrayItem)
ti.DisplayContextMenu(Me.ContextMenu, New System.Drawing.Point(e.X, e.Y))
End Select
Catch ex As System.Exception
End Try
End Sub
'Private Sub MenuItem1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
' WriteMsg("MenuItem1")
'End Sub
'Private Sub MenuItem2_Click(ByVal sender As Object, ByVal e As System.EventArgs)
' WriteMsg("MenuItem2")
'End Sub
'Private Sub MenuItem3_Click(ByVal sender As Object, ByVal e As System.EventArgs)
' WriteMsg("MenuItem3")
'End Sub
Private Sub WriteMsg(ByVal msg As String)
Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
doc.Editor.WriteMessage(Environment.NewLine & msg)
End Sub
Private Class MenuMsgFilter
Implements System.Windows.Forms.IMessageFilter
Private m_MenuIds
As Dictionary(Of Integer,
String)
Public Property MenuIds
() As Dictionary(Of Integer,
String) Get
Return m_MenuIds
End Get
Set(ByVal value
As Dictionary(Of Integer,
String)) m_MenuIds = value
End Set
End Property
Private Function LoWord(ByVal word As Integer) As Short
Return CShort(word And Short.MaxValue)
End Function
Private Function HiWord(ByVal word As Integer) As Short
Return CShort((word >> 16))
End Function
Private WM_COMMAND As Integer = &H111
Public Function PreFilterMessage(ByRef m As System.Windows.Forms.Message) As Boolean _
Implements System.Windows.Forms.IMessageFilter.PreFilterMessage
If m.Msg = WM_COMMAND Then
If HiWord(m.WParam) = 0 Then
If m_MenuIds.ContainsKey(LoWord(m.WParam)) Then
Dim msg As String = m_MenuIds(LoWord(m.WParam))
Dim doc As Document = Application.DocumentManager.MdiActiveDocument
Dim ed As Editor = doc.Editor()
ed.WriteMessage(vbCrLf + msg)
Return False
End If
End If
End If
Return False
End Function
End Class