如何设置系统托盘,按窗体最小化按钮时缩小到任务栏(托盘图标保留),按关闭按钮时缩小到托盘?

zhucehaoduo 2002-02-26 11:09:52
这样既可用alt+tab进行切换,按关闭时才从任务栏中消失(即可选择普通windows显示或隐藏在托盘处)。
...全文
535 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhucehaoduo 2002-03-13
  • 打赏
  • 举报
回复
等我赚了钱之后,哈哈。
zhengchong 2002-03-12
  • 打赏
  • 举报
回复
我用vb做拉个托盘控件,想要吗?Email:zhengxchong@263.net
zhucehaoduo 2002-03-12
  • 打赏
  • 举报
回复
wjying(葡萄) ,以后如果代码错误很多,先声明一下,否则粘贴过去后查错的时间可耗不起。
顺便说一句,zyl910(910:分儿,我来了!中的代码中有一个地方把 flags 敲成了 flage,我记得在粘贴时用过并即时发现了,为方便以后的兄弟,特地说明一下,呵呵。
等该问题被挤出csdn前一页后,我会给分,谢谢各位。
jett 2002-02-27
  • 打赏
  • 举报
回复
我只是让你明白一些原理啦
上面那段有挺多错误
SetForegroundWindow 改成setwindowpos比较好具体声明你查一下吧
如果你不需要让窗体在最上层不用也可以
jett 2002-02-27
  • 打赏
  • 举报
回复
右上角关闭按钮触发Form_QueryUnload事件,
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)

End Sub
然后是form_unload
所以你可以在Form_QueryUnload里通过UnloadMode 控制
zhucehaoduo 2002-02-27
  • 打赏
  • 举报
回复
对窗体右上角的关闭按钮没反应,是为什么?CmdExit_Click()是右上角关闭按钮的事件吗?
ehappyhare 2002-02-26
  • 打赏
  • 举报
回复
wjying(葡萄) 给的答案有一点小错误:

Private Sub Form_Unload(Cancel As Integer)
'this removes the icon from the system tray
Shell_NotifyIcon NIM_DELETE, nid
End Sub

在以上代码中应加上一句
Cancel=true
否则不能达到"按关闭按钮时缩小到托盘"的目的。
zhucehaoduo 2002-02-26
  • 打赏
  • 举报
回复
等我晚上有时间试试。
skydg 2002-02-26
  • 打赏
  • 举报
回复
炸弹工作中
jett 2002-02-26
  • 打赏
  • 举报
回复
声明的部分
Option Explicit
Private 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 Const NIM_ADD = &H0
Private Const NIM_MODIFY = &H1
Private Const NIM_DELETE = &H2

'The following constant is the message sent when a mouse event occurs
'within the rectangular boundaries of the icon in the taskbar status
'area.
Private Const WM_MOUSEMOVE = &H200

'The following constants are the flags that indicate the valid
'members of the NOTIFYICONDATA data type.
Private Const NIF_MESSAGE = &H1
Private Const NIF_ICON = &H2
Private Const NIF_TIP = &H4

'The following constants are used to determine the mouse input on the
'the icon in the taskbar status area.

Private Const WM_LBUTTONDBLCLK = &H203 'Double-click
Private Const WM_LBUTTONDOWN = &H201 'Button down
Private Const WM_LBUTTONUP = &H202 'Button up
Private Const WM_RBUTTONDBLCLK = &H206 'Double-click
Private Const WM_RBUTTONDOWN = &H204 'Button down
Private Const WM_RBUTTONUP = &H205 'Button up

Private Declare Function Shell_NotifyIcon Lib "shell32" _
Alias "Shell_NotifyIconA" _
(ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean


Dim nid As NOTIFYICONDATA
jett 2002-02-26
  • 打赏
  • 举报
回复

Private Sub Form_Load()
Me.Show
Me.Refresh
With nid
.cbSize = Len(nid)
.hwnd = Me.hwnd
.uId = vbNull
.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
.uCallBackMessage = WM_MOUSEMOVE
.hIcon = Me.Icon
.szTip = "炸弹工作中" & vbNullChar
End With
Shell_NotifyIcon NIM_ADD, nid
End Sub

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
'this procedure receives the callbacks from the System Tray icon.
Dim Result As Long
Dim msg As Long
'the value of X will vary depending upon the scalemode setting
If Me.ScaleMode = vbPixels Then
msg = X
Else
msg = X / Screen.TwipsPerPixelX
End If
Select Case msg
Case WM_LBUTTONUP '514 restore form window
Me.WindowState = vbNormal
Result = SetForegroundWindow(Me.hwnd)
Me.Show
Case WM_LBUTTONDBLCLK '515 restore form window
Me.WindowState = vbNormal
Result = SetForegroundWindow(Me.hwnd)
Me.Show
Case WM_RBUTTONUP '517 display popup menu
Result = SetForegroundWindow(Me.hwnd)
Me.PopupMenu Me.mPopupSys
End Select
End Sub

Private Sub Form_Resize()
'this is necessary to assure that the minimized window is hidden
If Me.WindowState = vbMinimized Then Me.Hide
End Sub

Private Sub Form_Unload(Cancel As Integer)
'this removes the icon from the system tray
Shell_NotifyIcon NIM_DELETE, nid
End Sub

Private Sub mPopExit_Click()
'called when user clicks the popup menu Exit command
Unload Me
End Sub

Private Sub mPopRestore_Click()
'called when the user clicks the popup menu Restore command
Me.WindowState = vbNormal
Result = SetForegroundWindow(Me.hwnd)
Me.Show
End Sub

在FORM中编辑MENU EDITOR编辑一个INVISIBLE一及菜单和两个可见的二级彩单!具体命名见程序中所含!

Fanks 2002-02-26
  • 打赏
  • 举报
回复
你可以在真正关闭前将窗体隐藏,点击托盘再显示。
zhucehaoduo 2002-02-26
  • 打赏
  • 举报
回复
没人知道吗,我看到remind-me软件的就是这样写的,很适合,我最讨厌找了半天右下角,还得右键单击打开。
zyl910 2002-02-26
  • 打赏
  • 举报
回复
Private Sub Form_Unload(Cancel As Integer)
'this removes the icon from the system tray
Shell_NotifyIcon NIM_DELETE, nid
End Sub

能达到"按关闭按钮时缩小到托盘"的目的?!

Private Flags As Boolean

Private Sub CmdExit_Click()
  Flags=True
  Unload me
End Sub

Private Sub Form_Unload(Cancel As Integer)
  If Flage Then
    Shell_NotifyIcon NIM_DELETE, nid
  Else
    Cancel=True
    ……
    Shell_NotifyIcon NIM_ADD, nid '添加图标
  End If
End Sub
zhucehaoduo 2002-02-26
  • 打赏
  • 举报
回复
这个函数 SetForegroundWindow 没有定义,调试通不过。
另外,我希望程序运行时任务栏和图标照样显示,不知上面的代码能否实现。

7,785

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