Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Private Const WS_POPUP = &H80000000 Private Const GWL_STYLE = (-16) Private Const WS_EX_APPWINDOW = &H40000 Private Const GWL_EXSTYLE = (-20) Private Const SW_HIDE = 0 Private Const SW_SHOW = 5 Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As LongPrivate Sub Command1_Click() Dim winstyle As Long Dim hwndParent As Long Dim childhwnd As Long childhwnd = FindWindow(vbNullString, "Form1") winstyle = GetWindowLong(childhwnd, GWL_EXSTYLE) winstyle = winstyle And Not WS_EX_APPWINDOW ShowWindow childhwnd, SW_HIDE ' 一定要先隐藏然后显示 SetWindowLong childhwnd, GWL_EXSTYLE, winstyle ShowWindow childhwnd, SW_SHOW End Sub
对窗口Form_Resize 事件进行处理: Private Sub Form_Resize() Me.Visible =not (Me.WindowState =1) '最小化时隐藏。 End Sub
Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As LongPublic 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 TypePrivate TheData As NOTIFYICONDATAPublic Sub AddToTray(frm As Form, mnu As Menu) Set TheForm = frm Set TheMenu = mnu
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Const WS_POPUP = &H80000000
Private Const GWL_STYLE = (-16)
Private Const WS_EX_APPWINDOW = &H40000
Private Const GWL_EXSTYLE = (-20)
Private Const SW_HIDE = 0
Private Const SW_SHOW = 5
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As LongPrivate Sub Command1_Click()
Dim winstyle As Long
Dim hwndParent As Long
Dim childhwnd As Long
childhwnd = FindWindow(vbNullString, "Form1")
winstyle = GetWindowLong(childhwnd, GWL_EXSTYLE)
winstyle = winstyle And Not WS_EX_APPWINDOW
ShowWindow childhwnd, SW_HIDE ' 一定要先隐藏然后显示
SetWindowLong childhwnd, GWL_EXSTYLE, winstyle
ShowWindow childhwnd, SW_SHOW
End Sub
Private Sub Form_Resize()
Me.Visible =not (Me.WindowState =1) '最小化时隐藏。
End Sub
cbSize As Long
hwnd As Long
uID As Long
uFlags As Long
uCallbackMessage As Long
hIcon As Long
szTip As String * 64
End TypePrivate TheData As NOTIFYICONDATAPublic Sub AddToTray(frm As Form, mnu As Menu) Set TheForm = frm
Set TheMenu = mnu
OldWindowProc = SetWindowLong(frm.hwnd, GWL_WNDPROC, AddressOf NewWindowProc)
With TheData
.uID = 0
.hwnd = frm.hwnd
.cbSize = Len(TheData)
.hIcon = frm.Icon.Handle
.uFlags = NIF_ICON
.uCallbackMessage = TRAY_CALLBACK
.uFlags = .uFlags Or NIF_MESSAGE
.cbSize = Len(TheData)
End With
Shell_NotifyIcon NIM_ADD, TheData
End Sub