在vb窗体编程中怎样实现单击关闭按钮将程序最小化到系统托盘而不关闭程序。

没有啊... 没有啊 展开
 我来答
百度网友6e36e31
2011-08-13 · TA获得超过145个赞
知道小有建树答主
回答量:280
采纳率:0%
帮助的人:157万
展开全部
加上这些东东,和一楼的代码,有空的话再加个右键菜单
'model1.bas

Public Const DefaultIconIndex = 1 '图标缺省索引
Public Const WM_LBUTTONDOWN = &H201 '按鼠标左键
Public Const WM_RBUTTONDOWN = &H204 '按鼠标右键

Public Const NIM_ADD = 0 '添加图标
Public Const NIM_MODIFY = 1 '修改图标
Public Const NIM_DELETE = 2 '删除图标

Public Const NIF_MESSAGE = 1 'message 有效
Public Const NIF_ICON = 2 '图标操作(添加、修改、删除)有效
Public Const NIF_TIP = 4 'ToolTip(提示)有效

'API函数声明
'图标操作
Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long
'判断窗口是否最小化
Declare Function IsIconic Lib "user32" (ByVal hWnd As Long) As Long
'
'设置窗口位置和状态(position)的功能
Declare Function SetWindowPos Lib "user32" (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long

'定义类型
'通知栏图标状态
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

'函数定义
'添加图标至通知栏
Public Function Icon_Add(iHwnd As Long, sTips As String, hIcon As Long, IconID As Long) As Long
'参数说明:iHwnd:窗口句柄,sTips:当鼠标移到通知栏图标上时显示的提示内容
'hIcon:图标句柄,IconID:图标Id号
Dim IconVa As NOTIFYICONDATA
With IconVa
.hWnd = iHwnd
.szTip = sTips + Chr$(0)
.hIcon = hIcon
.uID = IconID
.uCallbackMessage = WM_LBUTTONDOWN
.cbSize = Len(IconVa)
.uFlags = NIF_MESSAGE Or NIF_ICON Or NIF_TIP
Icon_Add = Shell_NotifyIcon(NIM_ADD, IconVa)
End With
End Function
'删除通知栏图标(参数说明同Icon_Add)
Function Icon_Del(iHwnd As Long, lIndex As Long) As Long
Dim IconVa As NOTIFYICONDATA
Dim l As Long
With IconVa
.hWnd = iHwnd
.uID = lIndex
.cbSize = Len(IconVa)
End With
Icon_Del = Shell_NotifyIcon(NIM_DELETE, IconVa)
End Function
'修改通知栏图标(参数说明同Icon_Add)
Public Function Icon_Modify(iHwnd As Long, sTips As String, hIcon As Long, IconID As Long) As Long
Dim IconVa As NOTIFYICONDATA
With IconVa
.hWnd = iHwnd
.szTip = sTips + Chr$(0)
.hIcon = hIcon
.uID = IconID
.uCallbackMessage = WM_LBUTTONDOWN
.cbSize = Len(IconVa)
.uFlags = NIF_MESSAGE Or NIF_ICON Or NIF_TIP
Icon_Modify = Shell_NotifyIcon(NIM_MODIFY, IconVa)
End With
End Function
'窗体代码:
Private MinFlag As Boolean

' 机能 : 窗口大小改变时
' 机能说明 : 窗口大小改变。
' 备注 :
Private Sub Form_Resize()
'判断窗口是否最小化状态,并且是按最小化按纽后第一次发生Resize事件
If IsIconic(Me.hWnd) <> 0 And MinFlag = False Then
MinFlag = True
Me.Visible = False '隐藏窗口
'将窗口图标加入通知栏
Call Icon_Add(Me.hWnd, Me.Caption, Me.Icon, 0)
End If
End Sub

' 机能 : 点击托盘图标
' 机能说明 : 点击托盘图标。
' 备注 :
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
Dim l
l = x \ 15
' Form1.Caption = Button & " " & Shift & " " & x & " " & y & " " & l

'点击通知栏图标,用鼠标右键时调出弹出菜单

Select Case l
Case WM_LBUTTONDOWN: showfrm
Case WM_RBUTTONDOWN: Me.PopupMenu m_Main
End Select

'点击通知栏图标,用鼠标左键时,将通知栏图标改为另外的图标
'
End Sub

Private Sub m_Menu_Click(Index As Integer)
Select Case Index
Case 0: '当单击"还原"菜单时
showfrm
Case 1: '当单击"退出"菜单时
Dim ret As Integer
ret = MsgBox("您确定要退出系统吗?", vbYesNo)
If ret = vbYes Then
Call Icon_Del(Form1.hWnd, 0) '删除通知栏图标
Unload Me
End '退出程序
End If
End Select
End Sub

Private Sub showfrm()
Form1.Show '调出窗口
Form1.WindowState = 0
Call Icon_Del(Form1.hWnd, 0) '删除通知栏图标
Form1.SetFocus
MinFlag = False
End Sub
沙慧月03
2011-08-13 · TA获得超过2155个赞
知道大有可为答主
回答量:2717
采纳率:100%
帮助的人:3592万
展开全部
加上这个
Private Sub Form_Unload(Cancel As Integer)
Cancel = 1
隐藏到托盘的代码

End Sub
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
pals
2011-08-13 · TA获得超过1043个赞
知道小有建树答主
回答量:1307
采纳率:0%
帮助的人:332万
展开全部
Private Sub Form_Unload(Cancel As Integer)
Cancel = 1
me.windowstate=vbminimized
End Sub
追问
qq一样托盘的话该怎么办呢、、
追答
我知道有些程序是这样做的,技术上一点不难,只是复杂一点。

内事问百度,具体的代码可以在网上搜到很多

也可到VBGood.com网站上找到很多。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式