VB 最小化到托盘

发布时间:2008年12月26日      浏览次数:802 次
要实现窗口最小化必须满足以下条件:
1:按下最小化按钮,窗体不可见
2:最小化后,最小化窗体的图标必须出现在任务栏的通知区域中
3:当双击通知区域中的图标时,窗体又显示出来,同时通知区域中的图标消失.
要满足上面的条件:
1:首先屏蔽窗体中系统自带的最小化图标,即Form.MinButton=False
2:声明1个API函数:
Public Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias " Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long
注意:上面这个API函数在XP中没有别名,将别名去掉后为:
Public Declare Function Shell_NotifyIcon Lib "shell32.dll"(ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long
3:声明7个常数:
Public Const NIM_ADD = &H0
Public Const NIM_DELETE = &H2
Public Const NIF_ICON = &H2
Public Const NIF_MESSAGE = &H1
Public Const NIF_TIP = &H4
Public Const WM_MOUSEMOVE = &H200
Public Const WM_LBUTTONDBLCLK = &H203
4:定义1个NOTIFYICONDATA类型
Public Type NOTIFYICONDATA
cbSize as Long
hWnd as Long
uId as Long
uFlags as Long
uCallBackMessage as Long
hIcon as Long
szTip as String
End Type
5:声明一个nid的类型
Public nid as NOTIFYICONDATA
思路清晰后,开始编写代码
1:在窗体中画1个Command,它的Caption属性为:"最小化"
2:将窗体Form的MinButton的属性设为:"False"
3:在窗体中添加1个模块,模块中的代码为:
Public Declare Function Shell_NotifyIcon Lib "shell32.dll"(ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long
Public Const NIM_ADD = &H0
Public Const NIM_DELETE = &H2
Public Const NIF_ICON = &H2
Public Const NIF_MESSAGE = &H1
Public Const NIF_TIP = &H4
Public Const WM_MOUSEMOVE = &H200
Public Const WM_LBUTTONDBLCLK = &H203
Public Type NOTIFYICONDATA
cbSize as Long
hWnd as Long
uId as Long
uFlags as Long
uCallBackMessage as Long
hIcon as Long
szTip as String
End Type
Public nid as NOTIFYICONDATA
4:编写Command的单击事件:
Private Sub Command1_Click()
nid.cbSize = Len(nid)
nid.uId = vbNull
nid.hWnd = Me.hWnd
nid.uFlags = NIF_TIP Or NIF_MESSAGE Or NIF_ICON
nid.uCallBackMessage = WM_MOUSEMOVE
nid.hIcon = Me.Icon
nid.szTip = "窗体最小化"
Shell_NotifyIcon NIM_ADD, nid
Me.Hide
End Sub
5:编写窗体的MouseMove事件:
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim msg As Long
msg = X / 15
If msg = WM_LBUTTONDBLCLK Then
Me.Show
Shell_NotifyIcon NIM_DELETE, nid
End If
End Sub
按F5启动,单击Command,看看通知区域中是不是多了个窗体的图标?双击该图标,窗体又显示出来了!
免责声明:本站相关技术文章信息部分来自网络,目的主要是传播更多信息,如果您认为本站的某些信息侵犯了您的版权,请与我们联系,我们会即时妥善的处理,谢谢合作!