材料/工具:win7
1、下载、安装一个优化软件,启动应用程序,点击进入系统设置按钮选项
2、我们点击左侧的:右键菜单;
3、我们再点右上角的:添加鼠标右键菜单项目;
4、在打开的窗口中,我们可以看到,有18项菜单可以添加到鼠标右键,在我们要添加的项目前面打勾再点:应用到系统就可以了。
With nfIconData.hWnd = Me.hWnd
.uID = Me.Icon
.uFlags = NIF_ICON Or NIF_MESSAGE Or NIF_TIP
.uCallbackMessage = WM_MOUSEMOVE
.hIcon = Me.Icon.Handle
'定义鼠标移动到托盘上时显示的Tip
.szTip = "已最小化到托盘" &vbNullChar
.cbSize = Len(nfIconData)End WithCall Shell_NotifyIcon(NIM_ADD, nfIconData)
'=============================================================System Tray EndMe.HideEnd Function
Private Sub Form_Load()
WindowStyleEnd SubPrivate Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim lMsg As Single
lMsg = X / Screen.TwipsPerPixelX
Select Case lMsg
Case WM_LBUTTONDBLCLK '双击左键显示窗体,要改成其他的看模块里的定义
ShowWindow Me.hWnd, SW_RESTORE
Me.Top = WindowTop
Me.Left = WindowLeft
Me.SetFocus
Case WM_RBUTTONUP '在托盘图标上点右键显示菜单
PopupMenu mnu '菜单名称为mnu,做菜单时你可以改成别的,这里也得改成相应的End SelectEnd SubPrivate Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)'退出程序时删除托盘图标
1、新建一个类模块,粘贴如下内容:Option Explicit
Public Declare Function SetWindowLong Lib "user32.dll" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function CallWindowProc Lib "user32.dll" 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 Const WM_RBUTTONUP = &H205
Public Const WM_USER = &H400
Public Const WM_NOTIFYICON = WM_USER + 1' 自定义消息
Public Const WM_LBUTTONDBLCLK = &H203
Public Const GWL_WNDPROC = (-4)
' 关于气球提示的自定义消息, 2000下不产生这些消息
Public Const NIN_BALLOONSHOW = (WM_USER + &H2) ' 当 Balloon Tips d出时执行
Public Const NIN_BALLOONHIDE = (WM_USER + &H3) ' 当 Balloon Tips 消失时执行(如 SysTrayIcon 被删除),
' 但指定的 TimeOut 时间到或鼠标点击 Balloon Tips 后的消失不发送此消息
Public Const NIN_BALLOONTIMEOUT = (WM_USER + &H4) ' 当 Balloon Tips 的 TimeOut 时间到时执行
Public Const NIN_BALLOONUSERCLICK = (WM_USER + &H5) ' 当鼠标点击 Balloon Tips 时执行。
' 注意:在XP下执行时 Balloon Tips 上有个关闭按钮,
' 如果鼠标点在按钮上将接收到 NIN_BALLOONTIMEOUT 消息。
Public preWndProc As Long
' Form1 窗口入口函数
Function WindowProc(ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
' 拦截 WM_NOTIFYICON 消息
If msg = WM_NOTIFYICON Then
Select Case lParam
Case WM_RBUTTONUP
Form1.Print "右键点击"
Form1.PopupMenu Form1.A '这个form1.A 是你的菜单名字,不要填错哦~~不然vb会死掉的
' 右键单击图标是运行这里的代码, 可以在这里添加d出右键菜单的代码
Case WM_LBUTTONDBLCLK
MsgBox "你双击了我哦,我现在会退出..."
Unload Form1
Case NIN_BALLOONSHOW
Form1.Print "显示提示"
Case NIN_BALLOONHIDE
Form1.Print "删除托盘图标"
Case NIN_BALLOONTIMEOUT
Form1.Print "提示消失"
Case NIN_BALLOONUSERCLICK
MsgBox "你点了我一次..."
Form1.Print "单击提示"
End Select
End If
WindowProc = CallWindowProc(preWndProc, hwnd, msg, wParam, lParam)
End Function
2、在form中,粘贴如下内容:
Option Explicit
Private Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long
Private Type NOTIFYICONDATA
cbSize As Long ' 结构大小(字节)
hwnd As Long' 处理消息的窗口的句柄
uId As Long ' 唯一的标识符
uFlags As Long ' Flags
uCallBackMessage As Long' 处理消息的窗口接收的消息
hIcon As Long ' 托盘图标句柄
szTip As String * 128 ' Tooltip 提示文本
dwState As Long ' 托盘图标状态
dwStateMask As Long ' 状态掩码
szInfo As String * 256 ' 气球提示文本
uTimeoutOrVersion As Long ' 气球提示消失时间或版本
' uTimeout - 气球提示消失时间(单位:ms, 10000 -- 30000)
' uVersion - 版本(0 for V4, 3 for V5)
szInfoTitle As String * 64 ' 气球提示标题
dwInfoFlags As Long ' 气球提示图标
End Type
' dwState to NOTIFYICONDATA structure
Private Const NIS_HIDDEN = &H1 ' 隐藏图标
Private Const NIS_SHAREDICON = &H2 ' 共享图标
' dwInfoFlags to NOTIFIICONDATA structure
Private Const NIIF_NONE = &H0 ' 无图标
Private Const NIIF_INFO = &H1 ' "消息"图标
Private Const NIIF_WARNING = &H2' "警告"图标
Private Const NIIF_ERROR = &H3 ' "错误"图标
' uFlags to NOTIFYICONDATA structure
Private Const NIF_ICON As Long = &H2
Private Const NIF_INFO As Long = &H10
Private Const NIF_MESSAGE As Long = &H1
Private Const NIF_STATE As Long = &H8
Private Const NIF_TIP As Long = &H4
' dwMessage to Shell_NotifyIcon
Private Const NIM_ADD As Long = &H0
Private Const NIM_DELETE As Long = &H2
Private Const NIM_MODIFY As Long = &H1
Private Const NIM_SETFOCUS As Long = &H3
Private Const NIM_SETVERSION As Long = &H4
Private Sub form_load()
' 向托盘区添加图标
Dim IconData As NOTIFYICONDATA
Dim title As String
title = "一个小得不能再小的信息..." &vbNullChar
With IconData
.cbSize = Len(IconData)
.hwnd = Me.hwnd
.uId = 0
.uFlags = NIF_TIP Or NIF_ICON Or NIF_MESSAGE Or NIF_INFO Or NIF_STATE
.uCallBackMessage = WM_NOTIFYICON
.szTip = title
.hIcon = Me.Icon.Handle
.dwState = 0
.dwStateMask = 0
.szInfo = "呵呵 看到了么? 点我试试? 如果要改图标,直接改form的图标就可以了!" &vbNullChar
.szInfoTitle = title
.dwInfoFlags = NIIF_INFO '气球图标常数(上面的定义区已经列出...),这个是信息图标
.uTimeoutOrVersion = 10000 ‘气球停留时间(毫秒)
End With
Shell_NotifyIcon NIM_ADD, IconData
preWndProc = SetWindowLong(Me.hwnd, GWL_WNDPROC, AddressOf WindowProc)
End Sub
Private Sub Form_Unload(Cancel As Integer)
' 删除托盘区图标
Dim IconData As NOTIFYICONDATA
With IconData
.cbSize = Len(IconData)
.hwnd = Me.hwnd
.uId = 0
.uFlags = NIF_TIP Or NIF_ICON Or NIF_MESSAGE
.uCallBackMessage = WM_NOTIFYICON
.szTip = "托盘程序"
.hIcon = Me.Icon.Handle
End With
Shell_NotifyIcon NIM_DELETE, IconData
SetWindowLong Me.hwnd, GWL_WNDPROC, preWndProc
' 卸载所有窗体
Dim frm As Form
For Each frm In Forms
Unload frm
Next
End Sub
3、最后不要忘了在form1里面用菜单编辑器新建一个叫做A的菜单(标题随便取,但名称一定要是A),在它下面多建几个子菜单,然后添加事件,然后...就可以运行了~~
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)