当前位置:编程学习 > VB >>

VB怎样实现最小化托盘,怎样实现悬浮框?

答案:可以用API函数

Option Explicit
Public uuu As String
Public uu As String
Public u As String
Public Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long
Public 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
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Public Type NOTIFYICONDATA
    cbSize As Long               '需填入NOTIFYICONDATA数据结构的长度
    hwnd As Long                 '设置成窗口的句柄
    Uid As Long                  '为图标所设置的ID值
    UFlags As Long               '设置uCallbackMessage,hIcon,szTip是否有效
    UCallbackMessage As Long     '消息编号
    hIcon As Long                '显示在状态栏上的图标
    SzTip As String * 64         '提示信息
End Type

Public Const NIF_ICON = &H2
Public Const NIF_TIP = &H4
Public Const NIF_MESSAGE = &H1
Public Const NIM_ADD = 0   '加入图标到系统状态栏中
Public Const NIM_MODIFY = 1 '修改系统状态栏中的图标
Public Const NIM_DELETE = 2 '删除系统状态栏中的图标
Public Const WM_USER = &H400
Public Const WM_LBUTTONUP = &H202
Public Const WM_MBUTTONUP = &H208
Public Const WM_RBUTTONUP = &H205
Public Const TRAY_CALLBACK = (WM_USER + 1001&)
Public Const GWL_WNDPROC = (-4)
Public Const GWL_USERDATA = (-21)

Public OldWindowProc As Long
Public TheForm As Form
Public TheMenu As Menu
Public TheData As NOTIFYICONDATA
Public LastState As Integer

'GWL_WNDPROC获得该窗口的窗口过程的地址,AddressOf是取址函数,NewWindowProc是我们写的过程
'OldWindowProc = SetWindowLong(frm.HWnd, GWL_WNDPROC, AddressOf
''               NewWindowProc)
'
'  然后在NewWindowProc函数中写入如下代码,需要注意的是下面代码中红色的TRAY_CALLBACK
'是由托盘区图标传来的消息,要让托盘图标传回消息,
'必须在添加托盘图标时指定:
 Public Function NewWindowProc(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
      '如果用户点击了托盘中的图标,则进行判断是点击了左键还是右键
      If Msg = TRAY_CALLBACK Then
          '如果点击了左键
          If lParam = WM_LBUTTONUP Then
              If TheForm.Visible = False Then
                  TheForm.WindowState = 0
                  TheForm.Show
               End If
          End If
          '如果点击了右键
          If lParam = WM_RBUTTONUP Then
              '则弹出右键菜单
              TheForm.PopupMenu TheMenu
              Exit Function
          End If
      End If
      '如果是其他类型的消息则传递给原有默认的窗口函数
      NewWindowProc = CallWindowProc(OldWindowProc, hwnd, Msg, wParam, lParam)
End Function

Public Sub AddToTray(frm As Form, mnu As Menu)

    '保存当前窗体和菜单信息
    Set TheForm = frm
    Set TheMenu = mnu
   
    'GWL_WNDPROC获得该窗口的窗口函数的地址
    OldWindowProc = SetWindowLong(frm.hwnd, GWL_WNDPROC, AddressOf NewWindowProc)
   
    '知识点滴:HWnd属性
    '返回窗体或控件的句柄。语法: object.HWnd
    '说明:Microsoft Windows 运行环境,通过给应用程序中的每个窗体和控件
    '分配一个句柄(或 hWnd)来标识它们。hWnd 属性用于Windows API调用。

    '将主窗体图标添加在托盘中
    With TheData
        .Uid = 0    '忘了吗?参考一下前面内容,Uid图标的序号,做动画图标有用
        .hwnd = frm.hwnd
        .cbSize = Len(TheData)
        .hIcon = frm.Icon.Handle
        .UFlags = NIF_ICON                  '指明要对图标进行设置
        .UCallbackMessage = TRAY_CALLBACK
        .UFlags = .UFlags Or NIF_MESSAGE    '指明要设置图标或返回信息给主窗体,此句不能省去
        .cbSize = Len(TheData)              '为什么呢?我们需要在添加图标的同时,让其返回信息
    End With                                '给主窗体,Or的意思是同时进行设置和返回消息
    Shell_NotifyIcon NIM_ADD, TheData       '根据前面定义NIM_ADD,设置为“添加模式”
End Sub
' *********************************************
' 删除系统托盘中的图标
' *********************************************
Public Sub RemoveFromTray()
    '删除托盘中的图标
    With TheData
        .UFlags = 0
    End With
    Shell_NotifyIcon NIM_DELETE, TheData   '根据前面定义NIM_DELETE,设置为“删除模式”
   
    '恢复原有的设置
    SetWindowLong TheForm.hwnd, GWL_WNDPROC, OldWindowProc
End Sub
' *********************************************
' 为托盘中的图标加上浮动提示(也就是鼠标移上去时出现的提示字条)
' *********************************************
Public Sub SetTrayTip(tip As String)
    With TheData
        .SzTip = tip & vbNullChar
        .UFlags = NIF_TIP   '指明要对浮动提示进行设置
    End With
    Shell_NotifyIcon NIM_MODIFY, TheData    '根据前面定义NIM_MODIFY,设置为“修改模式”
End Sub
' *****************

上一个:VB.NET和VB的区别是什么
下一个:VB怎么做得到上次电脑关闭的时间?

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,