Solved

VB and the Taskbar/System Tray

Posted on 2002-07-28
7
337 Views
Last Modified: 2010-05-02
I've got an app that's trying to minimize itself to the taskbar/system tray (whatever you want to call it), but the callbacks on WM_MOUSEMOVE aren't working. Here's the code I use in the Form_Load:

    nid.cbSize = Len(nid)
    nid.hwnd = Me.hwnd
    nid.uId = vbNull
    nid.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
    nid.uCallBackMessage = WM_MOUSEMOVE
    nid.hIcon = Me.Icon
    If (False = Shell_NotifyIcon(NIM_ADD, nid)) Then
        MsgBox "Oops!"
    End If
   
    If (gs.p.fMinimiseOnStartup) Then
        frmMain.WindowState = vbMinimized
        frmMain.Hide
    End If

And here's the procedure that SHOULD be getting the callbacks:
Private Sub Form_MouseMove(Button As Integer, _
                           Shift As Integer, _
                           X As Single, _
                           Y As Single)
    Dim Result As Long
    Dim msg As Long
   
    If frmMain.ScaleMode = vbPixels Then
        msg = X
    Else
        msg = X / Screen.TwipsPerPixelX
    End If
    Select Case msg
        Case WM_LBUTTONUP                    
            frmMain.WindowState = vbNormal
            SetForegroundWindow (frmMain.hwnd)
            frmMain.Show
        Case WM_LBUTTONDBLCLK                
            frmMain.WindowState = vbNormal
            SetForegroundWindow frmMain.hwnd
            frmMain.Show
        Case WM_RBUTTONUP
            SetForegroundWindow frmMain.hwnd
            frmMain.PopupMenu frmMain.mPopupSys
    End Select
End Sub

But it's not getting any MOUSEMOVE messages from the icon. The icon appears in the system tray just fine, and if I leave the form up, the MOUSEMOVE messages are invoking the procedure above...but any mouse movement over the icon in the system tray doesn't. What am I doing wrong?
0
Comment
Question by:toff_in_sydney
  • 3
  • 2
  • 2
7 Comments
 
LVL 38

Expert Comment

by:PaulHews
Comment Utility
>but any mouse movement over the icon in the system tray doesn't. What am I doing wrong?

Have you tried clicking on the icon?
0
 
LVL 38

Expert Comment

by:PaulHews
Comment Utility
AFAIK, only clicking a mouse button over the trayed icon will activate the callback routine, just moving your mouse over it doesn't do it.

It's working for me...
0
 

Author Comment

by:toff_in_sydney
Comment Utility
Yes, moving the mouse over it does activate the callback routine...tried and tested with small apps that just contain that. In any case, clicking doesn't activate it, either.
0
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

 
LVL 38

Expert Comment

by:PaulHews
Comment Utility
Since it is working for me and not for you, lets focus on what could be different.  These are the declarations I used to test:

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
   
Public Const NIM_ADD = 0
Public Const NIM_MODIFY = 1
Public Const NIM_DELETE = 2
Public Const NIF_MESSAGE = 1
Public Const NIF_ICON = 2
Public Const NIF_TIP = 4

Public Const WM_LBUTTONDBLCLK = &H203
Public Const WM_LBUTTONDOWN = &H201
Public Const WM_LBUTTONUP = &H202
Public Const WM_RBUTTONDBLCLK = &H206
Public Const WM_RBUTTONDOWN = &H204
Public Const WM_RBUTTONUP = &H205
Public Const WM_MOUSEMOVE = &H200
Public Const WM_NULL = &H0

