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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do i get my program to go into the system tray 2

Status
Not open for further replies.

LLANELLI2004

Programmer
Sep 2, 2004
7
0
0
GB
I have been working on a program that requires use of the system clock the question for which i have is how do i get my program to be in the system tray next to the clock

i have read a number of tutorials but none of them help me so does anyone have the answer for me

 
I have a tutorial for this that i got of the web a hile back.
Place your program's icon in the Windows System Tray





Summary

You can add a touch of class to your programs by placing an icon in the System tray when your App is minimised. Some of the programs I write stay running all day, and I use tool tips and icon changes to indicate status, and forms and menus that appear when the icon is clicked. Once you get used to Windows trickery involved, its easy to get carried away adding great functionality to your icon. Find out how to...

Use the System Tray Directly from Visual Basic 5 and 6

This article demonstrates how to take full advantage of the Windows System Tray using Visual Basic. It places an icon of your choice into the System Tray that will display a ToolTip of your choice when the mouse is rested over it, will restore the application when clicked, and will display a popup menu when right-clicked. This is all possible because of Visual Basic's ability to directly handle callbacks, therefore taking full advantage of the Shell_NotifyIcon function that is exported by Shell32.dll. . This function allows you to add, modify, delete, set a ToolTip string, and send a callback message to execute mouse events.

Add the following code to the declarations section of a standard module in your project:

'user defined type required by Shell_NotifyIcon API call
Public Type NOTIFYICONDATA
cbSize As Long
hwnd As Long
uId As Long
uFlags As Long
uCallBackMessage As Long
hIcon As Long
szTip As String * 64
End Type

'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


Public Declare Function SetForegroundWindow Lib "user32" _
(ByVal hwnd As Long) As Long
Public Declare Function Shell_NotifyIcon Lib "shell32" _
Alias "Shell_NotifyIconA" _
(ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean

Public nid As NOTIFYICONDATA

Add the following code to any form in your project that you want to respond to the System Tray Icon for your application:


Private Sub Form_Load()
'the form must be fully visible before calling Shell_NotifyIcon
Me.Show
Me.Refresh
With nid
.cbSize = Len(nid)
.hwnd = Me.hwnd
.uId = vbNull
.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
.uCallBackMessage = WM_MOUSEMOVE
.hIcon = Me.Icon
.szTip = "Your ToolTip" & vbNullChar
End With
Shell_NotifyIcon NIM_ADD, nid
End Sub


Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
'this procedure receives the callbacks from the System Tray icon.
Dim Result As Long
Dim msg As Long
'the value of X will vary depending upon the scalemode setting
If Me.ScaleMode = vbPixels Then
msg = X
Else
msg = X / Screen.TwipsPerPixelX
End If
Select Case msg
Case WM_LBUTTONUP '514 restore form window
Me.WindowState = vbNormal
Result = SetForegroundWindow(Me.hwnd)
Me.Show
Case WM_LBUTTONDBLCLK '515 restore form window
Me.WindowState = vbNormal
Result = SetForegroundWindow(Me.hwnd)
Me.Show
Case WM_RBUTTONUP '517 display popup menu
Result = SetForegroundWindow(Me.hwnd)
Me.PopupMenu Me.mPopupSys
End Select
End Sub

Private Sub Form_Resize()
'this is necessary to assure that the minimized window is hidden
If Me.WindowState = vbMinimized Then Me.Hide
End Sub


Private Sub Form_Unload(Cancel As Integer)
'this removes the icon from the system tray
Shell_NotifyIcon NIM_DELETE, nid
End Sub

Private Sub mPopExit_Click() 'called when user clicks the popup menu Exit command Unload Me End Sub


Private Sub mPopRestore_Click()
'called when the user clicks the popup menu Restore command
Me.WindowState = vbNormal
Result = SetForegroundWindow(Me.hwnd)
Me.Show
End Sub

Make the following Property Settings on the same form to which you added the above code:

Icon
The icon you want to appear in the system tray.

Minbutton
True

ShownInTaskbar
False




Add the following Menu items to the same form using the Menu Editor:

Caption
Name
Enabled
Visible
Position

&SysTray
mPopupSys
True
False
Main Level

&Restore
mPopRestore
True
True
Inset one

&Exit
mPopExit
True
True
one




You can add additional menu items as needed.

System Tray Flexibility

You can modify the ToolTip that appears over the System Tray icon by changing the following line in the Form_Load procedure:

.szTip = "Your ToolTip" & vbNullChar

Replace "Your ToolTip" with the text that you want to appear. You can modify the Icon that appears in the System Tray by changing the following line in the Form_Load procedure:

.hIcon = Me.Icon

Replace Me.Icon with any Icon in your project.

You can change any of the System Trays settings at any time after the use of the NIM_ADD constant by reassigning the values in the nid variable and then using the following variation of the Shell_NotifyIcon API call:

Shell_NotifyIcon NIM_MODIFY, nid.

However, if you want a different form to receive the callback, then you will need to delete the current icon first using "Shell_NotifyIcon NIM_Delete, nid" as the NIM_Modify function will not accept a new Hwnd, or you will need to add another Icon to the systray for the new form using "Shell_NotifyIcon NIM_ADD, nid" after refilling the nid type with the new forms Hwnd. You can also declare separate copies of the nid type for each form that you want to display an icon for in the Windows System Tray and change them in each form's activate event using the NIM_DELETE and NIM_ADD sequence.

Also there is a FREE third party control that can do it for you also MB TRAY
 
Creating a tray icon like that has never been my problem, but for some reason I cannot get rid of it. When I call the Shell_NotifyIcon with the NIM_DELETE, the icon remains in the system tray and doesn't disappear untill I move my mouse over it. I checked all the tutorials I could find and they pretty much use the same code so I thought I'd be stuck with that icon for ever.

Along came MB Tray via your post bombdropVB, and it solved my problem. Very easy to use and free. Have a star!
 
How do you make a program work as a system process, so it'll start up automatically without having to place the icon in the "start up" folder? Is there a FAQ (I didn't see one) explaining this, or someplace else that can? Or does someone here know how to explain it if it wouldn't be too detailed?

-Ovatvvon :-Q
 
Whoops, sorry, I meant to post this into the main forums. Please disregard.

-Ovatvvon :-Q
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top