Click to See Complete Forum and Search --> : tasktray icon
KnIgHt
Sep 12th, 2000, 09:55 AM
Um...I just wanted to know if anyone could tell me how to make my program minimize into a task tray icon when the X button is clicked on the form. i don't want the program to end when its clicked...i just want it to go into the task tray. and i also want it to go into the task tray when it is minimized....i know there are tips for this but i want some better explanation from someone. also how can i have the task tray icon respond with a popup menu when it gets click on. thnx.
-|Kn|gHt|
-UIN 17743984
Dhugal
Sep 12th, 2000, 11:30 AM
Hi man,
You need to use the windows API, and a funstion called
SHELL_NOTIFYICON
I think there is an article on this site somewhere, hang on...
Tick tock, tick tock,
http://www.vbsquare.com/tips/tip178.html
Have a look at this, If it is a bit confucing please send me a mail to Dhugal@callnetuk.com and I will explain it all.
Its not as bad as it looks! Honest!
Here is a bit more, which use the Shell_NotifyIcon api function and works great.
Declare Function Shell_NotifyIcon Lib "shell32" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean
Public Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
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_LBUTTONUP = &H202
Public Const WM_RBUTTONUP = &H205
Public Const WM_MOUSEMOVE = &H200
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 VBGTray As NOTIFYICONDATA
'Form Declarations:
Private Sub GoSystemTray()
VBGTray.cbSize = Len(VBGTray)
VBGTray.hwnd = Me.hwnd
VBGTray.uId = vbNull
VBGTray.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
VBGTray.ucallbackMessage = WM_MOUSEMOVE
VBGTray.hIcon = Me.Icon
'tool tip text
VBGTray.szTip = Me.Caption & vbNullChar
Call Shell_NotifyIcon(NIM_ADD, VBGTray)
App.TaskVisible = False 'remove application from taskbar
Me.Hide
End Sub
'Form Code:
Private Sub Form_MouseMove(button As Integer, Shift As Integer, X As Single, Y As Single)
Static lngMsg As Long
Static blnFlag As Boolean
Dim result As Long
lngMsg = X / Screen.TwipsPerPixelX
If blnFlag = False Then
blnFlag = True
Select Case lngMsg
'right-click
Case WM_RBUTTONUP
result = SetForegroundWindow(Me.hwnd)
PopupMenu MyPopUpMenu
Case WM_LBUTTONUP
result = SetForegroundWindow(Me.hwnd)
PopupMenu MyPopUpMenu
End Select
blnFlag = False
End If
End Sub
Private Sub Form_Resize()
If Me.WindowState = 1 Then
Call GoSystemTray
End If
End Sub
Private Sub Form_Unload(Cancel As Integer)
VBGTray.cbSize = Len(VBGTray)
VBGTray.hwnd = Me.hwnd
VBGTray.uId = vbNull
Call Shell_NotifyIcon(NIM_DELETE, VBGTray)
End Sub
KnIgHt
Sep 12th, 2000, 08:08 PM
Originally posted by Matthew Gates
Here is a bit more, which use the Shell_NotifyIcon api function and works great.
Declare Function Shell_NotifyIcon Lib "shell32" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean
Public Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
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_LBUTTONUP = &H202
Public Const WM_RBUTTONUP = &H205
Public Const WM_MOUSEMOVE = &H200
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 VBGTray As NOTIFYICONDATA
'Form Declarations:
Private Sub GoSystemTray()
VBGTray.cbSize = Len(VBGTray)
VBGTray.hwnd = Me.hwnd
VBGTray.uId = vbNull
VBGTray.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
VBGTray.ucallbackMessage = WM_MOUSEMOVE
VBGTray.hIcon = Me.Icon
'tool tip text
VBGTray.szTip = Me.Caption & vbNullChar
Call Shell_NotifyIcon(NIM_ADD, VBGTray)
App.TaskVisible = False 'remove application from taskbar
Me.Hide
End Sub
'Form Code:
Private Sub Form_MouseMove(button As Integer, Shift As Integer, X As Single, Y As Single)
Static lngMsg As Long
Static blnFlag As Boolean
Dim result As Long
lngMsg = X / Screen.TwipsPerPixelX
If blnFlag = False Then
blnFlag = True
Select Case lngMsg
'right-click
Case WM_RBUTTONUP
result = SetForegroundWindow(Me.hwnd)
PopupMenu MyPopUpMenu
Case WM_LBUTTONUP
result = SetForegroundWindow(Me.hwnd)
PopupMenu MyPopUpMenu
End Select
blnFlag = False
End If
End Sub
Private Sub Form_Resize()
If Me.WindowState = 1 Then
Call GoSystemTray
End If
End Sub
Private Sub Form_Unload(Cancel As Integer)
VBGTray.cbSize = Len(VBGTray)
VBGTray.hwnd = Me.hwnd
VBGTray.uId = vbNull
Call Shell_NotifyIcon(NIM_DELETE, VBGTray)
End Sub
Ok this is what I got my program to do...it'll minimize to the task tray but if i hit the X it won't goto the tasktray....I want the X to goto the tasktray to..I'm using an mdi form and also I want my popup menu to load the form back up again when i click on it. how do i do this??? please respond asap
module code:
Declare Function Shell_NotifyIcon Lib "shell32" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean
Public Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
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_LBUTTONUP = &H202
Public Const WM_RBUTTONUP = &H205
Public Const WM_MOUSEMOVE = &H200
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 VBGTray As NOTIFYICONDATA
form code:
Option Explicit
Private Sub GoSystemTray()
VBGTray.cbSize = Len(VBGTray)
VBGTray.hwnd = Me.hwnd
VBGTray.uId = vbNull
VBGTray.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
VBGTray.ucallbackMessage = WM_MOUSEMOVE
VBGTray.hIcon = Me.Icon
'tool tip text
VBGTray.szTip = Me.Caption & vbNullChar
Call Shell_NotifyIcon(NIM_ADD, VBGTray)
App.TaskVisible = False 'remove application from taskbar
Me.Hide
End Sub
Private Sub MDIForm_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Static lngMsg As Long
Static blnFlag As Boolean
Dim result As Long
lngMsg = X / Screen.TwipsPerPixelX
If blnFlag = False Then
blnFlag = True
Select Case lngMsg
'right-click
Case WM_RBUTTONUP
result = SetForegroundWindow(Me.hwnd)
PopupMenu MyPopupMenu
Case WM_LBUTTONUP
result = SetForegroundWindow(Me.hwnd)
PopupMenu MyPopupMenu
End Select
blnFlag = False
End If
End Sub
Private Sub MDIForm_Resize()
If Me.WindowState = 1 Then
Call GoSystemTray
End If
End Sub
Private Sub MDIForm_Unload(Cancel As Integer)
VBGTray.cbSize = Len(VBGTray)
VBGTray.hwnd = Me.hwnd
VBGTray.uId = vbNull
Call Shell_NotifyIcon(NIM_DELETE, VBGTray)
End Sub
Private Sub mnuKnightBoard_Click()
Me.Show
End Sub
Here you go:
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Cancel = True
Call GoSystemTray
End Sub
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.