Declare Function Shell_NotifyIconA Lib "shell32" _
(ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Integer

0
 
LVL 2

Accepted Solution

by:
priya_pbk earned 145 total points
Comment Utility
hi,

I had done this long back, you can try this...
-----------------------------------------------------

Option Explicit

Const MAX_TOOLTIP As Integer = 64
Const NIF_ICON = &H2
Const NIF_MESSAGE = &H1
Const NIF_TIP = &H4
Const NIM_ADD = &H0
Const NIM_DELETE = &H2
Const WM_MOUSEMOVE = &H200
Const WM_LBUTTONDOWN = &H201
Const WM_LBUTTONUP = &H202
Const WM_LBUTTONDBLCLK = &H203
Const WM_RBUTTONDOWN = &H204
Const WM_RBUTTONUP = &H205
Const WM_RBUTTONDBLCLK = &H206

Private Type NOTIFYICONDATA
   cbSize           As Long
   hwnd             As Long
   uID              As Long
   uFlags           As Long
   uCallbackMessage As Long
   hIcon            As Long
   szTip            As String * MAX_TOOLTIP
End Type
Private nfIconData As NOTIFYICONDATA

Private Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long

Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long

'***App Window Constants***
Private Const WIN_NORMAL = 1         'Open Normal
Private Const WIN_MAX = 3            'Open Maximized
Private Const WIN_MIN = 2            'Open Minimized

Private Declare Function apiShellExecute Lib "shell32.dll" _
   Alias "ShellExecuteA" _
   (ByVal hwnd As Long, _
   ByVal lpOperation As String, _
   ByVal lpFile As String, _
   ByVal lpParameters As String, _
   ByVal lpDirectory As String, _
   ByVal nShowCmd As Long) _
   As Long

'***Error Codes***
Private Const ERROR_SUCCESS = 32&
Private Const ERROR_NO_ASSOC = 31&
Private Const ERROR_OUT_OF_MEM = 0&
Private Const ERROR_FILE_NOT_FOUND = 2&
Private Const ERROR_PATH_NOT_FOUND = 3&
Private Const ERROR_BAD_FORMAT = 11&


Private Sub Form_Load()
AddIcon
Me.Hide
End Sub

Private Sub AddIcon()
'
' Add this application's icon to the system tray.
'
' Parm 1 = Handle of the window to receive notification messages
'          associated with an icon in the taskbar status area.
' Parm 2 = Icon to display.
' Parm 3 = Handle of icon to display.
' Parm 4 = Tooltip displayed when cursor moves over system tray icon.
'
With nfIconData
   .hwnd = Me.hwnd
   .uID = Me.Icon
   .uFlags = NIF_ICON Or NIF_MESSAGE Or NIF_TIP
   .uCallbackMessage = WM_MOUSEMOVE
   .hIcon = Me.Icon.Handle
   .szTip = "Projects/Application" & vbNullChar
   .cbSize = Len(nfIconData)
End With
Call Shell_NotifyIcon(NIM_ADD, nfIconData)
End Sub

Private Sub cmdQuit_Click()
Unload Me
End Sub


Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim lMsg As Single
'
' Determine the event that happened to the System Tray icon.
' Left clicking the icon displays a message box.
' Right clicking the icon creates an instance of an object from an
' ActiveX Code component then invokes a method to display a message.
'
lMsg = X / Screen.TwipsPerPixelX

Select Case lMsg
   Case WM_LBUTTONUP
       mnu_options.Visible = False
   Case WM_RBUTTONUP

   Case WM_MOUSEMOVE
   
   Case WM_LBUTTONDOWN
        mnu_options.Visible = False
   Case WM_LBUTTONDBLCLK

   Case WM_RBUTTONDOWN
           mnu_options.Visible = True
           SetForegroundWindow Me.hwnd
'if you want to show a menu right button down..
           PopupMenu mnu_options, , , , mnu_addcomment 'will be shown in bold
   Case WM_RBUTTONDBLCLK
           'whichever exe you may want to call
   Case Else
       mnu_options.Visible = False
   End Select
End Sub


'let there be menu in the form, say "mnu_options" which you can show at the right click on the icon. The menu will have an "exit" also , which when clicked will unload/end the appliction.

'---------------------------
Create an exe of this project. When you run the exe, it sits at the system tray of your pc. Put this exe in your startup folder of your machine, so will be loaded automatically when the pc is booted! Thru the mousemove, you can do the various tasks.

I hope this helps you!

-priya

0
 

Author Comment

by:toff_in_sydney
Comment Utility
thanks priya...I don't know what I was doing wrong, but i used your code instead and it works perfectly.
0
 
LVL 2

Expert Comment

by:priya_pbk
Comment Utility
you are welcome!
-priya
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
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…

772 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

10 Experts available now in Live!

Get 1:1 Help Now