How To Use the System Tray Directly from Visual Basic

 

This article was previously published under Q176085

SUMMARY

This article demonstrates how to take full advantage of the Windows System Tray, or Taskbar Notification Area, using Visual Basic. It places an icon of your choice into the Taskbar Notification Area that will display a ToolTip of your choice when the mouse is rested over it, will restore the application when clicked, and will display a popup menu when right-clicked. This is all possible because of Visual Basic's ability to directly handle callbacks, therefore taking full advantage of the Shell_NotifyIcon function that is exported by Shell32.dll.

MORE INFORMATION

The following example can be added to any Visual Basic Project that has at least one form and a standard module.

Step-by-Step Example

1.Add the following code to the declarations section of a standard module in your project:
      'user defined type required by Shell_NotifyIcon API call
      Public 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

      'constants required by Shell_NotifyIcon API call:
      Public Const NIM_ADD = &H0
      Public Const NIM_MODIFY = &H1
      Public Const NIM_DELETE = &H2
      Public Const NIF_MESSAGE = &H1
      Public Const NIF_ICON = &H2
      Public Const NIF_TIP = &H4
      Public Const WM_MOUSEMOVE = &H200
      Public Const WM_LBUTTONDOWN = &H201     'Button down
      Public Const WM_LBUTTONUP = &H202       'Button up
      Public Const WM_LBUTTONDBLCLK = &H203   'Double-click
      Public Const WM_RBUTTONDOWN = &H204     'Button down
      Public Const WM_RBUTTONUP = &H205       'Button up
      Public Const WM_RBUTTONDBLCLK = &H206   'Double-click

      Public Declare Function SetForegroundWindow Lib "user32" _
      (ByVal hwnd As Long) As Long
      Public Declare Function Shell_NotifyIcon Lib "shell32" _
      Alias "Shell_NotifyIconA" _
      (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean

      Public nid As NOTIFYICONDATA
					
2.Add the following code to any form in your project that you want to respond to the System Tray Icon, or Notification Icon, for your application:
      Private Sub Form_Load()
       'the form must be fully visible before calling Shell_NotifyIcon
       Me.Show
       Me.Refresh
       With nid
        .cbSize = Len(nid)
        .hwnd = Me.hwnd
        .uId = vbNull
        .uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
        .uCallBackMessage = WM_MOUSEMOVE
        .hIcon = Me.Icon
        .szTip = "Your ToolTip" & vbNullChar
       End With
       Shell_NotifyIcon NIM_ADD, nid
      End Sub

      Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As
   _
         Single, Y As Single)
      'this procedure receives the callbacks from the System Tray icon.
      Dim Result As Long
      Dim msg As Long
       'the value of X will vary depending upon the scalemode setting
       If Me.ScaleMode = vbPixels Then
        msg = X
       Else
        msg = X / Screen.TwipsPerPixelX
       End If
       Select Case msg
        Case WM_LBUTTONUP        '514 restore form window
         Me.WindowState = vbNormal
         Result = SetForegroundWindow(Me.hwnd)
         Me.Show
        Case WM_LBUTTONDBLCLK    '515 restore form window
         Me.WindowState = vbNormal
         Result = SetForegroundWindow(Me.hwnd)
         Me.Show
        Case WM_RBUTTONUP        '517 display popup menu
         Result = SetForegroundWindow(Me.hwnd)
         Me.PopupMenu Me.mPopupSys
       End Select
      End Sub

      Private Sub Form_Resize()
       'this is necessary to assure that the minimized window is hidden
       If Me.WindowState = vbMinimized Then Me.Hide
      End Sub

      Private Sub Form_Unload(Cancel As Integer)
       'this removes the icon from the system tray
       Shell_NotifyIcon NIM_DELETE, nid
      End Sub

      Private Sub mPopExit_Click()
       'called when user clicks the popup menu Exit command
       Unload Me
      End Sub

      Private Sub mPopRestore_Click()
       'called when the user clicks the popup menu Restore command
       Dim Result As Long
       Me.WindowState = vbNormal
       Result = SetForegroundWindow(Me.hwnd)
       Me.Show
      End Sub
					
