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

Need help with system tray programming...

Status
Not open for further replies.

namxat

Programmer
Oct 23, 2002
6
US
Hello. I inappropriately posted this message to the C newsgroup. I'm a little bit new to the Windows API, and was wondering if someone could help me out. I am using the LCC-Win32 compiler, by the way.

Basically, I want to create a program that runs in the system tray. When the user left-clicks on the system tray icon, it should display a dialog box. (The program will not end execution when the dialog box is dismissed). When the user right-clicks on the system tray icon, a menu will pop up from which the user can choose to exit the program or do various other things.

I know how to create, show, and retrieve the return value of a dialog box. I also know how to put an icon in the system tray, sort of: I know it involves the Shell_NotifyIcon method. What I need to find out is how to combine the two.

I also have some compatibility concerns about the Shell_NotifyIcon method. This application will be distributed across the Win95/98/2000/ME/NT/XP line. If anybody could help me out, I would greatly appreciate it. Thank you.
 
'form1'
Private Sub Form_Load()
Hook Me
TraySet Me, " - Off-line", Me.Icon
End Sub

'module FormHook
Option Explicit

Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" _
(ByVal lpPrevWndFunc As Long, _
ByVal hwnd As Long, _
ByVal Msg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long

Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer

Public Const GWL_WNDPROC = -4
Public Const WM_LBUTTONUP = &H202
Public lpPrevWndProc As Long
Global gHW As Long
Global gHW1 As Long
Global appForm As Form

Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long

Public Sub Hook(frm As Form)
Set appForm = frm
lpPrevWndProc = SetWindowLong(frm.hwnd, GWL_WNDPROC, AddressOf WindowProc)
End Sub

Public Sub UnHook()
Dim lngReturnValue As Long
lngReturnValue = SetWindowLong(gHW, GWL_WNDPROC, lpPrevWndProc)
End Sub

Function WindowProc(ByVal hwnd As Long, _
ByVal uMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
Dim retProc As Boolean

On Error Resume Next
Select Case uMsg
Case WM_LBUTTONUP
If GetAsyncKeyState(vbLeftButton) < 0 And bTraySet Then
Sleep 150
TrayRestore Main
End If
If GetAsyncKeyState(vbRightButton) < 0 And bTraySet Then
TrayNotify.TrayMenu Main
End If
retProc = True
Case Else
retProc = True
End Select


If retProc Then
WindowProc = CallWindowProc(lpPrevWndProc, hwnd, uMsg, wParam, lParam)
Else
WindowProc = 0
End If
End Function

'module TrayNotify
Option Explicit

Public Declare Function Shell_NotifyIcon Lib &quot;shell32.dll&quot; Alias &quot;Shell_NotifyIconA&quot; (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long
Public Declare Function CreatePopupMenu Lib &quot;user32&quot; () As Long
Public Declare Function TrackPopupMenu Lib &quot;user32&quot; (ByVal hMenu As Long, ByVal wFlags As Long, ByVal x As Long, ByVal y As Long, ByVal nReserved As Long, ByVal hwnd As Long, ByVal lprc As Any) As Long
Public Declare Function AppendMenu Lib &quot;user32&quot; Alias &quot;AppendMenuA&quot; (ByVal hMenu As Long, ByVal wFlags As Long, ByVal wIDNewItem As Long, ByVal lpNewItem As Any) As Long
Public Declare Function DestroyMenu Lib &quot;user32&quot; (ByVal hMenu As Long) As Long

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


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 MF_GRAYED = &H1&
Public Const MF_STRING = &H0&
Public Const MF_SEPARATOR = &H800&
Public Const TPM_NONOTIFY = &H80&
Public Const TPM_RETURNCMD = &H100&
Public Const TPM_HORNEGANIMATION = &H800&
Public Const TPM_HORPOSANIMATION = &H400&
Public Const TPM_VERNEGANIMATION = &H2000&
Public Const TPM_VERPOSANIMATION = &H1000&

Public Const MF_UNCHECKED = &H0&
Public Const MF_CHECKED = &H8&

Public Const MF_POPUP = &H10&
Public Const MF_MENUBARBREAK = &H20&
Public Const MF_MENUBREAK = &H40&

Public bTraySet As Boolean
Public lMenu As Long

Public nID As NOTIFYICONDATA

Public Sub TraySet(frm As Form, sztip As String, hIcon As Long)
With nID
.cbSize = Len(nID)
.hIcon = hIcon
.hwnd = frm.hwnd
.sztip = sztip & vbNullChar
.uCallbackMessage = WM_LBUTTONUP
.uFlags = NIF_MESSAGE Or NIF_ICON Or NIF_TIP
.uid = 1&
End With
Shell_NotifyIcon NIM_ADD, nID
frm.Hide
bTraySet = True
End Sub

Public Sub TrayRestore(frm As Form)
Dim nID As NOTIFYICONDATA
With nID
.cbSize = Len(nID)
.hwnd = frm.hwnd
.uid = 1&
End With

Shell_NotifyIcon NIM_DELETE, nID
frm.Show
bTraySet = False
Main.TrayTimer.Enabled = False

End Sub

Public Sub TrayMenu(frm As Form)
MsgBox &quot;test&quot;
End Sub

Public Sub TrayModify(frm As Form, sztip As String, hIcon As Long)

With nID
.cbSize = Len(nID)
.hIcon = hIcon
.hwnd = frm.hwnd
.sztip = sztip & vbNullChar
.uCallbackMessage = WM_LBUTTONUP
.uFlags = NIF_MESSAGE Or NIF_ICON Or NIF_TIP
.uid = 1&
End With
Shell_NotifyIcon NIM_MODIFY, nID
bTraySet = True

End Sub
' --------- ' end

If you want you program were running while you track popup menu you have to put you main code in diferent process:
Declare Function CreateProcess Lib &quot;kernel32&quot; ...) As Long
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top