CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2000
    Posts
    45

    SHOW IN SYSTEM TRAY

    How do i put my program in the system tray?


  2. #2
    Join Date
    Aug 1999
    Location
    US, Florida
    Posts
    817

    Re: SHOW IN SYSTEM TRAY

    OK follow this:
    You need to make:
    1) picturebox (picture1)
    2) Place a image into it (size of image should be approximately size of an icon)
    3) Place this code into module
    Declare Function Shell_NotifyIconA Lib "SHELL32" (byval dwMessage as Long, lpData as NOTIFYICONDATA) as Long

    Type NOTIFYICONDATA
    cbze as Long
    hwnd as Long
    exid as Long
    flags as Long
    backmessage as Long
    icon as Long
    tip as string * 64
    End Type

    Global Const IcD_ADD = &H0&
    Global Const IcD_MODIFY = &H1
    Global Const IcD_DELETE = &H2
    Global Const IcD_BACKMESSAGE = &H1
    Global Const IcD_ICON = &H2
    Global Const IcD_TIP = &H4
    Global Const WM_MOUSEMOVE = &H200
    Global IcD as NOTIFYICONDATA




    4) Place this code into form

    private Sub Form_Load()
    IcD.cbze = len(IcD)
    IcD.hwnd = Picture1.hwnd
    IcD.exid = 0
    IcD.exid = IcD.exid + 1
    IcD.flags = IcD_BACKMESSAGE Or IcD_ICON Or IcD_TIP
    IcD.icon = Picture1.Picture
    IcD.tip = "tooltip for picture1 (tray icon)" + Chr$(0)
    IcD.backmessage = WM_MOUSEMOVE

    lResult = Shell_NotifyIconA(IcDM_ADD, IcD)
    End Sub


    private Sub Form_Unload(Cancel as Integer)
    Shell_NotifyIconA IcD_DELETE, IcD 'removes the icon from the tray

    End Sub

    private Sub Picture1_MouseMove(Button as Integer, Shift as Integer, X as Single, Y as Single)
    Dim tray as Long
    tray = (X And &HFF) * &H100

    Select Case tray
    Case 0 'mouse moves

    Case &HF00 'left mouse button down
    MsgBox "left mouse button down"
    Case &H1E00 'left mouse button up

    Case &H2D00 'left mouse button double click

    Case &H3C00 'right mouse button down
    MsgBox "right mouse button down"
    Case &H4B00 'right mouse button up

    Case &H5A00 'right mouse button double click

    End Select
    End Sub



    Ok on Form_Load image in picture1 goes into systemtray, on Form_Unload it gets removed, on Picture1_MouseMove you specify what to do on mouse clicks, like popup menu or unload form, etc.


  3. #3
    Join Date
    Jan 2000
    Location
    MO, USA
    Posts
    1,506

    Re: SHOW IN SYSTEM TRAY

    I have always wondered about doing that, but never got around to asking. This is excellent!

    Two little things though...
    The lResult variable isn't defined, and in the line
    "lResult = Shell_NotifyIconA(IcDM_ADD, IcD)"
    the "IcDM_ADD" is wrong, it should be "IcD_ADD" as it is declared in the global section above.

    These aren't major, and some people may never notice them, but i always have Option Explicit on and so i happened to catch it.

    Thank you thank you thank you!!

    John

    John Pirkey
    MCSD
    www.ShallowWaterSystems.com
    John Pirkey
    MCSD (VB6)
    http://www.stlvbug.org

  4. #4
    Join Date
    Jan 2000
    Posts
    4

    Re: SHOW IN SYSTEM TRAY

    This is a great technique. One thing though : How do you add the "pop" up menu when you right-click on the icon for example?


  5. #5
    Join Date
    Jan 2000
    Location
    MO, USA
    Posts
    1,506

    Re: SHOW IN SYSTEM TRAY

    This was my first questions as well. It turns out that, it is as easy as doing it regularly. In the series of case statements to test for the action of the mouse above, for the test of the right mouse click paste this code:

    PopupMenu mnuPopupMenu

    where mnuPopupMenu is the name of root menu item you want to show up on when the user right clicks the icon in the system tray. mnuPopupMenu would then be created on the form with the picturebox. if you would rather use a menu list from another form, just prefix the menu name with the name of the form (eg. mdiMain.mnuPopupMenu).

    Hope this helps,

    John

    John Pirkey
    MCSD
    www.ShallowWaterSystems.com
    John Pirkey
    MCSD (VB6)
    http://www.stlvbug.org

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured