我又在网上找了一些例子,同时加入了闪烁图标,代码在下面列出来
问题有2
1、MDI窗口如果在最小化前有子窗口活跃中,那么最小化后托盘图标点击不了(所以在RESIZE里有UNLOAD的代码出现)
2、托盘图标仍然需要点击两次才可以恢复窗口(而状态判定检测Formministatus是已经加入了的)
------MDIFORM代码------
Dim appli As Integer
Dim a As Integer
Dim Formministatus As Boolean
Private Sub MDIForm_MouseMove(Button As Integer, Shift As Integer, X As Single, y As Single)
Dim lMsg As Single
lMsg = X / Screen.TwipsPerPixelX
If Me.WindowState = vbMinimized And Formministatus = True Then
Select Case lMsg
Case LeftUp
Me.Show
Me.WindowState = vbMaximized
Case RightUp
PopupMenu Pomenu
End Select
End If
End Sub
Private Sub MDIForm_Resize()
If Me.WindowState = vbMinimized Then
Unload Form1
Unload Form11
Unload Form2
Unload Form21
Unload Form22
Unload Form3
Unload Form9
If appli = 0 Then
MsgBox "已最小化窗体,请点击恢复", vbOKOnly + vbInformation, "提示"
appli = 1
End If
Me.Hide
TrayAddIcon Me, "处理平台"
Formministatus = True
Timer1.Enabled = True
Timer1.Interval = 500
Else
Timer1.Enabled = False
Formministatus = False
TrayRemoveIcon
End If
End Sub
Private Sub MDIForm_Unload(Cancel As Integer)
TrayRemoveIcon
End Sub
Private Sub MDIForm_Load()
Me.Icon = Picture1(0).Picture
Dim m As String
m = Environ("USERNAME")
MDIForm1.Caption = "处理平台" & "
" & m
Load Form9
Me.SetFocus
End Sub
Private Sub MDIForm_QueryUnload(Cancel As Integer, UnloadMode As Integer)
TrayRemoveIcon
Unload Me
End Sub
Private Sub Timer1_Timer()
If a = 0 Then
Me.Icon = Picture1(0).Picture
a = a + 1
Else
Me.Icon = Picture1(1).Picture
a = 0
End If
TraychangeIcon
End Sub
Private Sub Toolbar1_ButtonClick(ByVal Button As MSComctlLib.Button)
Dim m As String
m = Environ("USERNAME")
Select Case (Button.Index)
Case 1
Me.PopupMenu Addnew
Case 2
Me.PopupMenu Settle
Case 3
Me.PopupMenu Findout
Case 4
Unload Me
End Select
End Sub
Private Sub showw_click()
Me.Show
Me.WindowState = vbMaximized
End Sub
Private Sub quitp_click()
TrayRemoveIcon
Unload Me
End Sub
------MODULE代码------
Option Explicit
Private Const LR_LOADFROMFILE = &H10
Private Const LR_LOADMAP3DCOLORS = &H1000
Private Const IMAGE_ICON = 1
Private Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long
Private Const NIF_MESSAGE = &H1
Private Const NIF_ICON = &H2
Private Const NIF_TIP = &H4
Private Const NIF_STATE = &H8
Private Const NIF_INFO = &H10
Private Const NIM_ADD = &H0
Private Const NIM_MODIFY = &H1
Private Const NIM_DELETE = &H2
Private Const NIM_SETFOCUS = &H3
Private Const NIM_SETVERSION = &H4
Private Const NIM_VERSION = &H5
Private Const WM_USER As Long = &H400
Private Const NIN_BALLOONSHOW = (WM_USER + 2)
Private Const NIN_BALLOONHIDE = (WM_USER + 3)
Private Const NIN_BALLOONTIMEOUT = (WM_USER + 4)
Private Const NIN_BALLOONUSERCLICK = (WM_USER + 5)
Private Const NOTIFYICON_VERSION = 3
Private Const NIS_HIDDEN = &H1
Private Const NIS_SHAREDICON = &H2
Private Const WM_NOTIFY As Long = &H4E
Private Const WM_COMMAND As Long = &H111
Private Const WM_CLOSE As Long = &H10
Private Const WM_MOUSEMOVE As Long = &H200
Private Const WM_LBUTTONDOWN As Long = &H201
Private Const WM_LBUTTONUP As Long = &H202
Private Const WM_LBUTTONDBLCLK As Long = &H203
Private Const WM_MBUTTONDOWN As Long = &H207
Private Const WM_MBUTTONUP As Long = &H208
Private Const WM_MBUTTONDBLCLK As Long = &H209
Private Const WM_RBUTTONDOWN As Long = &H204
Private Const WM_RBUTTONUP As Long = &H205
Private Const WM_RBUTTONDBLCLK As Long = &H206
Public Enum bFlag
NIIF_NONE = &H0
NIIF_INFO = &H1
NIIF_WARNING = &H2
NIIF_ERROR = &H3
NIIF_GUID = &H5
NIIF_ICON_MASK = &HF
NIIF_NOSOUND = &H10
End Enum
Private Type NOTIFYICONDATA
cbSize As Long
hWnd As Long
uID As Long
uFlags As Long
uCallbackMessage As Long
hIcon As Long
szTip As String * 128
dwState As Long
dwStateMask As Long
szInfo As String * 256
uTimeoutAndVersion As Long
szInfoTitle As String * 64
dwInfoFlags As Long
End Type
Public Enum TrayRetunEventEnum
MouseMove = &H200
LeftUp = &H202
LeftDown = &H201
LeftDbClick = &H203
RightUp = &H205
RightDown = &H204
RightDbClick = &H206
MiddleUp = &H208
MiddleDown = &H207
MiddleDbClick = &H209
BalloonClick = (WM_USER + 5)
End Enum
Public ni As NOTIFYICONDATA
Public Sub TrayAddIcon(ByVal MyForm As Form, ByVal ToolTip As String, Optional ByVal bFlag As bFlag)
With ni
.cbSize = Len(ni)
.hWnd = MDIForm1.hWnd
.uID = vbNull
.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
.uCallbackMessage = WM_MOUSEMOVE
.hIcon = MDIForm1.Icon
.szTip = ToolTip & vbNullChar
End With
Shell_NotifyIcon NIM_ADD, ni
End Sub
Public Sub TraychangeIcon()
With ni
.hIcon = MDIForm1.Icon
End With
Shell_NotifyIcon NIM_MODIFY, ni
End Sub
Public Sub TrayRemoveIcon()
Shell_NotifyIcon NIM_DELETE, ni
End Sub
Public Sub TrayBalloon(ByVal MyForm As Form, ByVal sBaloonText As String, sBallonTitle As String, Optional ByVal bFlag As bFlag)
With ni
.cbSize = Len(ni)
.hWnd = MDIForm1.hWnd
.uID = vbNull
.uFlags = NIF_INFO
.dwInfoFlags = bFlag
.szInfoTitle = sBallonTitle & vbNullChar
.szInfo = sBaloonText & vbNullChar
End With
Shell_NotifyIcon NIM_MODIFY, ni
End Sub
Public Sub TrayTip(ByVal MyForm As Form, ByVal sTipText As String)
With ni
.cbSize = Len(ni)
.hWnd = MDIForm1.hWnd
.uID = vbNull
.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
.szTip = sTipText & vbNullChar
End With
Shell_NotifyIcon NIM_MODIFY, ni
End Sub