2个回答
展开全部
不在任务栏显示很简单,直接设置ShowInTaskbar属性就可以了。
要在系统托盘显示,需要加入自己的代码。
这个代码早就已经有网友封装好了。代码如下:
Attribute VB_Name = "mod_NotifyIcon"
' ********************************************************
' 模块名称:mod_NotifyIcon
' 模块功能:在系统栏加入图标
' 作 者:yangyxd
' 日 期:2007年05月10日 完成
' 版 权:飞雪(yangyxd)工作室
' 备 注:欢迎使用本模块, 请不要删除本信息
' ********************************************************
Option Explicit
Private Const WM_MOUSEMOVE = &H200 '在图标上移动鼠标
Private Const WM_LBUTTONDOWN = &H201 '鼠标左键按下
Private Const WM_LBUTTONUP = &H202 '鼠标左键释放
Private Const WM_LBUTTONDBLCLK = &H203 '双击鼠标左键
Private Const WM_RBUTTONDOWN = &H204 '鼠标右键按下
Private Const WM_RBUTTONUP = &H205 '鼠标右键释放
Private Const WM_RBUTTONDBLCLK = &H206 '双击鼠标右键
Private Const WM_SYSCOMMAND = &H112
Private Const WM_HOTKEY = &H312
Private Const WM_USER = &H400
Private Const WM_SYSTEM_TRAYICON = WM_USER + 1
Private Const TRAY_CALLBACK = (WM_USER + 1001&)
Private Const NIM_ADD = &H0 '在“金碟”中加一图标
Private Const NIM_MODIFY = &H1 '修改“金碟”中的图标
Private Const NIM_DELETE = &H2 '删除“金碟”中的图标
Private Const NIF_MESSAGE = &H1 '使类型"NOTIFYICONDATA"中的uCallbackMessage有效
Private Const NIF_ICON = &H2 '使类型"NOTIFYICONDATA"中的hIcon有效
Private Const NIF_TIP = &H4 '使类型"NOTIFYICONDATA"中的szTip有效
'API函数声明
Private Declare Function RegisterWindowMessage Lib "user32" Alias "RegisterWindowMessageA" (ByVal lpString As String) As Long
Private Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long
'自定义一个调用API Shell_NotifyIcon要用到的类 型"NOTIFYICONDATA"
Private 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
'定义一个"NOTIFYICONDATA"类型的变量
Private TheData As NOTIFYICONDATA
Private result As Long
'添加图标
Public Sub InitIcon(ByVal frmhwnd As Long, ByVal iconhwnd As Long, Optional ByVal tip As String)
With TheData
.cbSize = Len(TheData)
.hwnd = frmhwnd
.hIcon = iconhwnd
.uID = 0
.szTip = tip & vbNullChar
.uFlags = NIF_ICON Or NIF_MESSAGE Or NIF_TIP
.uCallbackMessage = WM_SYSTEM_TRAYICON
End With
result = Shell_NotifyIcon(NIM_ADD, TheData)
'向Windows注入托盘重建消息
RegTrayIconMessage = RegisterWindowMessage("TaskbarCreated")
'DoEvents
'MsgBox TheData.hWnd
End Sub
'删除图标
Public Sub UnInitIcon()
TheData.uFlags = 0
Shell_NotifyIcon NIM_DELETE, TheData
'向Windows注入托盘重建消息
RegTrayIconMessage = RegisterWindowMessage("TaskbarCreated")
End Sub
'更改图标
Public Sub ReInitIcon(ByVal iconhwnd As Long)
With TheData
.hIcon = iconhwnd
'pic是图片狂PictureBox,存放图标文件
.uFlags = NIF_ICON
End With
Shell_NotifyIcon NIM_MODIFY, TheData
'向Windows注入托盘重建消息
RegTrayIconMessage = RegisterWindowMessage("TaskbarCreated")
End Sub
'更改提示信息
Public Sub ReTipInitIcon(ByVal tip As String)
With TheData
.szTip = tip & vbNullChar
'tip是字符串string,存储提示信息
.uFlags = NIF_TIP
'指明要对浮动提示进行设置
End With
Shell_NotifyIcon NIM_MODIFY, TheData
End Sub
'*************************************
' 托盘消息处理
'*************************************
Public Function WndProcIcon(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Select Case Msg
Case WM_SYSTEM_TRAYICON '托盘消息
If lParam = WM_MOUSEMOVE Then '在图标上移动鼠标
'不做任何事
ElseIf lParam = WM_LBUTTONDBLCLK Then '双击鼠标左键
'如果窗口最小化就显示窗口
formMain.WindowState = 0
formMain.Show
ElseIf lParam = WM_LBUTTONDOWN Then '鼠标左键按下
SetForegroundWindow hwnd
ElseIf lParam = WM_LBUTTONUP Then '鼠标左键释放
'不做任何事
ElseIf lParam = WM_RBUTTONDOWN Then '鼠标右键按下
SetForegroundWindow hwnd '关键的一步
ElseIf lParam = WM_RBUTTONUP Then '鼠标右键释放
' 弹出菜单
x.PopupMenu frmMenu.A
ElseIf lParam = WM_RBUTTONDBLCLK Then '双击鼠标右键
'不做任何事
End If
Case RegTrayIconMessage '托盘图标重建消息
Call InitIcon(formMain.hwnd, formMain.Icon.Handle, formMain.Caption)
Frm_Notify = True
End Select
'将消息传下去
WndProcIcon = CallWindowProc(lpHotKey, hwnd, Msg, wParam, lParam)
End Function
用法如下:
程序启动时:
Call InitIcon(Me.hwnd, Me.Icon.Handle, Me.Caption)
OldWndProc = GetWindowLong(Me.hwnd, GWL_WNDPROC)
SetWindowLong Me.hwnd, GWL_WNDPROC, AddressOf WndProcIcon
程序关闭时:
Call UnInitIcon
SetWindowLong formMain.hwnd, GWL_WNDPROC, OldWndProc
要在系统托盘显示,需要加入自己的代码。
这个代码早就已经有网友封装好了。代码如下:
Attribute VB_Name = "mod_NotifyIcon"
' ********************************************************
' 模块名称:mod_NotifyIcon
' 模块功能:在系统栏加入图标
' 作 者:yangyxd
' 日 期:2007年05月10日 完成
' 版 权:飞雪(yangyxd)工作室
' 备 注:欢迎使用本模块, 请不要删除本信息
' ********************************************************
Option Explicit
Private Const WM_MOUSEMOVE = &H200 '在图标上移动鼠标
Private Const WM_LBUTTONDOWN = &H201 '鼠标左键按下
Private Const WM_LBUTTONUP = &H202 '鼠标左键释放
Private Const WM_LBUTTONDBLCLK = &H203 '双击鼠标左键
Private Const WM_RBUTTONDOWN = &H204 '鼠标右键按下
Private Const WM_RBUTTONUP = &H205 '鼠标右键释放
Private Const WM_RBUTTONDBLCLK = &H206 '双击鼠标右键
Private Const WM_SYSCOMMAND = &H112
Private Const WM_HOTKEY = &H312
Private Const WM_USER = &H400
Private Const WM_SYSTEM_TRAYICON = WM_USER + 1
Private Const TRAY_CALLBACK = (WM_USER + 1001&)
Private Const NIM_ADD = &H0 '在“金碟”中加一图标
Private Const NIM_MODIFY = &H1 '修改“金碟”中的图标
Private Const NIM_DELETE = &H2 '删除“金碟”中的图标
Private Const NIF_MESSAGE = &H1 '使类型"NOTIFYICONDATA"中的uCallbackMessage有效
Private Const NIF_ICON = &H2 '使类型"NOTIFYICONDATA"中的hIcon有效
Private Const NIF_TIP = &H4 '使类型"NOTIFYICONDATA"中的szTip有效
'API函数声明
Private Declare Function RegisterWindowMessage Lib "user32" Alias "RegisterWindowMessageA" (ByVal lpString As String) As Long
Private Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long
'自定义一个调用API Shell_NotifyIcon要用到的类 型"NOTIFYICONDATA"
Private 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
'定义一个"NOTIFYICONDATA"类型的变量
Private TheData As NOTIFYICONDATA
Private result As Long
'添加图标
Public Sub InitIcon(ByVal frmhwnd As Long, ByVal iconhwnd As Long, Optional ByVal tip As String)
With TheData
.cbSize = Len(TheData)
.hwnd = frmhwnd
.hIcon = iconhwnd
.uID = 0
.szTip = tip & vbNullChar
.uFlags = NIF_ICON Or NIF_MESSAGE Or NIF_TIP
.uCallbackMessage = WM_SYSTEM_TRAYICON
End With
result = Shell_NotifyIcon(NIM_ADD, TheData)
'向Windows注入托盘重建消息
RegTrayIconMessage = RegisterWindowMessage("TaskbarCreated")
'DoEvents
'MsgBox TheData.hWnd
End Sub
'删除图标
Public Sub UnInitIcon()
TheData.uFlags = 0
Shell_NotifyIcon NIM_DELETE, TheData
'向Windows注入托盘重建消息
RegTrayIconMessage = RegisterWindowMessage("TaskbarCreated")
End Sub
'更改图标
Public Sub ReInitIcon(ByVal iconhwnd As Long)
With TheData
.hIcon = iconhwnd
'pic是图片狂PictureBox,存放图标文件
.uFlags = NIF_ICON
End With
Shell_NotifyIcon NIM_MODIFY, TheData
'向Windows注入托盘重建消息
RegTrayIconMessage = RegisterWindowMessage("TaskbarCreated")
End Sub
'更改提示信息
Public Sub ReTipInitIcon(ByVal tip As String)
With TheData
.szTip = tip & vbNullChar
'tip是字符串string,存储提示信息
.uFlags = NIF_TIP
'指明要对浮动提示进行设置
End With
Shell_NotifyIcon NIM_MODIFY, TheData
End Sub
'*************************************
' 托盘消息处理
'*************************************
Public Function WndProcIcon(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Select Case Msg
Case WM_SYSTEM_TRAYICON '托盘消息
If lParam = WM_MOUSEMOVE Then '在图标上移动鼠标
'不做任何事
ElseIf lParam = WM_LBUTTONDBLCLK Then '双击鼠标左键
'如果窗口最小化就显示窗口
formMain.WindowState = 0
formMain.Show
ElseIf lParam = WM_LBUTTONDOWN Then '鼠标左键按下
SetForegroundWindow hwnd
ElseIf lParam = WM_LBUTTONUP Then '鼠标左键释放
'不做任何事
ElseIf lParam = WM_RBUTTONDOWN Then '鼠标右键按下
SetForegroundWindow hwnd '关键的一步
ElseIf lParam = WM_RBUTTONUP Then '鼠标右键释放
' 弹出菜单
x.PopupMenu frmMenu.A
ElseIf lParam = WM_RBUTTONDBLCLK Then '双击鼠标右键
'不做任何事
End If
Case RegTrayIconMessage '托盘图标重建消息
Call InitIcon(formMain.hwnd, formMain.Icon.Handle, formMain.Caption)
Frm_Notify = True
End Select
'将消息传下去
WndProcIcon = CallWindowProc(lpHotKey, hwnd, Msg, wParam, lParam)
End Function
用法如下:
程序启动时:
Call InitIcon(Me.hwnd, Me.Icon.Handle, Me.Caption)
OldWndProc = GetWindowLong(Me.hwnd, GWL_WNDPROC)
SetWindowLong Me.hwnd, GWL_WNDPROC, AddressOf WndProcIcon
程序关闭时:
Call UnInitIcon
SetWindowLong formMain.hwnd, GWL_WNDPROC, OldWndProc
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询