主题:VB6.0如何最小化到托盘区
chenjun302
[专家分:80] 发布于 2007-12-12 18:06:00
各位大侠:
VB6如何实现:点击最小化按钮程序自动最小化到托盘区,双击再自动恢复啊????谢谢!
回复列表 (共6个回复)
沙发
tanchuhan [专家分:15140] 发布于 2007-12-12 18:40:00
API: Shell_NotifyIcon Function
板凳
yzai [专家分:20] 发布于 2008-03-01 21:38:00
我也想要。
这个Shell_NotifyIcon Function
不行
3 楼
一江秋水 [专家分:9680] 发布于 2008-03-02 09:30:00
在窗体上用菜单编辑器添加一个菜单项:标题与名称均为:mnuTray,设置其为隐藏,再随便添加
几个子菜单,具体的功能你可以根据你的具体需要来编辑
1.模块的代码
Option Explicit
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
Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long
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 Const NIF_ICON = &H2
Public Const NIF_TIP = &H4
Public Const NIM_ADD = &H0
Public Const NIF_MESSAGE = &H1
Public Const NIM_MODIFY = &H1
Public Const NIM_DELETE = &H2
Public OldWindowProc As Long
Public TheForm As Form
Public TheMenu As Menu
Public 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
Private TheData As NOTIFYICONDATA
'新的窗口程序,它将取代原来的窗口程序
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 ' 单击左键,恢复窗体
TheForm.WindowState = 0
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
'装载新的窗口程序
OldWindowProc = SetWindowLong(frm.hwnd, GWL_WNDPROC, AddressOf NewWindowProc)
' 将程序图标添加到系统托盘区
With TheData
.uID = 0
.hwnd = frm.hwnd
.cbSize = Len(TheData)
.hIcon = frm.Icon.Handle
.uFlags = NIF_ICON + NIF_TIP + NIF_MESSAGE
.uCallbackMessage = TRAY_CALLBACK
.uFlags = .uFlags Or NIF_MESSAGE
.cbSize = Len(TheData)
End With
Shell_NotifyIcon NIM_ADD, TheData
End Sub
Public Sub RemoveFromTray() '将图标从系统托盘区中删除
TheData.uFlags = 0
Shell_NotifyIcon NIM_DELETE, TheData
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
End Sub
2.窗体的代码:
Option Explicit
Private Sub Form_Load()
AddToTray Me, mnuTray '调用添加托盘图标子程序
SetTrayTip "王牌软件" '调用在托盘图标上显示提示的子程序
End Sub
Private Sub Form_Unload(Cancel As Integer)
On Error Resume Next
RemoveFromTray '在窗体退出后,删除托盘中的图标
End Sub
4 楼
xuranchun [专家分:270] 发布于 2008-03-02 12:41:00
要了,呵呵!
5 楼
yzai [专家分:20] 发布于 2008-03-17 23:45:00
mnuTray 变量未定义???
6 楼
superzx [专家分:0] 发布于 2008-03-19 17:35:00
vb.net方便实现!不用调用api
仅仅需要在窗体上放置NotifyIcon控件即能实现最小化到托盘的应用程序
Public Class Form1
Private Sub NotifyIcon1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles NotifyIcon1.DoubleClick
Me.ShowInTaskbar = True
Me.Show()
Me.WindowState = FormWindowState.Normal
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.ShowInTaskbar = False
Me.Hide()
End Sub
End Class
我来回复