如何实现VB编写的程序点击最小化进入托盘区??

请问大家如何实现VB编写的程序点击最小化进入托盘区啊哦??我现在添加一个:Command1以后是可以实现的,可但是这样子会影响到我编写的程序的美观性。我想请问一下大家有没... 请问大家如何实现VB编写的程序点击最小化进入托盘区啊哦??我现在添加一个:Command1以后是可以实现的,可但是这样子会影响到我编写的程序的美观性。我想请问一下大家有没有什么好的方法可以让我点击窗口上面的那一个最小化的按钮就可以实现到托盘区啊哦??先谢谢大家了。如果有谁不会的希望不要回答,谢谢。 展开
 我来答
etain
2011-10-24
知道答主
回答量:14
采纳率:0%
帮助的人:13.1万
展开全部
给你个例子吧

建立一个frmmain主窗体
Option Explicit

Private Sub Form_Load()
'初始化imgIcon控件的Picture属性
imgIcon.Picture = LoadPicture(App.Path & "\" & "heart.ico")
mnuTrayShow.Enabled = False

'初始化NOTIFYICONDATA数据结构
Dim nid As NOTIFYICONDATA
nid.cbSize = Len(nid)
nid.hwnd = frmTemp.hwnd
nid.uId = 1&
nid.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
nid.uCallbackMessage = WM_NOTIFYICON
nid.hicon = imgIcon.Picture
nid.szTip = "系统托盘中的图标" & Chr(0)

'调用Shell_NotifyIcon函数将图标加入到系统托盘中
Shell_NotifyIcon NIM_ADD, nid
End Sub

Private Sub Form_Unload(Cancel As Integer)
'设置自定义类型的变量的内容
Dim nid As NOTIFYICONDATA
nid.cbSize = Len(nid)
nid.hwnd = frmTemp.hwnd
nid.uId = 1&
nid.uFlags = 0

'调用Shell_NotifyIcon方法来删除系统托盘中的图标
Shell_NotifyIcon NIM_DELETE, nid

Unload frmTemp
End Sub

Private Sub mnuTrayExit_Click()
Unload Me
End Sub

Private Sub mnuTrayHide_Click()
'隐藏窗体,并相应改变菜单项的状态
Me.Hide
mnuTrayHide.Enabled = False
mnuTrayShow.Enabled = True
End Sub

Private Sub mnuTrayIcon_Click()
'设置dlgOpen公用对话框的属性,以便取得相应的图标文件
dlgOpen.Filter = "图标文件(*.ico)|*.ico"
dlgOpen.Flags = cdlOFNFileMustExist
dlgOpen.ShowOpen
If dlgOpen.FileName = "" Then Exit Sub

'根据所取得图标文件,来改变imgIcon文件的属性
imgIcon.Picture = LoadPicture(dlgOpen.FileName)

'设置NOTIFYICONDATA数据结构
Dim nid As NOTIFYICONDATA
nid.cbSize = Len(nid)
nid.hwnd = frmTemp.hwnd
nid.uId = 1&
nid.uFlags = NIF_ICON
nid.hicon = imgIcon.Picture

'调用Shell_NotifyIcon函数来修改系统托盘中的图标
Shell_NotifyIcon NIM_MODIFY, nid
End Sub

Private Sub mnuTrayShow_Click()
'显示窗体,并相应改变菜单项的状态
Me.Show
mnuTrayHide.Enabled = True
mnuTrayShow.Enabled = False
End Sub

Private Sub mnuTrayTip_Click()
Dim str As String
str = InputBox("输入系统托盘中的图标的提示信息:", "提示信息")
If str = "" Then Exit Sub

'设置NOTIFYICONDATA数据结构
Dim nid As NOTIFYICONDATA
nid.cbSize = Len(nid)
nid.hwnd = frmTemp.hwnd
nid.uId = 1&
nid.uFlags = NIF_TIP
nid.szTip = str & Chr(0)

'调用Shell_NotifyIcon函数来修改系统托盘中的图标的提示
Shell_NotifyIcon NIM_MODIFY, nid
End Sub

再建立一个空窗体叫frmTemp

再建一个模块,名字随便了
Option Explicit

'声明Shell_NotifyIcon函数,用于将图标加入到系统托盘中
Public Declare Function Shell_NotifyIcon Lib "shell32" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean

'声明自定义的数据类型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 * 64
End Type

'声明常量
Public Const NIM_ADD = &H0
Public Const NIM_MODIFY = &H1
Public Const NIM_DELETE = &H2

Public Const NIF_MESSAGE = &H1
Public Const NIF_ICON = &H2
Public Const NIF_TIP = &H4

'定义SetWindowLong,用以改变frmTemp窗体的窗体函数
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long

'定义事件的常数代码
Public Const WM_LBUTTONDOWN = &H201
Public Const WM_RBUTTONDOWN = &H204
Public Const WM_USER = &H400

'定义自定义的事件WM_NOTIFYICON
Public Const WM_NOTIFYICON = WM_USER + &H100

'替换窗口处理函数
Public Const GWL_WNDPROC = (-4)

'该函数为frmTest的窗体处理函数。
Function DialogProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Dim bln As Boolean
bln = True
If uMsg = WM_NOTIFYICON Then
'表示对系统托盘中的程序进行操作
If lParam = WM_LBUTTONDOWN Then
'如果按下鼠标左键,则显示菜单
frmMain.Show
frmMain.mnuTrayShow.Enabled = False
frmMain.mnuTrayHide.Enabled = True
ElseIf lParam = WM_RBUTTONDOWN Then
'如果按下鼠标右键,则弹出菜单
frmMain.PopupMenu frmMain.mnuTray
Else
'否则,将程序控制权交给系统处理
bln = False
End If
Else
'否则,将程序控制权交给系统处理
bln = False
End If
DialogProc = bln
End Function

Sub Main()
Load frmTemp
frmMain.Show

'调用SetWindowLong函数来改变frmTemp窗体的窗体函数
Call SetWindowLong(frmTemp.hwnd, GWL_WNDPROC, AddressOf DialogProc)
End Sub

我就不详细解释了,看看注释,如果稍微会一点vb就能看懂了.建立工程也很简单,不再罗嗦

注意:把vbp的代码也给你,中间mnu开头的是自己建的菜单.中间HEART.ICO和SPADE.ICO是指2个在托盘中显示的图标.如果不自己改例子的代码,就把图标放到同目录下吧.
Type=Exe
Form=frmMain.frm
Object={F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0; COMDLG32.OCX
Form=frmTemp.frm
Module=mdlStandard; mdlStandard.bas
IconForm="frmMain"
Startup="Sub Main"
HelpFile=""
Title="TBIcon"
ExeName32="TBIcon.exe"
Command32=""
Name="TrayIcon"
HelpContextID="0"
CompatibleMode="0"
MajorVer=1
MinorVer=0
RevisionVer=5
AutoIncrementVer=1
ServerSupportFiles=0
VersionComments="Exemple VB4 "
VersionCompanyName="-"
VersionLegalCopyright="Benjamin Bourderon Benjab@msn.com"
CompilationType=0
OptimizationType=0
FavorPentiumPro(tm)=0
CodeViewDebugInfo=0
NoAliasing=0
BoundsCheck=0
OverflowCheck=0
FlPointCheck=0
FDIVCheck=0
UnroundedFP=0
StartMode=0
Unattended=0
Retained=0
ThreadPerObject=0
MaxNumberOfThreads=1

[MS Transaction Server]
AutoRefresh=1
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

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

类别

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

说明

0/200

提交
取消

辅 助

模 式