Link to home
Start Free TrialLog in
Avatar of gary234
gary234

asked on

Coding A Program To The Task Bar

Hi,

I would like for my program to go to the task bar when minimized. Not the main taskbar, but over by the clock, like what happens when you minimize an IM program or where the virus software remains running. I would then like the program to maximize when needed.

thanks
Avatar of DeAn
DeAn

Avatar of gary234

ASKER

Wow, thats a bit complicated (for me). Can i copy-paste the whole program and have it work??
From the Microsoft Knowledge base:
HOWTO: Manipulate Icons in the System Tray with Visual Basic
http://support.microsoft.com/default.aspx?scid=KB;en-us;q162613
Avatar of gary234

ASKER

Well the code provided does work in the sense that when the cmd button is pressed it adds an icon to the task bar, but i cant figure how i can make my application minimize to JUST the task bar by clicking on the minimize button.
i think this is what you want:

Private Sub Form_Resize()
Me.Visible = Not (Me.WindowState = vbMinimized)
End Sub

you mean minimize to system tray, in which case you need to set the ShowInTaskbar property of the form to false. unfortuately i think you can only do that at design time, so the form will never appear in the task bar, just in the tray

but make sure that clicking on the tray restores the form or you won't be able to get it back.
I have the full code to place you program into the system tray, minimise to the sys tray and have right click options, also double click on the tray icon to maximise and minimise, if you want it cos i dont have it here at home then email me tomorrow when im at work and i can forward it onto you. the email address you can contact me on is thecompany@myfastmail.com
Avatar of gary234

ASKER

Thanks to all, but no luck so far. HJ2K3 when i run the program and minimize it, it dissapears.

I will email Andre412 tomorrow.

I will clarify again. When i close my icq program by clicking on the X in the top left hand corner, it sets itsself in the tray. I can right click on it for options and to fully close. I would also like for the program to open its self when Incoming data (from the parallel port) occurs.

Thanks again to all
create a form (call it frmSysTray) and add a menu to it with the caption "&Popup" and name "mnuPopup". insert this, then insert another entry under this with no caption and name "mnuSysTray" (click the right arrow to make this a menu sub-item and not another menu in itself). then paste the following code into this form:

----------------------------------------------------------

'*** FrmSysTray - interface for system tray icon

Option Explicit

' frmSysTray.
' Steve McMahon
' based on code supplied from Ben Baird:

'Author:
'        Ben Baird <psyborg@cyberhighway.com>
'        Copyright (c) 1997, Ben Baird
'
'Purpose:
'        Demonstrates setting an icon in the taskbar's
'        system tray without the overhead of subclassing
'        to receive events.

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

Private Const WM_MOUSEMOVE = &H200
Private Const NIF_ICON = &H2
Private Const NIF_MESSAGE = &H1
Private Const NIF_TIP = &H4
Private Const NIM_ADD = &H0
Private Const NIM_MODIFY = &H1
Private Const NIM_DELETE = &H2
Private Const MAX_TOOLTIP As Integer = 64
Private Type NOTIFYICONDATA
    cbSize As Long
    hwnd As Long
    uID As Long
    uFlags As Long
    uCallbackMessage As Long
    hIcon As Long
    szTip As String * MAX_TOOLTIP
End Type
Private nfIconData As NOTIFYICONDATA

Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long

Private Const WM_LBUTTONDBLCLK = &H203
Private Const WM_LBUTTONDOWN = &H201
Private Const WM_LBUTTONUP = &H202
Private Const WM_RBUTTONDBLCLK = &H206
Private Const WM_RBUTTONDOWN = &H204
Private Const WM_RBUTTONUP = &H205

Public Event SysTrayMouseDown(ByVal eButton As MouseButtonConstants)
Public Event SysTrayMouseUp(ByVal eButton As MouseButtonConstants)
Public Event SysTrayMouseMove()
Public Event SysTrayDoubleClick(ByVal eButton As MouseButtonConstants)
Public Event MenuClick(ByVal lIndex As Long, ByVal sKey As String)

Private m_bAddedMenuItem As Boolean
Private m_iDefaultIndex As Long

Public Property Get ToolTip() As String
Dim sTip As String
Dim iPos As Long
    sTip = nfIconData.szTip
    iPos = InStr(sTip, Chr$(0))
    If (iPos <> 0) Then
        sTip = Left$(sTip, iPos - 1)
    End If
    ToolTip = sTip
End Property
Public Property Let ToolTip(ByVal sTip As String)
    If (sTip & Chr$(0) <> nfIconData.szTip) Then
        nfIconData.szTip = sTip & Chr$(0)
        nfIconData.uFlags = NIF_TIP
        Shell_NotifyIcon NIM_MODIFY, nfIconData
    End If