3.Make the following Property Settings on the same form to which you added the above code:
   Property         Required Setting for Taskbar Notification Area example
   -----------------------------------------------------------------------
   Icon           = The icon you want to appear in the system tray.
   Minbutton      = True
   ShownInTaskbar = False
					
4.Add the following Menu items to the same form using the Menu Editor:
   Caption      Name          Enabled   Visible   Position
   ---------------------------------------------------------
   &SysTray     mPopupSys      True      False    Main Level
   &Restore     mPopRestore    True      True     Inset one
   &Exit        mPopExit       True      True     Inset one
					
You can add additional menu items as needed.

Taskbar Notification Area Flexibility

You can modify the ToolTip that appears over the Notification icon by changing the following line in the Form_Load procedure:
   .szTip = "Your ToolTip" & vbNullChar
				
Replace "Your ToolTip" with the text that you want to appear.

You can modify the Icon that appears in the Taskbar Notification Area by changing the following line in the Form_Load procedure:
   .hIcon = Me.Icon
				
Replace Me.Icon with any Icon in your project.

You can change any of the Taskbar Notification Area settings at any time after the use of the NIM_ADD constant by reassigning the values in the nid variable and then using the following variation of the Shell_NotifyIcon API call:
   Shell_NotifyIcon NIM_MODIFY, nid.
				
However, if you want a different form to receive the callback, then you will need to delete the current icon first using "Shell_NotifyIcon NIM_Delete, nid" as the NIM_Modify function will not accept a new Hwnd, or you will need to add another Icon to the systray for the new form using "Shell_NotifyIcon NIM_ADD, nid" after refilling the nid type with the new forms Hwnd. You can also declare separate copies of the nid type for each form that you want to display an icon for in the Windows System Tray and change them in each form's activate event using the NIM_DELETE and NIM_ADD sequence.

REFERENCES

For information regarding using the System Tray, or Taskbar Notification Area, from Visual Basic 4.0 or earlier, please see the following article in the Microsoft Knowledge Base:
149276 (http://support.microsoft.com/kb/149276/EN-US/) How To Use Icons with the Windows 95 System Tray

转载于:https://www.cnblogs.com/Qia_sky/archive/2005/07/24/199153.html

synergy是一种用于共享键盘和鼠标的软件,用于多台计算机间的协同操作。当该软件报错"no systemtrayicon available synergy"时,意味着系统托盘图标不可用。 这个错误通常出现在Windows操作系统上。下面我将提供一些可能的解决方案来解决该问题: 1. 检查synergy是否已正确安装:首先,确保synergy已正确安装在您的计算机上。您可以尝试重新安装软件并确保您使用的是最新版本。 2. 检查系统托盘图标设置:确保系统设置中已经允许synergy在系统托盘中显示图标。您可以通过在任务栏上右键单击并选择相应的选项来进行设置。 3. 检查系统托盘图标驱动:有时,系统托盘图标的驱动程序可能损坏或不兼容,导致synergy无法显示图标。您可以尝试更新或升级系统托盘图标驱动程序来解决问题。 4. 检查防火墙设置:某些防火墙或安全软件可能会阻止synergy在系统托盘中显示图标。您可以尝试禁用或重新配置防火墙,并确保synergy被允许在托盘中显示图标。 5. 检查其他软件冲突:某些其他正在运行的软件可能与synergy发生冲突,导致无法显示系统托盘图标。可以尝试在启动synergy之前关闭其他程序,以查看是否可以解决问题。 如果尝试以上解决方法后问题仍然存在,您可以尝试联系synergy的技术支持团队,详细描述您遇到的问题,以便他们提供更准确的解决方案。希望这些建议能对您有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值