VB6.0实现系统托盘

VB6.0实现系统托盘



  WINDOWS状态栏也称系统托盘,在WINDOWS9X中已有系统时钟、音量控制、输入法等程序在WINDOWS的状态栏中设有图标,一些应用程序在安装完后也将它们本身的图标放入了状态栏中,如超级解霸、WINAMP等。通过在应用程序中有效地控制状态栏中的图标,不仅可以使应用程序具有专业水准,也方便了用户的操作。VB做为一种使用很广的高级语言,实现将图标放入状态栏的功能并不困难,只要有效地利用一个API函数 Shell_NotifyIcon和NOTIFYICONDATA数据结构就能达到这一目的,有关这两者的定义和使用在程序中有详细的注释,在此就不再详述了。

  下面的这个程序运行后,将窗口图标加入到了WINDOWS状态栏中,用鼠标右击该图标会弹出一个菜单,可实现修改该图标、窗口复位、最小化、最大化及关闭程序等功能。

  在VB6中新建一工程,将FORM1的ScalMode的属性设为3,加入一个image控件和一个对话框控件(要加入对话框控件,须在部件中选取Microsoft Common Dialog Control 6.0),将image1的visible属性改为False,为该Form添加一个菜单,菜单设置如下:

标题 名称
文件(&F) mnuFile (一级菜单)
退出(&E) mnuExit (二级菜单)
Popup mnuTray (一级菜单,去掉该项的"可见"项)
更换图标(&I) mnuTrayChangeIcon (以下全为二级菜单)
恢复(&R) mnuTrayRestore
最小化(&N) mnuTrayMinimize
最大化(&X) mnuTrayMaximize
- mnuTrayLine
关闭(&C) mnuTrayClose

  以下是程序清单:

Option Explicit

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

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Private Const WM_SYSCOMMAND = &H112
Private Const SC_RESTORE = &HF120&

Private LastState As Integer '保留原窗口状态

'---------- dwMessage可以是以下NIM_ADD、NIM_DELETE、NIM_MODIFY 标识符之一----------
Private Const NIM_ADD = &H0 '在任务栏中增加一个图标
Private Const NIM_DELETE = &H2 '删除任务栏中的一个图标
Private Const NIM_MODIFY = &H1 '修改任务栏中个图标信息

Private Const NIF_MESSAGE = &H1 'NOTIFYICONDATA结构中uFlags的控制信息
Private Const NIF_ICON = &H2
Private Const NIF_TIP = &H4

Private Const WM_MOUSEMOVE = &H200 '当鼠标指针移至图标上

Private Const WM_LBUTTONUP = &H202
Private Const WM_RBUTTONUP = &H205

Private Type NOTIFYICONDATA
 cbSize As Long '该数据结构的大小
 hwnd As Long '处理任务栏中图标的窗口句柄
 uID As Long '定义的任务栏中图标的标识
 uFlags As Long '任务栏图标功能控制,可以是以下值的组合(一般全包括)
 'NIF_MESSAGE 表示发送控制消息;
 'NIF_ICON表示显示控制栏中的图标;
 'NIF_TIP表示任务栏中的图标有动态提示。
 uCallbackMessage As Long '任务栏图标通过它与用户程序交换消息,处理该消息的窗口由hWnd决定
 hIcon As Long '任务栏中的图标的控制句柄
 szTip As String * 64 '图标的提示信息
End Type

Dim myData As NOTIFYICONDATA

Private Sub Form_Load()
 If WindowState = vbMinimized Then
  LastState = vbNormal
 Else
  LastState = WindowState
 End If

 With myData
  .cbSize = Len(myData)
  .hwnd = Me.hwnd
  .uID = 0
  .uFlags = NIF_ICON Or NIF_MESSAGE Or NIF_TIP
  .uCallbackMessage = WM_MOUSEMOVE
  .hIcon = Me.Icon.Handle '默认为窗口图标
  .szTip = "提示" & vbNullChar
 End With

 Shell_NotifyIcon NIM_ADD, myData

End Sub

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
 Select Case CLng(X)
  Case WM_RBUTTONUP '鼠标在图标上右击时弹出菜单
    Me.PopupMenu mnuTray
  Case WM_LBUTTONUP '鼠标在图标上左击时窗口若最小化则恢复窗口位置
    If Me.WindowState = vbMinimized Then
     Me.WindowState = LastState
     Me.SetFocus
    End If
 End Select
End Sub

Private Sub Form_Unload(Cancel As Integer)
 Shell_NotifyIcon NIM_DELETE, myData '窗口卸载时,将状态栏中的图标一同卸载
End Sub

Private Sub mnuExit_Click()
 Unload Me
End Sub

Private Sub mnuTrayChangeIcon_Click()
 On Error GoTo ErrHandler
 With cdlOpen
  .CancelError = True ' 设置标志
  .InitDir = App.Path ' 默认的文件夹为当前文件夹
  .Flags = cdlOFNHideReadOnly ' 设置过滤器
  .Filter = "图标文件 (*.ico)|*.ico" ' 指定缺省的过滤器为图标文件
  .ShowOpen ' 显示选定文件的名字
 End With

 Image1.Picture = LoadPicture(cdlOpen.FileName)

 With myData
  .hIcon = Image1.Picture
  .uFlags = NIF_ICON
 End With
 Shell_NotifyIcon NIM_MODIFY, myData

ErrHandler: ' 用户按了"取消"按钮
  Exit Sub
End Sub

Private Sub mnuTrayClose_Click()
 Unload Me
End Sub

Private Sub Form_Resize()
 Select Case WindowState
  Case vbMinimized
   mnuTrayMaximize.Enabled = True
   mnuTrayMinimize.Enabled = False
   mnuTrayRestore.Enabled = True
  Case vbMaximized
   mnuTrayMaximize.Enabled = False
   mnuTrayMinimize.Enabled = True
   mnuTrayRestore.Enabled = True
  Case vbNormal
   mnuTrayMaximize.Enabled = True
   mnuTrayMinimize.Enabled = True
   mnuTrayRestore.Enabled = False
 End Select
 If WindowState <> vbMinimized Then LastState = WindowState
End Sub

Private Sub mnuTrayMaximize_Click()
 WindowState = vbMaximized
End Sub

Private Sub mnuTrayMinimize_Click()
 WindowState = vbMinimized
End Sub

Private Sub mnuTrayRestore_Click()
 SendMessage hwnd, WM_SYSCOMMAND, SC_RESTORE, 0&
End Sub
 

你可能感兴趣的:(VB6.0实现系统托盘)