Solved

Formless app and Tray Icon

Posted on 2004-10-19
6
229 Views
Last Modified: 2010-05-02
Guys,

I have an application without user interface (kind of service).
To close it I am using task manager (not very nice solution). I want to add an icon to notification area with simple menu poping up with only "Exit" item. Is it possible to do without using form to app?
I know how to put icon, but menu?

Thanks,

Michael
0
Comment
Question by:Michael_D
  • 3
  • 2
6 Comments
 
LVL 14

Expert Comment

by:ajexpert
ID: 12350659
This will add application to system tray
http://www.vb-helper.com/howto_tray_icon.html
0
 
LVL 13

Author Comment

by:Michael_D
ID: 12350970
aj: I know this technique. I asked about formless method.
Is it posible to create popup menu without form?

Michael
0
 
LVL 48

Expert Comment

by:Mikal613
ID: 12351698
http://www.vb-helper.com/howto_tray_icon_formless.html

The book Custom Controls Library includes a control that manipulates the system tray and another that animates a system tray icon. For more information go to www.vb-helper.com/ccl.htm.
Use a hidden form to display the icon and manage tray menus.

Set ShowInTaskBar = False so the form's icon doesn't appear in the task bar. Set Visible = False so the form doesn't apear. Give the form an appropriate icon.

When the program starts, it calls AddToTray to add itself to the system tray. It then calls SetTrayTip to give its system tray icon a tooltip.
 
 
 
Private Sub Form_Load()
    AddToTray Me, mnuTray

    SetTrayTip "VB Helper ""formless"" tray icon program"
End Sub
 
 
 
Subroutine AddToTray gives the form a new WindowProc so it can look for tray events. It then uses the Shell_NotifyIcon API function to make the tray icon. Note that the data structure sets .hIcon = frm.Icon.Handle so the tray icon uses the form's icon.  
 
 
' Add the form's icon to the tray.
Public Sub AddToTray(frm As Form, mnu As Menu)
    ' ShowInTaskbar must be set to False at
    ' design time because it is read-only at
    ' run time.

    ' Save the form and menu for later use.
    Set TheForm = frm
    Set TheMenu = mnu
   
    ' Install the new WindowProc.
    OldWindowProc = SetWindowLong(frm.hwnd, _
        GWL_WNDPROC, AddressOf NewWindowProc)
   
    ' Install the form's icon in the tray.
    With TheData
        .uID = 0
        .hwnd = frm.hwnd
        .cbSize = Len(TheData)
        .hIcon = frm.Icon.Handle
        .uFlags = NIF_ICON
        .uCallbackMessage = TRAY_CALLBACK
        .uFlags = .uFlags Or NIF_MESSAGE
        .cbSize = Len(TheData)
    End With
    Shell_NotifyIcon NIM_ADD, TheData
End Sub
 
 
 
Subroutine SetTrayTip uses Shell_NotifyIcon to set the tray icon's tooltip.  
 
 
' Set a new tray tip.
Public Sub SetTrayTip(tip As String)
    With TheData
        .szTip = tip & vbNullChar
        .uFlags = NIF_TIP
    End With
    Shell_NotifyIcon NIM_MODIFY, TheData
End Sub
 
 
 
When the new WindowProc sees the WM_NCDESTROY message, it calls RemoveFromTray to remove the tray icon and restore the original WindowProc. If you do not remove the tray icon, it remains in the tray but doesn't work properly.
If the new WindowProc sees a TRAY_CALLBACK message, it checks the event in more detail. If the user right clicked the icon, the program displays a popup menu that contains Do Stuff and Close commands. Do Stuff displays a message box. Close unloads the program's form.
 
 
 
' The replacement window proc.
Public Function NewWindowProc(ByVal hwnd As Long, ByVal Msg _
    As Long, ByVal wParam As Long, ByVal lParam As Long) As _
    Long
Const WM_NCDESTROY = &H82

    ' If we're being destroyed, remove the tray icon
    ' and restore the original WindowProc.
    If Msg = WM_NCDESTROY Then
        RemoveFromTray
    ElseIf Msg = TRAY_CALLBACK Then
        ' The user clicked on the tray icon.
        ' Look for click events.
        If lParam = WM_RBUTTONUP Then
            ' On right click, show the menu.
            SetForegroundWindow TheForm.hwnd
            TheForm.PopupMenu TheMenu
            If Not (TheForm Is Nothing) Then
                PostMessage TheForm.hwnd, WM_NULL, ByVal _
                    0&, ByVal 0&
            End If
            Exit Function
        End If
    End If

    ' Send other messages to the original
    ' window proc.
    NewWindowProc = CallWindowProc( _
        OldWindowProc, hwnd, Msg, _
        wParam, lParam)
End function
 
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 13

Author Comment

by:Michael_D
ID: 12351974
As I understand there is no way to do this without form, right?

HIDDEN FORM IS A FORM AS WELL!
Application that use hidden form cannot be called "formless" application

Please forget about TrayIcon.

the question is: HOW TO CREATE PopUpMenu WITHOUT form (NO HIDDEN FORM, PLEASE)
0
 
LVL 48

Accepted Solution

by:
Mikal613 earned 500 total points
ID: 12352049
You have to create the first item in design-time, but you can dynamically add items in runtime using a control array. You can hide the items you create in runtime using Hide method or setting the Visible property to False

basically you need a form sorry.

Alternative:  .net
0
 
LVL 48

Expert Comment

by:Mikal613
ID: 12352097
G-d bless Microsoft.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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…
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

759 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