Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ContextMenu & NotifyIcon issue

Status
Not open for further replies.

PsychoCoder

Programmer
May 31, 2006
140
US
Hello all,

This is driving me crazy (not that its too long of a trip). I have created a custom NotifyIcon class file, as far as displaying the icon in the system tray and sending the balloon messages it works great. I actually have 2 class files in one, one is a listener the other is the actual NotifyIcon file.

In the Listener class I have added events:
Code:
Public Shadows Event Click(ByVal sender As Object, ByVal e As System.EventArgs)
Public Shadows Event DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs)
Public Shadows Event MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
Public Shadows Event MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
Public Shadows Event MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
Public Event Reload()
Public Event BalloonShow(ByVal sender As Object)
Public Event BalloonHide(ByVal sender As Object)
Public Event BalloonTimeout(ByVal sender As Object)
Public Event BalloonClick(ByVal sender As Object)
Private WM_TASKBARCREATED As Int32 = RegisterWindowMessage("TaskbarCreated")

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
   Select Case m.Msg
     Case WM_USER_TRAY
       Select Case m.LParam.ToInt32
     Case WM_LBUTTONDBLCLK, WM_RBUTTONDBLCLK, WM_MBUTTONDBLCLK
       RaiseEvent DoubleClick(Me, New MouseEventArgs(MouseButtons, 0, MousePosition.X, MousePosition.Y, 0))
     Case WM_LBUTTONDOWN, WM_RBUTTONDOWN, WM_MBUTTONDOWN
       RaiseEvent MouseDown(Me, New MouseEventArgs(MouseButtons, 0, MousePosition.X, MousePosition.Y, 0))
     Case WM_MOUSEMOVE
       RaiseEvent MouseMove(Me, New MouseEventArgs(MouseButtons, 0, MousePosition.X, MousePosition.Y, 0))
     Case WM_LBUTTONUP
       RaiseEvent MouseUp(Me, New MouseEventArgs(Windows.Forms.MouseButtons.Left, 0, MousePosition.X, MousePosition.Y, 0))
      RaiseEvent Click(Me, New MouseEventArgs(Windows.Forms.MouseButtons.Left, 0, MousePosition.X, MousePosition.Y, 0))
     Case WM_RBUTTONUP
       RaiseEvent MouseUp(Me, New MouseEventArgs(Windows.Forms.MouseButtons.Right, 0, MousePosition.X, MousePosition.Y, 0))
       RaiseEvent Click(Me, New MouseEventArgs(Windows.Forms.MouseButtons.Right, 0, MousePosition.X, MousePosition.Y, 0))
     Case WM_MBUTTONUP
       RaiseEvent MouseUp(Me, New MouseEventArgs(Windows.Forms.MouseButtons.Middle, 0, MousePosition.X, MousePosition.Y, 0))
       RaiseEvent Click(Me, New MouseEventArgs(Windows.Forms.MouseButtons.Middle, 0, MousePosition.X, MousePosition.Y, 0))
     Case NIN_BALLOONSHOW
        RaiseEvent BalloonShow(Me)
     Case NIN_BALLOONHIDE
        RaiseEvent BalloonHide(Me)
     Case NIN_BALLOONTIMEOUT
        RaiseEvent BalloonTimeout(Me)
     Case NIN_BALLOONUSERCLICK
        RaiseEvent BalloonClick(Me)
     Case Else
        Debug.WriteLine(m.LParam.ToInt32)
     End Select
   Case WM_TASKBARCREATED
     RaiseEvent Reload()
   Case Else
     End Select
  MyBase.WndProc(m)
 End Sub

In the actual NotifyIcon file I have a ContextMenu Property:
Code:
Public Property ContextMenu() As System.Windows.Forms.ContextMenuStrip
  Get
    Return m_ContextMenu
  End Get
  Set(ByVal Value As System.Windows.Forms.ContextMenuStrip)
    m_ContextMenu = Value
  End Set
End Property

And the Event Handlers:
Code:
Public Event Click(ByVal sender As Object, ByVal e As System.EventArgs)
    Public Event DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs)
    Public Event RightClick(ByVal sender As Object, ByVal e As System.EventArgs)
    Public Event MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
    Public Event MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
    Public Event MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
    Public Event BalloonShow(ByVal sender As Object)
    Public Event BalloonHide(ByVal sender As Object)
    Public Event BalloonTimeout(ByVal sender As Object)
    Public Event BalloonClick(ByVal sender As Object)
Private Shadows Sub Messages_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Messages.Click
        RaiseEvent Click(Me, e)
    End Sub

    Private Shadows Sub Messages_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Messages.DoubleClick
        RaiseEvent DoubleClick(Me, e)
    End Sub

    Private Shadows Sub Messages_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Messages.MouseDown
        RaiseEvent MouseDown(Me, e)
    End Sub

    Private Shadows Sub Messages_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Messages.MouseMove
        RaiseEvent MouseMove(Me, e)
    End Sub

    Private Shadows Sub Messages_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Messages.MouseUp
        RaiseEvent MouseUp(Me, e)

        If e.Button = MouseButtons.Right Then
            Messages.Activate()
            Dim Position As Point = New Point(Cursor.Position.X - Messages.PointToScreen(New Point(0, 0)).X, Cursor.Position.Y - Messages.PointToScreen(New Point(0, 0)).Y)
            If Not m_ContextMenu Is Nothing Then m_ContextMenu.Show(Messages, Position)
            'MsgBox("Set right-click!", MsgBoxStyle.OkOnly)
        End If
    End Sub

But in the application I cannot get the ContextMenuStrip to show when a right-click is execute. Any help would be greatly appreciated.

Senior Qik III, ASP.Net, VB.Net ,SQL Programmer

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"In the new millennium there will be two kinds of business; those on the net and those out of business"

~ Bill Gates ~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
Maybe I'm missing something, but nowhere do I see that you have your context menu assigned to the NotifyIcon.

Ron Repp

If gray hair is a sign of wisdom, then I'm a genius.
 
RonRepp,

In the main form's Load event I have
Code:
oIcon.AddIconToTray(Me)
oIcon.SendBalloonInfoNotification("Amadeus is running")
oIcon.ContextMenu = cmsIcon

This is where I set the ContextMenu property for the NotifyIcon class, then in the class I raise the event like this:
Code:
Private Shadows Sub Messages_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Messages.MouseUp
   RaiseEvent MouseUp(Me, e)
   If e.Button = MouseButtons.Right Then
     Messages.Activate()
     Dim Position As Point = New Point(Cursor.Position.X - Messages.PointToScreen(New Point(0, 0)).X, Cursor.Position.Y - Messages.PointToScreen(New Point(0, 0)).Y)
     If Not _ContextMenu Is Nothing Then  _ContextMenu.Show(Messages, Position)
   End If
End Sub

Senior Qik III, ASP.Net, VB.Net ,SQL Programmer

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SELECT * FROM Users WHERE clue > 0
0 Rows Returned
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top