[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


System Tray Icon 1.1

Posted on 1998-12-16
Medium Priority
Last Modified: 2012-08-13
I am getting a runtime error 440 "Automation Error" when using this STI.OCX in my application.  It seems to work okay in my development environment, but not when I install on another machine. I am using the package and deployment wizard to compile the setup application. I am not sure that all the necessary dll's are getting included in the setup app. Has anyone else had this problem. Please advise.. Thanks!

Question by:gwaltersii
  • 3
  • 2
LVL 13

Expert Comment

ID: 1450822
What do need from this STI.OCX. I can give the information to you maybe?

HOWTO: Use the System Tray Directly from Visual Basic 5.0
Last reviewed: November 3, 1997
Article ID: Q176085  
The information in this article applies to:
Microsoft Visual Basic Control Creation, Learning, Professional, and Enterprise Editions for Windows, version 5.0

This article demonstrates how to take full advantage of the Windows System Tray using Visual Basic 5.0. It places an icon of your choice into the System Tray that will display a ToolTip of your choice when the mouse is rested over it, will restore the application when clicked, and will display a popup menu when right-clicked. This is all possible because of Visual Basic 5.0's ability to directly handle callbacks, therefore taking full advantage of the Shell_NotifyIcon function that is exported by Shell32.dll.

The following example can be added to any Visual Basic 5.0 Project that has at least one form and a standard module.

Step-by-Step Example:

Add the following code to the declarations section of a standard module in your project:

      'user defined type required by Shell_NotifyIcon API call
      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

      'constants required by Shell_NotifyIcon API call:
      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_MOUSEMOVE = &H200
      Public Const WM_LBUTTONDOWN = &H201     'Button down
      Public Const WM_LBUTTONUP = &H202       'Button up
      Public Const WM_LBUTTONDBLCLK = &H203   'Double-click
      Public Const WM_RBUTTONDOWN = &H204     'Button down
      Public Const WM_RBUTTONUP = &H205       'Button up
      Public Const WM_RBUTTONDBLCLK = &H206   'Double-click
      Public Declare Function SetForegroundWindow Lib "user32" _
      (ByVal hwnd As Long) As Long
      Public Declare Function Shell_NotifyIcon Lib "shell32" _
      Alias "Shell_NotifyIconA" _
      (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean
      Public nid As NOTIFYICONDATA

Add the following code to any form in your project that you want to respond to the System Tray Icon for your application:

      Private Sub Form_Load()
       'the form must be fully visible before calling Shell_NotifyIcon
       With nid
        .cbSize = Len(nid)
        .hwnd = Me.hwnd
        .uId = vbNull
        .uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
        .uCallBackMessage = WM_MOUSEMOVE
        .hIcon = Me.Icon
        .szTip = "Your ToolTip" & vbNullChar
       End With
       Shell_NotifyIcon NIM_ADD, nid
      End Sub
      Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As
         Single, Y As Single)
      'this procedure receives the callbacks from the System Tray icon.
      Dim Result As Long
      Dim msg As Long
       'the value of X will vary depending upon the scalemode setting
       If Me.ScaleMode = vbPixels Then
        msg = X
        msg = X / Screen.TwipsPerPixelX
       End If
       Select Case msg
        Case WM_LBUTTONUP        '514 restore form window
         Me.WindowState = vbNormal
         Result = SetForegroundWindow(Me.hwnd)
        Case WM_LBUTTONDBLCLK    '515 restore form window
         Me.WindowState = vbNormal
         Result = SetForegroundWindow(Me.hwnd)
        Case WM_RBUTTONUP        '517 display popup menu
         Result = SetForegroundWindow(Me.hwnd)
         Me.PopupMenu Me.mPopupSys
       End Select
      End Sub
      Private Sub Form_Resize()
       'this is necessary to assure that the minimized window is hidden
       If Me.WindowState = vbMinimized Then Me.Hide
      End Sub
      Private Sub Form_Unload(Cancel As Integer)
       'this removes the icon from the system tray
       Shell_NotifyIcon NIM_DELETE, nid
      End Sub
      Private Sub mPopExit_Click()
       'called when user clicks the popup menu Exit command
       Unload Me
      End Sub
      Private Sub mPopRestore_Click()
       'called when the user clicks the popup menu Restore command
       Me.WindowState = vbNormal
       Result = SetForegroundWindow(Me.hwnd)
      End Sub

Make the following Property Settings on the same form to which you added the above code:

   Property         Required Setting for System Tray example
   Icon           = The icon you want to appear in the system tray.
   Minbutton      = True
   ShownInTaskbar = False

Add the following Menu items to the same form using the Menu Editor:

   Caption      Name          Enabled   Visible   Position
   &SysTray     mPopupSys      True      False    Main Level
   &Restore     mPopRestore    True      True     Inset one
   &Exit        mPopExit       True      True     Inset one

You can add additional menu items as needed.

System Tray Flexibility
You can modify the ToolTip that appears over the System Tray icon by changing the following line in the Form_Load procedure:

   .szTip = "Your ToolTip" & vbNullChar

Replace "Your ToolTip" with the text that you want to appear.
You can modify the Icon that appears in the System Tray by changing the following line in the Form_Load procedure:

   .hIcon = Me.Icon

Replace Me.Icon with any Icon in your project.
You can change any of the System Trays settings at any time after the use of the NIM_ADD constant by reassigning the values in the nid variable and then using the following variation of the Shell_NotifyIcon API call:

   Shell_NotifyIcon NIM_MODIFY, nid.

However, if you want a different form to receive the callback, then you will need to delete the current icon first using "Shell_NotifyIcon NIM_Delete, nid" as the NIM_Modify function will not accept a new Hwnd, or you will need to add another Icon to the systray for the new form using "Shell_NotifyIcon NIM_ADD, nid" after refilling the nid type with the new forms Hwnd. You can also declare separate copies of the nid type for each form that you want to display an icon for in the Windows System Tray and change them in each form's activate event using the NIM_DELETE and NIM_ADD sequence.  


Author Comment

ID: 1450823
Actually, I figured out the problem. I was calling a form into memory using the "click" event. For some reason this control does not like to open forms with status bar contols.  I removed the control and the form showed perfectly.  Very strange, but I can live with it.

LVL 13

Expert Comment

ID: 1450824
Ask customer service to delete this question. OR delete it yourself when possible.

Author Comment

ID: 1450825
Actually I like your answer as well.. put it in the form of an answer. If this ocx gives me more grief, I will use your code instead. At least I will have a little more control.  Thanks
LVL 13

Accepted Solution

Mirkwood earned 200 total points
ID: 1450826
See comments below.

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month19 days, 23 hours left to enroll

873 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