VBGood网站全文搜索 Google

搜索VBGood全站网页(全文搜索)
首页 - 经验之谈 - 用VISUAL BASIC编写托盘程序
发表评论(0)作者:, 平台:, 阅读:10376, 日期:2000-05-18
用VISUAL BASIC编写托盘程序





  在WINDOWS98里,右下角有许多应用程序的图标。而程序本身的窗口是隐藏的,若你需要调用应用程序的窗口,则双击该图标即可。这种程序称为托盘程序。这是WINDOWS98操作系统的一大特色。使用户能够更加快捷的显示和隐藏应用程序,可以使任务栏不致于太乱。托盘程序在Visual Basic里是怎么实现的呢?

    编写托盘程序主要解决两个问题:(1)创建、修改、删除托盘;(2)如何对托盘接收到的消息进行处理。这就要用到几个Windows API函数。

    首先,Shell_NotifyIcon是用于托盘的Shell API。该API用到一个NOTIFYICONDATA结构.

  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

    其次,应该考虑怎样在VB中接收、处理托盘的消息(双击、单击、左键、右键)。C++、Delphi等语言对消息循环的处理较简单,但在VB中处理消息循环时必须应用Win32的SetWindowLong、CallWindowProc这两个API函数。SetWindowLong 函数利用GWL_WNDPROC 索引来创建窗口类的子类(窗口类是用来创建窗口的),它使用AddressOf关键字和回调函数(WindowProc)来截取消息并根据消息来执行相应的功能,如窗口的最大化、最小化、隐藏、退出等。CallWindowProc函数调用原窗口类缺省的指针,程序最后退出时可通过SetWindowLong来关闭子类,重新使原来的Windows过程成为回调函数。

    本程序项目包括一个模块和一个窗体

    1、模块源代码为:

   Option Explicit '强制定义每个使用的变量

    Type NOTIFYICONDATA '定义结构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

    '以下为 Shell_NotifyIcon将用到的常量

    Public Const NIF_ICON = &H2

    Public Const NIF_MESSAGE = &H1

    Public Const NIF_TIP = &H4

    Public Const NIM_ADD = &H0

    Public Const NIM_DELETE = &H2

    Public Const NIM_MODIFY = &H1

    'Shell_NotifyIcon的函数声明

    Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA"

    (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long

   '处理消息将用到的结构、常量、API声明

    Type POINTAPI

     x As Long

     y As Long

    End Type

    Type Msg

     hwnd As Long

     message As Long

     wParam As Long

     lParam As Long

     time As Long

     pt As POINTAPI

    End Type

     Public Const WM_USER = &H400

  Public Const WM_RBUTTONDOWN = &H204

  Public Const WM_LBUTTONDBLCLK = &H203

    Public Const GWL_WNDPROC = -4

    Public trayflag As Boolean '定义托盘图标是否在桌面上

    Global lpPrevWndProc As Long

    Global gHW As Long

  Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

  Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long

   '以下过程为消息循环处理

  Function WindowProc(ByVal hw As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

   If hw = Form1.hwnd And uMsg = WM_USER+100 Then'检测到鼠标点动托盘图标

    Select Case lParam

      Case WM_RBUTTONDOWN '鼠标右键按下

     Form1.PopupMenu Form1.mainmenu '弹出菜单

    Case WM_LBUTTONDBLCLK '鼠标左键双击

   Form1.Show '显示窗口

    Case Else

    End Select

   Else '调用缺省窗口指针

     WindowProc = CallWindowProc(lpPrevWndProc, hw,uMsg, wParam, lParam)

   End If

  End Function

  Public Sub hook() '将程序勾入消息环中

   '利用AddressOf取得消息处理函数WindowProc的指针,并将其传给SetWindowLong

   'lpPrevWndProc用来存储原窗口的指针

   lpPrevWndProc = SetWindowLong(gHW, GWL_WNDPROC, AddressOf WindowProc)

  End Sub

  Public Sub Unhook()

  '将程序从消息环退出。用原窗口的指针替换WindowProc函数的指针,即关闭子类、退出消息循环

   Dim temp As Long

   temp = SetWindowLong(gHW, GWL_WNDPROC, lpPrevWndProc)

  End Sub 

    2、在窗口form1加入一个主菜单mainmenu,设置为不可见。在加入一些子菜单如"显示窗口"(名称为:show),"隐藏窗口"(名称为:hide),'退出程序"(名称为:exit)。在加入四个按钮Command1,Command2,Command3,Command。caption属性分别为:“删除托盘图标",“创建托盘图标",“修改托盘图标",“退出程序".

   代码窗口的内容为:

   Dim MyNot As NOTIFYICONDATA '定义一个托盘结构

   Private Sub Command1_Click() '按下删除托盘图标按钮

   With MyNot

   .hIcon = Form1.Icon '托盘图标指针指向窗口的图标

   .hwnd = Form1.hwnd '窗体指针

   .szTip = "" '弹出提示字符串,删除时应为空

   .uCallbackMessage = WM_USER + 100 '对应程序定义的消息

   .uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE '图标标志

   .uID = 1 '图标识别符

   .cbSize = Len(MyNot) '计算结构实例MyNot的长度

   End With

   hh = Shell_NotifyIcon(NIM_DELETE, MyNot) '删除该托盘图标

   trayflag = False '托盘图标删除后trayflag为假

   End Sub

   Private Sub Command2_Click() '按下创建托盘图标按钮

   Dim hh As Long

   With MyNot

   .hIcon = Form1.Icon

   .hwnd = Form1.hwnd

   .szTip = "托盘图标" & Chr(&H0)

   .uCallbackMessage = WM_USER + 100

   .uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE

   .uID = 1

   .cbSize = Len(MyNot)

   End With

   hh = Shell_NotifyIcon(NIM_ADD, MyNot) '添加一个托盘图标

   trayflag = True '托盘图标添加后trayflag为真

   End Sub

   Private Sub Command3_Click() '按下修改托盘图标按钮

   Dim hh As Long

   Set P = LoadPicture("cd.ico") '导入一个新图标

   With MyNot

   .hIcon = P '将托盘图标改为新图标

   .hwnd = Form1.hwnd

   .szTip = "光盘图标" & Chr(&H0) '更改提示信息

   .uCallbackMessage = WM_USER + 100

   .uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE

   .uID = 1

   .cbSize = Len(MyNot)

   End With

   hh = Shell_NotifyIcon(NIM_MODIFY, MyNot) '修改托盘图标的某些特征

   End Sub

   Private Sub Command4_Click() '退出窗口按钮被按下

   If trayflag = True Then Command1_Click '如果托盘图标仍在,模拟按下"删除托盘图标"按钮

   Unhook '退出消息循环

   Unload Me '卸载窗体

   End Sub

  Private Sub exit_Click()

   If trayflag = True Then Command1_Click '如果托盘图标仍在,模拟按下“删除托盘图标"按钮

   Unhook '退出消息循环

   Unload Me '卸载窗体

  End Sub

   Private Sub Form_Load()

   gHW = Me.hwnd '取得本窗体指针

   hook '调用钩子函数,将自制消息处理函数钩入Windows的消息循环

   End Sub

  Private Sub hide_Click()

  Form1.hide '隐藏窗口

  End Sub

  Private Sub show_Click()

  Form1.show 显示窗口

  End Sub

    在VB中编译这个程序,点击创建托盘图标。则在任务栏的右下角出现一图标。将鼠标放在此图标上,则显示一条信息。点击右键,弹出菜单,可以选择相应的功能执行的。当窗口隐藏时双击托盘图标可显示窗口。注意:在VB中使用函数指针将使程序变得脆弱,在运用与其相关的函数及