科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道基础软件VB6.0实现系统托盘

VB6.0实现系统托盘

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

VB做为一种使用很广的高级语言,实现将图标放入状态栏的功能并不困难。

作者:张新华 来源:yesky 2007年10月15日

关键字:

  • 评论
  • 分享微博
  • 分享邮件
以下是程序清单:

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

查看本文来源

    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章