如何在程序最小化时在系统托盘中显示图表,而任务栏中没有,象金山词霸

powerword 2000-08-28 07:54:00
如何在程序最小化时在系统托盘中显示图表,而任务栏中没有,象金山词霸
...全文
211 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
liyang 2000-08-28
  • 打赏
  • 举报
回复
这是msdn中的例子:

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

Global Const NIM_ADD = 0
Global Const NIM_MODIFY = 1
Global Const NIM_DELETE = 2
Global Const NIF_MESSAGE = 1
Global Const NIF_ICON = 2
Global Const NIF_TIP = 4

Declare Function Shell_NotifyIconA Lib "SHELL32" _
(ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Integer
The following code is a function that takes the parameters that need to be set for the NOTIFYICONDATA type and returns a variable of this type. Add to Form1:



Private Function setNOTIFYICONDATA(hWnd As Long, ID As Long, _
Flags As Long, CallbackMessage As Long, Icon As Long, _
Tip As String) As NOTIFYICONDATA

Dim nidTemp As NOTIFYICONDATA

nidTemp.cbSize = Len(nidTemp)
nidTemp.hWnd = hWnd
nidTemp.uID = ID
nidTemp.uFlags = Flags
nidTemp.uCallbackMessage = CallbackMessage
nidTemp.hIcon = Icon
nidTemp.szTip = Tip & Chr$(0)

setNOTIFYICONDATA = nidTemp
End Function
The three procedures in this block of code call the function created in step 5 to add, modify, and remove systray icons. Add this code to Form1 also:



Private Sub Command1_Click()
'Add an icon. This procedure uses the icon specified in
'the Icon property of Form1. This can be modified as desired.

Dim i As Integer
Dim s As String
Dim nid As NOTIFYICONDATA

s = InputBox("Enter string:")
nid = setNOTIFYICONDATA(hWnd:=Form1.hWnd, _
ID:=vbNull, _
Flags:=NIF_MESSAGE Or NIF_ICON _
Or NIF_TIP, _
CallbackMessage:=vbNull, _
Icon:=Form1.Icon, _
Tip:=s)

i = Shell_NotifyIconA(NIM_ADD, nid)
End Sub

Private Sub Command2_Click()
'Modify an existing icon. This procedure uses the icon
'specified in the Icon property of Form1. This can be modified
'as desired.

Dim i As Integer
Dim s As String
Dim nid As NOTIFYICONDATA

s = InputBox("Enter string:")
nid = setNOTIFYICONDATA(hWnd:=Form1.hWnd, _
ID:=vbNull, _
Flags:=NIF_MESSAGE Or NIF_ICON _
Or NIF_TIP, _
CallbackMessage:=vbNull, _
Icon:=Form1.Icon, _
Tip:=s)

i = Shell_NotifyIconA(NIM_MODIFY, nid)
End Sub

Private Sub Command3_Click()
'Delete an existing icon.

Dim i As Integer
Dim nid As NOTIFYICONDATA

nid = setNOTIFYICONDATA(hWnd:=Form1.hWnd, _
ID:=vbNull, _
Flags:=NIF_MESSAGE Or NIF_ICON _
Or NIF_TIP, _
CallbackMessage:=vbNull, _
Icon:=Form1.Icon, _
Tip:="")

i = Shell_NotifyIconA(NIM_DELETE, nid)
End Sub
qiangsheng 2000-08-28
  • 打赏
  • 举报
回复
这样的问题以前问过很多了,请在以前的帖子里搜索一下吧。
bing71 2000-08-28
  • 打赏
  • 举报
回复
Form:

Option Explicit

Public LastState As Integer

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const WM_SYSCOMMAND = &H112
Private Const SC_MOVE = &HF010&
Private Const SC_RESTORE = &HF120&
Private Const SC_SIZE = &HF000&
Private Sub Form_Load()
If WindowState = vbMinimized Then
LastState = vbNormal
Else
LastState = WindowState
End If

AddToTray Me, mnuTray

SetTrayTip "VB Helper tray icon program"
End Sub

' Enable the correct tray menu items.
Private Sub Form_Resize()
Select Case WindowState
Case vbMinimized
mnuTrayMaximize.Enabled = True
mnuTrayMinimize.Enabled = False
mnuTrayMove.Enabled = False
mnuTrayRestore.Enabled = True
mnuTraySize.Enabled = False
Case vbMaximized
mnuTrayMaximize.Enabled = False
mnuTrayMinimize.Enabled = True
mnuTrayMove.Enabled = False
mnuTrayRestore.Enabled = True
mnuTraySize.Enabled = False
Case vbNormal
mnuTrayMaximize.Enabled = True
mnuTrayMinimize.Enabled = True
mnuTrayMove.Enabled = True
mnuTrayRestore.Enabled = False
mnuTraySize.Enabled = True
End Select

If WindowState <> vbMinimized Then _
LastState = WindowState
End Sub
' Important! Remove the tray icon.
Private Sub Form_Unload(Cancel As Integer)
RemoveFromTray
End Sub


Private Sub mnuFileExit_Click()
Unload Me
End Sub

Private Sub mnuTrayClose_Click()
Unload Me
End Sub


Private Sub mnuTrayMaximize_Click()
WindowState = vbMaximized
End Sub


Private Sub mnuTrayMinimize_Click()
WindowState = vbMinimized
End Sub


Private Sub mnuTrayMove_Click()
SendMessage hwnd, WM_SYSCOMMAND, _
SC_MOVE, 0&
End Sub


Private Sub mnuTrayRestore_Click()
SendMessage hwnd, WM_SYSCOMMAND, _
SC_RESTORE, 0&
End Sub


Private Sub mnuTraySize_Click()
SendMessage hwnd, WM_SYSCOMMAND, _
SC_SIZE, 0&
End Sub


模块:
Option Explicit

Public OldWindowProc As Long
Public TheForm As Form
Public TheMenu As Menu

Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long

Public Const WM_USER = &H400
Public Const WM_LBUTTONUP = &H202
Public Const WM_MBUTTONUP = &H208
Public Const WM_RBUTTONUP = &H205
Public Const TRAY_CALLBACK = (WM_USER + 1001&)
Public Const GWL_WNDPROC = (-4)
Public Const GWL_USERDATA = (-21)
Public Const NIF_ICON = &H2
Public Const NIF_TIP = &H4
Public Const NIM_ADD = &H0
Public Const NIF_MESSAGE = &H1
Public Const NIM_MODIFY = &H1
Public Const NIM_DELETE = &H2

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

Private TheData As NOTIFYICONDATA
' *********************************************
' The replacement window proc.
' *********************************************
Public Function NewWindowProc(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If Msg = TRAY_CALLBACK Then
' The user clicked on the tray icon.
' Look for click events.
If lParam = WM_LBUTTONUP Then
' On left click, show the form.
If TheForm.WindowState = vbMinimized Then _
TheForm.WindowState = TheForm.LastState
TheForm.SetFocus
Exit Function
End If
If lParam = WM_RBUTTONUP Then
' On right click, show the menu.
TheForm.PopupMenu TheMenu
Exit Function
End If
End If

' Send other messages to the original
' window proc.
NewWindowProc = CallWindowProc( _
OldWindowProc, hwnd, Msg, _
wParam, lParam)
End Function
' *********************************************
' Add the form's icon to the tray.
' *********************************************
Public Sub AddToTray(frm As Form, mnu As Menu)
' ShowInTaskbar must be set to False at
' design time because it is read-only at
' run time.

' Save the form and menu for later use.
Set TheForm = frm
Set TheMenu = mnu

' Install the new WindowProc.
OldWindowProc = SetWindowLong(frm.hwnd, _
GWL_WNDPROC, AddressOf NewWindowProc)

' Install the form's icon in the tray.
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
' *********************************************
' Remove the icon from the system tray.
' *********************************************
Public Sub RemoveFromTray()
' Remove the icon from the tray.
With TheData
.uFlags = 0
End With
Shell_NotifyIcon NIM_DELETE, TheData

' Restore the original window proc.
SetWindowLong TheForm.hwnd, GWL_WNDPROC, _
OldWindowProc
End Sub
' *********************************************
' Set a new tray tip.
' *********************************************
Public Sub SetTrayTip(tip As String)
With TheData
.szTip = tip & vbNullChar
.uFlags = NIF_TIP
End With
Shell_NotifyIcon NIM_MODIFY, TheData
End Sub
' *********************************************
' Set a new tray icon.
' *********************************************
Public Sub SetTrayIcon(pic As Picture)
' Do nothing if the picture is not an icon.
If pic.Type <> vbPicTypeIcon Then Exit Sub

' Update the tray icon.
With TheData
.hIcon = pic.Handle
.uFlags = NIF_ICON
End With
Shell_NotifyIcon NIM_MODIFY, TheData
End Sub

7,763

社区成员

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

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