PDA

Click to See Complete Forum and Search --> : SysTray


Aaron Young
Nov 20th, 1999, 09:54 AM
Add an Invisible Picturebox to your Form, then Paste this Code:

Private 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

Private Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long

Private Const NIF_ICON = &H2
Private Const NIF_MESSAGE = &H1
Private Const NIF_TIP = &H4
Private Const NIM_ADD = &H0
Private Const NIM_DELETE = &H2
Private Const NIM_MODIFY = &H1
Private Const WM_MOUSEMOVE = &H200
Private Const WM_LBUTTONDBLCLK = &H203
Private Const WM_LBUTTONDOWN = &H201
Private Const WM_RBUTTONDOWN = &H204

Private tTrayIcon As NOTIFYICONDATA

Private Sub Form_Load()
'Create a Tray Icon
Picture1.Visible = False
With tTrayIcon
.hIcon = Icon
.hwnd = Picture1.hwnd
.szTip = "My Tray Icon" & Chr(0)
.uCallbackMessage = WM_MOUSEMOVE
.uFlags = NIF_ICON Or NIF_MESSAGE Or NIF_TIP
.uID = 1
.cbSize = Len(tTrayIcon)
End With
Shell_NotifyIcon NIM_ADD, tTrayIcon
End Sub

Private Sub Form_Resize()
DoEvents
If WindowState = vbMinimized Then
Hide
End If
End Sub

Private Sub Form_Unload(Cancel As Integer)
'Remove the Tray Icon
Shell_NotifyIcon NIM_DELETE, tTrayIcon
End Sub

Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Select Case ScaleX(X, vbTwips, vbPixels)
Case WM_RBUTTONDOWN
'Show Popup Menu
Case WM_LBUTTONDBLCLK
'Show the Form
WindowState = vbNormal
Show
End Select
End Sub

If you want it even simpler I believe there's an OCX available on this site somewhere, which bascially does all this in the background.

Only downside is you have to ship the OCX with your Project.

------------------
Aaron Young
Analyst Programmer
aarony@redwingsoftware.com
adyoung@win.bright.net

CyberCarsten
Nov 20th, 1999, 11:56 AM
Does anyone know how i can
put my program in the
System Tray in an EASY way????

/ CyberCarsten

CyberCarsten
Nov 20th, 1999, 03:26 PM
Thank you Aaron!
I'll try it!

CyberCarsten
Nov 20th, 1999, 03:37 PM
When i place the following code
in my Private Sub Form_Load()
'Create a Tray Icon
Picture1.Visible = False
With tTrayIcon
.hIcon = Icon
.hwnd = Picture1.hwnd
.szTip = "My Tray Icon" & Chr(0)
.uCallbackMessage = WM_MOUSEMOVE
.uFlags = NIF_ICON Or NIF_MESSAGE Or NIF_TIP
.uID = 1
.cbSize = Len(tTrayIcon)
End With
Shell_NotifyIcon NIM_ADD, tTrayIcon

VB marks : Shell_NotifyIcon NIM_ADD, tTrayIcon
and the following error occuors:

Compile Error
Sub or Function not defined

What is wrong??

/ CyberCarsten

MiDaWe
Nov 21st, 1999, 12:00 AM
I'd like to add a question here.

I'd like to use a systray icon for my program, but I'd like the program to load as a tray icon and then allow the form1 to be shown once the user click the icon in the tray and select for the program to be seen from a drop down menu.

Anyone know how to do this?

Thanks,
MiDaWe

Aaron Young
Nov 21st, 1999, 05:52 AM
Just Set the Forms Visible Property to False, the Load Event will still trigger creating the Tray Icon, then in the Right Click Section of the Select Case Statement, Display the PopupMenu from which you can set the Forms Visible Property to True.

------------------
Aaron Young
Analyst Programmer
aarony@redwingsoftware.com
adyoung@win.bright.net

Yonatan
Nov 21st, 1999, 11:44 AM
Try copying all of Aaron's code and not just a part. The code works on an empty form with only an invisible PictureBox in it.

------------------
Yonatan
Teenage Programmer
E-Mail: RZvika@netvision.net.il
ICQ: 19552879 (http://www.icq.com/19552879)
AIM: RYoni69

CyberCarsten
Nov 22nd, 1999, 12:41 AM
It still doesn't work!
Could someone please send me the project???

carsten.h.thomsen@mail.tele.dk

/ CyberCarsten

Aaron Young
Nov 22nd, 1999, 01:46 AM
Set the Forms Visible Property to False, Add an Invisible Picturebox.
Create a Menu Call mnuPopup and add the Sub Items; mnuShowForm1 and mnuExit, make sure the Visible Property of mnuPopup is False (Unchecked).

Then paste All this code into the Forms General Section..

Private 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

Private Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long

Private Const NIF_ICON = &H2
Private Const NIF_MESSAGE = &H1
Private Const NIF_TIP = &H4
Private Const NIM_ADD = &H0
Private Const NIM_DELETE = &H2
Private Const WM_MOUSEMOVE = &H200
Private Const WM_RBUTTONUP = &H205

Private tTrayIcon As NOTIFYICONDATA

Private Sub Form_Load()
With tTrayIcon
.hIcon = Icon
.hwnd = Picture1.hwnd
.szTip = Caption & Chr(0)
.uCallbackMessage = WM_MOUSEMOVE
.uFlags = NIF_ICON Or NIF_MESSAGE Or NIF_TIP
.uID = 1
.cbSize = Len(tTrayIcon)
End With
Shell_NotifyIcon NIM_ADD, tTrayIcon
End Sub

Private Sub Form_Resize()
If WindowState = vbMinimized Then
Hide
End If
End Sub

Private Sub Form_Unload(Cancel As Integer)
Shell_NotifyIcon NIM_DELETE, tTrayIcon
End Sub

Private Sub mnuExit_Click()
Unload Me
End Sub

Private Sub mnuShowForm1_Click()
Show
End Sub

Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Select Case ScaleX(X, vbTwips, vbPixels)
Case WM_RBUTTONUP
Me.PopupMenu mnuPopup
End Select
End Sub



------------------
Aaron Young
Analyst Programmer
aarony@redwingsoftware.com
adyoung@win.bright.net

CyberCarsten
Nov 22nd, 1999, 02:01 AM
Hi Aaron!
Thank you for all your help!!
It finally worked!!!

And just one last Question:
Can i call the same procedure with a button??

Yours Sincierly
/ CyberCarsten

Aaron Young
Nov 22nd, 1999, 03:07 AM
The Form is Shown from the mnuShowForm1 Click Event, so you can either call that Event from the Command Button or Simple Show the Form, eg.

Private Sub Command1_Click()
mnuShowForm1_Click
End Sub

or..

Private Sub Command1_Click()
Show
End Sub



------------------
Aaron Young
Analyst Programmer
aarony@redwingsoftware.com
adyoung@win.bright.net