how can i add a menu when someone right clicks over the icon?

Posted on 2009-12-16
Medium Priority
Last Modified: 2013-12-26
This code has to be in vb6

This is not my code, I found it here:

The code works.  I was wondering how can i add a menu when someone right clicks over the icon?
Such as a menu that would stop the application, and other menu option to quit the app?
Thanks in advance.

Minimise VB 6.0 Application to Task Bar

Open a new standard project in vb 6.0

Then add a module named modPublic.bas in the project and add the following code in the module which are the public declaration and API call code:

Option Explicit

Public Declare Function Shell_NotifyIcon Lib "shell32" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, pnid As MinimiseIconData) As Boolean

'constants required by Shell_NotifyIcon API call:
Public Const NIM_ADD = &H0
Public Const NIM_MODIFY = &H1
Public Const NIM_DELETE = &H2
Public Const NIF_MESSAGE = &H1
Public Const NIF_ICON = &H2
Public Const NIF_TIP = &H4
Public Const WM_MOUSEMOVE = &H200
Public Const WM_LBUTTONDOWN = &H201 'Button down
Public Const WM_LBUTTONUP = &H202 'Button up
Public Const WM_LBUTTONDBLCLK = &H203 'Double-click
Public Const WM_RBUTTONDOWN = &H204 'Button down
Public Const WM_RBUTTONUP = &H205 'Button up
Public Const WM_RBUTTONDBLCLK = &H206 'Double-click

Then add other module named modType for the public type declaration for the project. Add the following code in this module:

Public Type MinimiseIconData
cbSize As Long
hwnd As Long
uId As Long
uFlags As Long
uCallBackMessage As Long
hIcon As Long
szTip As String * 64
End Type

Then on the Form1 properties select the icon which you want to display when your application is minimised to the task bar.
Note: icon has the extention .ico

Add one command button on the form

Then in the Form1 code page add the following code:

Option Explicit

Private minIco As MinimiseIconData

Private Sub Command1_Click()
Unload Me
End Sub

Private Sub Form_Load()
If App.PrevInstance Then

End If
With minIco

.cbSize = Len(minIco)
.hwnd = Me.hwnd
.uId = vbNull
.uCallBackMessage = WM_MOUSEMOVE
.hIcon = Me.Icon ' <== You can change this to another icon
.szTip = "Double-click this icon to make the application visible" & vbNullChar ' <== You can change this also.

End With
Shell_NotifyIcon NIM_ADD, minIco
End Sub

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim lngButtonAction As Long

lngButtonAction = X / Screen.TwipsPerPixelX

Select Case lngButtonAction

' Left mouse button has been double clicked
' If app is currently minimized &.
Me.WindowState = vbNormal
'If Me.WindowState = vbMinimized Then ' & then restore it to normal size
' Me.WindowState = vbNormal
' Shell_NotifyIcon NIM_DELETE, nid ' and remove the icon
'ElseIf Not Me.Visible Then ' Else, if the app is hidden &
' Me.Show ' then show the form again
' Me.WindowState = vbNormal
' Shell_NotifyIcon NIM_DELETE, nid ' and remove the icon
'End If

' You could put code in here to make something happen when
' left mouse button is single clicked on the icon

' You could put code in here to make something happen when
' right mouse button is single clicked on the icon

End Select

End Sub

Private Sub Form_Resize()
On Error Resume Next

If Me.WindowState = vbMinimized Then
Shell_NotifyIcon NIM_ADD, minIco
Else ' otherwise don't show it
Shell_NotifyIcon NIM_DELETE, minIco ' ç This removes the icon
End If
End Sub

Private Sub Form_Terminate()
Shell_NotifyIcon NIM_DELETE, minIco
End Sub
Question by:blackbookcoder2
  • 2
LVL 86

Accepted Solution

Mike Tomlinson earned 2000 total points
ID: 26068972
See "How to use the System Tray directly from Visual Basic":
LVL 22

Expert Comment

ID: 26088068
Add a top level menu item "mnuPopUp" and uncheck visible.
Add 2 submenus "mnuTray" with captions "Stop", "Exit".

Add this code to your project:

   ' You could put code in here to make something happen when
   ' right mouse button is single clicked on the icon
   Me.PopupMenu mnuPopUp

Private Sub mnuTray_Click(Index As Integer)
   Select Case Index
      Case 0 '
         Debug.Print "Stop"
      Case 1 '
         Debug.Print "Exit"
   End Select
End Sub


Author Comment

ID: 26154528
i will get back to you all with the points shortly, thanks for your help, not feeling well this week

Author Closing Comment

ID: 31667150
Thanks very much

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question