Solved

How To Add Entry to Windows Tray while VB App running

Posted on 2004-09-10
1
173 Views
Last Modified: 2010-05-02
How to I add an entry (icon and description) to the windows tray while my VB App is running.  Of course it will have to be removed also when the app terminates.
0
Comment
Question by:mlcktmguy
1 Comment
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 100 total points
Comment Utility
Create a new project and add a picturebox.   Set the picture property of the picturebox to the icon you want to use in the tray.  This particular example makes an app that minimizes to the tray.  Double clicking the tray icon brings the app back.  Hitting the close button also makes the app go back to the tray.  Then (compiled) app can only be closed from the task manager.

Regards,

Idle_Mind

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

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 Const NIM_ADD = &H0
Private Const NIM_MODIFY = &H1
Private Const NIM_DELETE = &H2
Private Const NIF_MESSAGE = &H1
Private Const NIF_ICON = &H2
Private Const NIF_TIP = &H4
Private Const NIF_DOALL = NIF_MESSAGE Or NIF_ICON Or NIF_TIP
Private Const WM_MOUSEMOVE = &H200
Private Const WM_LBUTTONDBLCLK = &H203
Private Const WM_LBUTTONDOWN = &H201
Private Const WM_RBUTTONDOWN = &H204

Private tic As NOTIFYICONDATA
Private inTray As Boolean

Private Sub Form_Load()
    Picture1.Visible = False
    Me.WindowState = vbMinimized
End Sub

Private Sub Form_Resize()
    If Me.WindowState = vbMinimized Then
        dropToTray
    End If
End Sub

Private Sub dropToTray()
    tic.cbSize = Len(tic)
    tic.hWnd = Picture1.hWnd ' Callback Control
    tic.uID = 1&
    tic.uFlags = NIF_DOALL
    tic.uCallbackMessage = WM_MOUSEMOVE
    tic.hIcon = Picture1.Picture
    tic.szTip = "My Application" & Chr$(0)
   
    Me.Visible = False
    Shell_NotifyIcon NIM_ADD, tic
    inTray = True
End Sub

Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    X = X / Screen.TwipsPerPixelX
    Select Case X
        Case WM_LBUTTONDBLCLK
            Shell_NotifyIcon NIM_DELETE, tic
            Me.Visible = True
            Me.WindowState = vbNormal
            Me.Show
            inTray = False
    End Select
End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    Select Case UnloadMode
        Case vbAppTaskManager, vbAppWindows
            If inTray Then
                Shell_NotifyIcon NIM_DELETE, tic
            End If

        Case Else
            Cancel = True
            If Me.Visible = True Then
                dropToTray
            End If
           
    End Select
End Sub
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now