End Property
Public Property Get IconHandle() As Long
    IconHandle = nfIconData.hIcon
End Property
Public Property Let IconHandle(ByVal hIcon As Long)
    If (hIcon <> nfIconData.hIcon) Then
        nfIconData.hIcon = hIcon
        nfIconData.uFlags = NIF_ICON
        Shell_NotifyIcon NIM_MODIFY, nfIconData
    End If
End Property
Public Function AddMenuItem(ByVal sCaption As String, Optional ByVal sKey As String = "", Optional ByVal bDefault As Boolean = False) As Long
Dim iIndex As Long
    If Not (m_bAddedMenuItem) Then
        iIndex = 0
        m_bAddedMenuItem = True
    Else
        iIndex = mnuSysTray.UBound + 1
        Load mnuSysTray(iIndex)
    End If
    mnuSysTray(iIndex).Visible = True
    mnuSysTray(iIndex).Tag = sKey
    mnuSysTray(iIndex).Caption = sCaption
    If (bDefault) Then
        m_iDefaultIndex = iIndex
    End If
    AddMenuItem = iIndex
End Function
Private Function ValidIndex(ByVal lIndex As Long) As Boolean
    ValidIndex = (lIndex >= mnuSysTray.LBound And lIndex <= mnuSysTray.UBound)
End Function
Public Sub EnableMenuItem(ByVal lIndex As Long, ByVal bState As Boolean)
    If (ValidIndex(lIndex)) Then
        mnuSysTray(lIndex).Enabled = bState
    End If
End Sub
Public Function RemoveMenuItem(ByVal iIndex As Long) As Long
Dim i As Long
    If ValidIndex(iIndex) Then
        If (iIndex = 0) Then
            mnuSysTray(0).Caption = ""
        Else
            ' remove the item:
            For i = iIndex + 1 To mnuSysTray.UBound
                mnuSysTray(iIndex - 1).Caption = mnuSysTray(iIndex).Caption
                mnuSysTray(iIndex - 1).Tag = mnuSysTray(iIndex).Tag
            Next i
            Unload mnuSysTray(mnuSysTray.UBound)
        End If
    End If
End Function
Public Property Get DefaultMenuIndex() As Long
    DefaultMenuIndex = m_iDefaultIndex
End Property
Public Property Let DefaultMenuIndex(ByVal lIndex As Long)
    If (ValidIndex(lIndex)) Then
        m_iDefaultIndex = lIndex
    Else
        m_iDefaultIndex = 0
    End If
End Property
Public Function ShowMenu()
   SetForegroundWindow Me.hwnd
    If (m_iDefaultIndex > -1) Then
        ABar.Bands("Options").TrackPopup -1, -1
        'Me.PopupMenu mnuPopup, 0, , , mnuSysTray(m_iDefaultIndex)
    Else
        'Me.PopupMenu mnuPopup, 0
    End If
End Function

Private Sub Form_Load()
    'Add the icon to the system tray...
    With nfIconData
        .hwnd = Me.hwnd
        .uID = Me.Icon
        .uFlags = NIF_ICON Or NIF_MESSAGE Or NIF_TIP
        .uCallbackMessage = WM_MOUSEMOVE
        .hIcon = Me.Icon.Handle
        .szTip = App.FileDescription & Chr$(0)
        .cbSize = Len(nfIconData)
    End With
    Shell_NotifyIcon NIM_ADD, nfIconData
End Sub

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim lX As Long
   ' VB manipulates the x value according to scale mode:
   ' we must remove this before we can interpret the
   ' message windows was trying to send to us:
   lX = ScaleX(X, Me.ScaleMode, vbPixels)
   Select Case lX
   Case WM_MOUSEMOVE
       RaiseEvent SysTrayMouseMove
   Case WM_LBUTTONUP
       RaiseEvent SysTrayMouseDown(vbLeftButton)
   Case WM_LBUTTONUP
       RaiseEvent SysTrayMouseUp(vbLeftButton)
   Case WM_LBUTTONDBLCLK
       RaiseEvent SysTrayDoubleClick(vbLeftButton)
   Case WM_RBUTTONDOWN
       RaiseEvent SysTrayMouseDown(vbRightButton)
   Case WM_RBUTTONUP
       RaiseEvent SysTrayMouseUp(vbRightButton)
   Case WM_RBUTTONDBLCLK
       RaiseEvent SysTrayDoubleClick(vbRightButton)
   End Select

End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    Shell_NotifyIcon NIM_DELETE, nfIconData
End Sub

Private Sub mnuSysTray_Click(Index As Integer)
    RaiseEvent MenuClick(Index, mnuSysTray(Index).Tag)
End Sub

----------------------------------------------------------


then, in the general area put:

Dim WithEvents sTray as frmSysTray

