VB6 and Tray Icon

Posted on 2005-04-13
Last Modified: 2008-02-01

I used to use Me.Hide and call the notification icon function to create a minimize to tray effect.

The problem with that is the app still shows up on the "Task" list, which sometimes interferes with system's shutdown. I know that this is because the form is not properly unloaded, but only hidden.

Is there any better way that is better than Me.Hide that will actually "unload the form" but the "app remains open"?

Question by:HM_CodeMaster
    LVL 32

    Expert Comment

    by:Robberbaron (robr)
    I cant see why you couldnt use Unload frm1 from with a module level code but I think you would lose the ability to show the form again and certainly the use of right-click menus would not be available as they are part of the form.
    LVL 8

    Expert Comment

    I  had the same thing happen to me. here's what i did.

    1. use Sub Main in a module as the startup routine.
    2. in Sub Main, load a form(frmTrayIcon) that contains the trayicon code in form_load.
    3. in (frmTayIcon's) Form_Unload and Form_QueryUnload, put your shutdown routine. this form will only shutdown if you shutdown the computer.
    4. Create menu commands For Program Exit and Shutdown that are accessed from the rightclick of the trayicon and on the frmTrayIcon Form. You can then unload the other form or do a hide (its not the form keeping the application alive any more.)

    In a nut shell, you have a form that loads once at startup and all it knows is to stay loaded, until proper application shutdown, and holds your Icon Menu. If your computer goes to unload that form because it is shutting down, that form know how to shut the Application down properly. you wont get anymore crashes on shutdown. Your other forms are just loaded and unloaded when you need them. And if you want you could call your shutdown from any of the other forms as well.
    LVL 2

    Author Comment

    Ok, I really appreciated your response.

    However, I'm not very familiar with the apps startup routine thingy, and I don't really understand because the tray icon is now tied to the frmTrayIcon, which cannot be unloaded because frmTrayIcon is the object that keep the app alive, then it will comes to the same problems isn't it, you can only call frmTrayIcon.Hide but not Unload frmTrayIcon.

    Sorry if I misunderstood anything but can you give me the pseudo-code for Sub Main and frmTrayIcon?

    Another thing, where should I put the Sub Main routine? In the BAS module?
    LVL 2

    Author Comment

    In case you don't fully understand my question.

    Say it simply, is there any way to make the "Process" running while not displaying on the "Task" part in the Windows Task Manager. Normally tray-icon-based program like Outlook, MSN Messenger, etc. tend to unload the application but remains running in the system memory (so the app is still shown in "Processes" but not "Tasks") Is there a way to do the same thing in VB6?
    LVL 32

    Accepted Solution

    To your last post, no I dont think so. Applications are usually in the Tasks & Processes set, while Services are in the Processes set.

    I think wraiths description of the startup / shutdown is earier to read. As to your quey re startup seq, you can either nominate a Sub Main() or a Form (which invokes the Form_Load) to be the initial point of your application.  This selection is on the Properties Dialog of your project, Startup object.

    I think what Wraith is saying is that when you app is asked to shutdown by the os, it calls the Query_Unload. Therefore you should put code that closes all other forms, objects , files etc within the application in either location. Or call your own CleanQuit routine, that way the quit routine can be called from elsewhere within the app as well.

    From VB5 help on Query_Unload... {This event is typically used to make sure there are no unfinished tasks in the forms included in an application before that application closes. For example, if a user has not yet saved some new data in any form, your application can prompt the user to save the data.
    When an application closes, you can use either the QueryUnload or Unload event procedure to set the Cancel property to True, stopping the closing process. However, the QueryUnload event occurs in all forms before any are unloaded, and the Unload event occurs as each form is unloaded.}
    LVL 8

    Expert Comment

    dang, thats makes about 3 questions i have answered and got no points for. My l luck sucks, or i can't explain anything in simple terms hehehh :)

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
    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…
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

    734 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

    24 Experts available now in Live!

    Get 1:1 Help Now