Results 1 to 6 of 6

Thread: Minimizing An Application To The System Tray

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2003
    Location
    United Kingdom
    Posts
    35

    Minimizing An Application To The System Tray

    I have this module for creating an icon in the system tray for my application:

    Option Explicit

    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

    'The following constants are the messages sent to the
    'Shell_NotifyIcon function to add, modify, or delete an icon
    'from the taskbar status area.
    Const NIM_ADD = &H0
    Const NIM_MODIFY = &H1
    Const NIM_DELETE = &H2

    'The following constant is the message sent when a mouse event
    'occurs within the rectangular boundaries of the icon in the
    'taskbar status area.
    Const WM_MOUSEMOVE = &H200

    'The following constants are the flags that indicate the valid
    'members of the NOTIFYICONDATA data type.
    Const NIF_MESSAGE = &H1
    Const NIF_ICON = &H2
    Const NIF_TIP = &H4

    'The following constants are used to determine the mouse input
    'on the the icon in the taskbar status area.
    'Left-click constants.
    Const WM_LBUTTONDBLCLK = &H203 'Double-click
    Const WM_LBUTTONDOWN = &H201 'Button down
    Const WM_LBUTTONUP = &H202 'Button up
    'Right-click constants.
    Const WM_RBUTTONDBLCLK = &H206 'Double-click
    Const WM_RBUTTONDOWN = &H204 'Button down
    Const WM_RBUTTONUP = &H205 'Button up

    'Declare the API function call.
    Declare Function Shell_NotifyIcon Lib "shell32" Alias _
    "Shell_NotifyIconA" (ByVal dwMessage As Long, _
    pnid As NOTIFYICONDATA) As Boolean

    'Dimension a variable as the user-defined data type.
    Dim nid As NOTIFYICONDATA


    Public Sub AddTrayIcon(hWnd As Long, hIcon As Long, sTTText As String)
    'As the name indicates, this Sub adds an icon to the System Teay.
    'Parameters- hWnd: Handle to the form the icon is attached to,
    ' normally the main form of the application.
    ' hIcon: Handle to the icon to display. This can
    ' be the Icon property of a form, or the Picture
    ' property of an Image control or PictureBox.
    ' sTTText: The ToolTop text to display when the
    ' mouse hovers over the icon in the Tray.
    nid.cbSize = Len(nid)
    nid.hWnd = hWnd
    nid.uId = vbNull
    nid.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
    nid.uCallBackMessage = WM_MOUSEMOVE
    nid.hIcon = hIcon
    nid.szTip = sTTText & vbNullChar
    'Call the Shell_NotifyIcon function to add the icon to the taskbar
    'status area.
    Shell_NotifyIcon NIM_ADD, nid
    End Sub


    Public Sub RemoveTrayIcon()
    'This Sub assumes the "nid" variable is still loaded with
    'values from the AddTrayIcon Sub, which will ordinarily be true.
    Shell_NotifyIcon NIM_DELETE, nid
    End Sub

    Public Sub TrayEvent(frmApp As Form, mnuPop As Menu, fXCoord As Single)
    'In this example, we are only resonding to double-clicking
    'the left button on the Tray Icon, or right-clicking it. If
    'you want to respond to other mouse events (like right button
    'double-clicks), put code in the relevant cases.
    'Parameters- frmApp: A reference to the form calling this code.
    ' mnuPop: A reference to the PopUpMenu to display.
    ' fXCoord: The X parameter from the MouseMove event
    ' calling this code.

    Dim lMsg As Long

    lMsg = fXCoord / Screen.TwipsPerPixelX
    Select Case lMsg
    Case WM_LBUTTONDOWN
    Case WM_LBUTTONUP
    Case WM_LBUTTONDBLCLK 'Show the main form of the app on
    frmApp.Show 'the left double-click event.
    Case WM_RBUTTONDOWN 'Show the PopUp menu on the right
    frmApp.PopupMenu mnuPop 'click event.
    Case WM_RBUTTONUP
    Case WM_RBUTTONDBLCLK
    End Select
    End Sub

    Public Sub ChangeTrayIcon(hIcon As Long)
    'This Sub changes the icon associated with the Tray Icon. It
    'assumes the nid variable is still loaded from the AddTrayIcon Sub.
    'Parameter - hIcon: Long handle to the icon to be used.
    nid.hIcon = hIcon
    Shell_NotifyIcon NIM_MODIFY, nid
    End Sub
    Adding this to the Form_Load event adds the icon into the tray:

    AddTrayIcon Me.hWnd, Me.Icon, Me.Caption
    BUT, here is my problem. I am nearly sure this is the module I used when I added in code to minimize my application to the system tray icon so it's not on the taskbar, but I have forgotten the code. To clarify, I want to minimize my app to the system tray by clicking the minimize button on the form : _ [ ] X

    Thanks all in advance.

  2. #2
    Frenzied Member RudyL's Avatar
    Join Date
    Mar 2001
    Location
    Chicago
    Posts
    1,519
    in the form_resize event....


    VB Code:
    1. if form1.windowstate = vbminmized then form1.hide


    rudy
    10 different ways to skin a cat and amazingly enough each and every one has the same result, the cat gets skinned! The same can be applied to code, so be nice and accept each others "preferences".

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2003
    Location
    United Kingdom
    Posts
    35
    Tried that Rudy. It minimizes to the taskbar and only disappears when I click it in the taskbar to maximize it again.

  4. #4
    Addicted Member DJ_Catboy's Avatar
    Join Date
    Jan 2003
    Location
    Suffolk, UK
    Posts
    159
    Hi,

    Try this: in the Form_Resize event...

    Code:
    select case form.windowstate
      case vbMinimised
        form.showintaskbar = false
        Shell_NotifyIcon NIM_ADD, nid
      case vbNormal
        form.showintaskbar = true
        Shell_NotifyIcon NIM_DELETE, nid
    
    end select
    The Shell_NotifyIcon calls just give the impression of "restoring from the tray to an app" and minimising to the tray.

    Maybe this works? Hope it helps...

    DJ
    Last edited by DJ_Catboy; Aug 6th, 2003 at 10:55 AM.

  5. #5
    Frenzied Member RudyL's Avatar
    Join Date
    Mar 2001
    Location
    Chicago
    Posts
    1,519
    Originally posted by Chiraq
    Tried that Rudy. It minimizes to the taskbar and only disappears when I click it in the taskbar to maximize it again.
    ok... You can do one of these two things then..

    Set the showintaskbar option to false

    or (and I like this much better, I use it alot)

    remove the minimized and maximized buttons, only have the close button.

    in the close event..

    VB Code:
    1. cancel = 1
    2. form1.hid = true

    Rudy
    10 different ways to skin a cat and amazingly enough each and every one has the same result, the cat gets skinned! The same can be applied to code, so be nice and accept each others "preferences".

  6. #6
    Hyperactive Member
    Join Date
    Jun 2001
    Location
    Buffalo, NY
    Posts
    297
    Chiraq,

    The Form_Resize event is firing before the form is finished resizing. In other words, you minimize it and the before the windowstate changes to vbminimized, the form resize event runs and decides to not hide your form, then windowstate changes to vbminimized.

    When you maximize the form, before it's windowstate changes back to vbmaximized or vbnormal the form resize event fires and hides your form because the windowstate is still vbminimized.

    Make sense? That being said, I'm not sure how you should address this issue properly.

    -Ben

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