and in your main Form_Load() put:
----------------------------------------------------------
Set sTray = New frmSysTray
With sTray
.AddMenuItem "&Open Program", "Open", True
.AddMenuItem "-"
.AddMenuItem "&About...", "vbAccelerator"
.AddMenuItem "-"
.AddMenuItem "&Close", "Close"
'add other menu items similarly

.ToolTip = "This program tray tooltip"
.IconHandle = Me.Icon.Handle  'this line only if you want the icon to be same as this form, otherwise just set icon in frmSysTray
End With
----------------------------------------------------------


now, in the object list (under (General)) should be sTray.
click on this and you will get the events in the box to the right. then put the following code into the events:

----------------------------------------------------------
Private Sub sTray_MenuClick(ByVal lIndex As Long, ByVal sKey As String)
Select Case sKey
    Case "Open"
        Me.WindowState = FormWindowStateConstants.vbNormal
        Me.Show
        Me.ZOrder
    Case "Close"
        Me.WindowState = FormWindowStateConstants.vbNormal
        Me.Show
        Me.ZOrder
        Unload Me
        Exit Sub
    Case Else
        MsgBox "Clicked item with key " & sKey, vbInformation
   End Select
End Sub

Private Sub sTray_SysTrayDoubleClick(ByVal eButton As MouseButtonConstants)
Call sTray_MenuClick(1, "Open")
End Sub

Private Sub sTray_SysTrayMouseDown(ByVal eButton As MouseButtonConstants)
    If (eButton = vbRightButton) Then
        sTray.ShowMenu
    End If
End Sub
----------------------------------------------------------

also, in the Form_Unload should be this:

Unload sTray
Set sTray = Nothing

in order to remove the icon from the system tray and free up resources

you could also set the Form_Resize event to
Me.Visible = Not (Me.WindowState = vbMinimized)

since clicking on the tray should bring it back up

I have used this method in a couple of programs and have found it to work well.

hope you find success with it too.
sorry, after the first block of code, everything should be in the main form of your program, not frmSysTray
...also where it says "ABar.Bands("Options").TrackPopup -1, -1", you should remove this and uncomment the next line, and the one just after Else.

if you want the close button on the form to just minimise, put the following in Form_Unload:
----------------------------------------------------------
if not wantUnload then
  Cancel = -1
  Me.WindowState = FormWindowStateConstants.vbMinimized
endif
----------------------------------------------------------

and put this in main form general area:

Dim wantUnload as Boolean

and then under Case "Close" of sTray_MenuClick should be:

wantUnload = True
Unload Me
Exit Sub
Avatar of gary234

ASKER

How do i add a menu and caption? I really am a newbie...
ok, once you have a new form, go to Tools->Menu editor
in Caption box put "&Popup", underneath that in Name put "mnuPopup", then where "&Popup" appears in the large box below, click just beneath the text. The Caption and Name boxes should now be clear; put "mnuSysTray" in Name and click on the right pointing arrow button. Click OK and you should have a menu called Popup with one blank entry. then paste the code as stated above into this new form. Save it as frmSysTray. Then the rest of the code (after "then, in the general area put:") should go in the form of your main program. it's a bit messy and maybe unclear, please ask if you need clarification
Avatar of gary234

ASKER

To hj2k3,

could you specify where the first block of code ends? I am having trouble getting this to work. Could you possibly email me the working code and the form.

To andre421,

i received your email. Thanks! But i open the three forms, add the module and it dosn't work. I get errors.
any advice??

Thanks guys
ASKER CERTIFIED SOLUTION
Avatar of hj2k3
hj2k3

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gary234

ASKER

To hj2k3,

could you specify where the first block of code ends? I am having trouble getting this to work. Could you possibly email me the working code and the form.

To andre421,

i received your email. Thanks! But i open the three forms, add the module and it dosn't work. I get errors.
any advice??

Thanks guys
Avatar of gary234

ASKER

hj2k3,

Ok i tried it again and received one error.

Private Sub mnuSysTray_Click(Index As Integer)

Error:
Procedure declaration does not match
Avatar of gary234

ASKER

hj2k3,

Ok i tried it again and received one error.

Private Sub mnuSysTray_Click(Index As Integer)

Error:
Procedure declaration does not match
Avatar of gary234

ASKER

hj2k3,

Ok i tried it again and received one error.

Private Sub mnuSysTray_Click(Index As Integer)

Error:
Procedure declaration does not match
Avatar of gary234

ASKER

hj2k3,

Ok i tried it again and received one error.

Private Sub mnuSysTray_Click(Index As Integer)

Error:
Procedure declaration does not match description of event or procedure having the same name.

how can i fix this??

my email is greg.d@musicstop.com

Thanks for the help