'1.为窗体添加菜单
'为窗体添加菜单: mFast
'为 mFast 添加下级子菜单 mmFast,并将 mmFast 的索引设置为 0
'2.在窗体上添加四个控件,所有控件均采用默认设置:
'Label1,Command1,Command2,Command3
Dim ctTrayIco As NotifyIconData
Private Sub Form_Load()
Me.Caption = "系统托盘例子"
'为数组菜单 mmFast 添加条目
mFast.Visible = False
LoadKjZu mmFast, "显示 " &Me.Caption &"(&R)", "mmFast-Run"
LoadKjZu mmFast, "最小化窗口(&N)", "mmFast-Min"
LoadKjZu mmFast, "菜单例子 1 (&A)", "mmFast-A"
LoadKjZu mmFast, "菜单例子 2 (&B)", "mmFast-B"
LoadKjZu mmFast, "-" '菜单分隔条
LoadKjZu mmFast, "退出(X)", "mmFast-Exit"
Dim S As Single
Label1.AutoSize = True: S = Label1.Height
Label1.Caption = Me.Caption: Label1.Move S, S
Command1.Caption = "添加到系统托盘": Command1.Move S, S * 3, S * 11, S * 2
Command2.Caption = "最小化到系统托盘": Command2.Move S, S * 6, S * 11, S * 2
Command3.Caption = "从系统托盘删除图标": Command3.Move S, S * 9, S * 11, S * 2
'将窗口函数的地址设置为模块中的 WndProc 过程,当用户
'在系统托盘图标单击鼠标右键时,d出自定义的菜单 mFast
WinAddress Me.hWnd
''如果需要程序一启动就添加到系统托盘,解除下面语句的注释即可
'Call SysTray
End Sub
Private Sub Form_Resize()
'设置快捷菜单的是否可用状态
Dim nEnabled As Boolean
On Error Resume Next
nEnabled = Me.WindowState = vbMinimized
mmFast(KjZuIndex(mmFast, "mmFast-Min")).Enabled = nEnabled
mmFast(KjZuIndex(mmFast, "mmFast-Run")).Enabled = Not nEnabled
End Sub
Private Sub Form_Unload(Cancel As Integer)
SysTray True '退出时,从系统托盘删除本程序图标
WinAddress Me.hWnd, True '退出时,将窗口地址还原
End Sub
Private Sub LoadKjZu(Kj As Object, nCap As String, Optional nTag As String)
'为数组控件添加一个成员
Dim I As Long
I = Kj.UBound
If Kj(I).Tag <>"" Then
I = I + 1
Load Kj(I): Kj(I).Visible = True
End If
Kj(I).Caption = nCap
If nCap = "-" Then Kj(I).Tag = "bar" Else Kj(I).Tag = nTag
End Sub
Private Function KjZuIndex(Kj As Object, nTag As String) As Long
'返回数组控件中 Tag 属性为 nTag 的成员索引,没有找到返回 -1
Dim I As Long
For I = Kj.LBound To Kj.UBound
If Kj(I).Tag <>nTag Then KjZuIndex = I: Exit Function
Next
KjZuIndex = -1
End Function
Private Sub mmFast_Click(Index As Integer)
'系统托盘快捷菜单
Dim nCmd As String
nCmd = Trim(mmFast(Index).Tag)
Select Case UCase(nCmd)
Case UCase("mmFast-Run"): Call WinNormal '正常显示主窗口
Case UCase("mmFast-Min"): Call WinMinimized '缩小到系统托盘
Case UCase("mmFast-Exit"): Unload Me '退出程序
Case UCase("mmFast-A"):MsgBox "这是系统托盘快捷菜单:mmFast-A", vbInformation, Me.Caption
'Case UCase("mmFast-B")
Case Else: MsgBox "此命令在“mmFast_Click”中无效:" &vbCrLf &vbCrLf &"nCmd = " &nCmd, vbInformation, Me.Caption &" - 无效命令"
End Select
End Sub
Private Sub Command1_Click()
Call SysTray '添加到系统托盘
End Sub
Private Sub Command2_Click()
Call WinMinimized '缩小到系统托盘
End Sub
Private Sub Command3_Click()
Call SysTray(True) '从系统托盘删除本程序图标
End Sub
Private Sub WinNormal()
'正常显示主窗口
Me.Visible = True
DoEvents
Me.WindowState = 0
End Sub
Private Sub WinMinimized()
'缩小到系统托盘
Me.WindowState = vbMinimized
Call SysTray: Me.Visible = False
Label1.Caption = "缩小到系统图标"
End Sub
Private Sub SysTray(Optional IcoDel As Boolean)
'在系统托盘为本程序添加图标
Dim dl As Long
If IcoDel Then
dl = Shell_NotifyIcon(NIM_DELETE, ctTrayIco)
If dl = 0 Then
Label1.Caption = "从系统托盘删除图标,失败"
Else
Label1.Caption = "已从系统托盘删除图标,成功"
End If
Exit Sub
End If
ctTrayIco.cbSize = Len(ctTrayIco)
ctTrayIco.hWnd = Me.hWnd
ctTrayIco.uID = SysTray_ID
ctTrayIco.uFlags = NIF_MESSAGE + NIF_ICON + NIF_TIP
ctTrayIco.uCallbackMessage = WM_Tray
ctTrayIco.hIcon = Me.Icon.Handle
ctTrayIco.szTip = "我的系统托盘图标" &Chr(0)
dl = Shell_NotifyIcon(NIM_ADD, ctTrayIco)
If dl = 0 Then
Label1.Caption = "图标添加到系统托盘,失败"
Else
Label1.Caption = "图标已添加到系统托盘,成功"
End If
End Sub
'■■以下是模块代码 ------------------------------------
Public Type NotifyIconData
cbSize As Long '结构大小,设置为:Len(NotifyIconData)
hWnd As Long '建立托盘图标窗体的句柄
uID As Long '托盘图标 ID 标识
uFlags As Long '对图标的 *** 作方式:
uCallbackMessage As Long '回调函数消息编号,设置为:WM_USER + ?
hIcon As Long'图标 Handle,设置为:Me.Icon.Handle
szTip As String * 64 '图标提示信息,设置为:"字符串" &Chr(0)
End Type
Public Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NotifyIconData) As Long
Public Const SysTray_ID = 1
Public Const NIM_ADD = &H0
Public Const NIM_DELETE = &H2
Public Const NIM_MODIFY = &H1
Public Const NIF_MESSAGE = &H1
Public Const NIF_ICON = &H2
Public Const NIF_TIP = &H4
Public Const WM_USER = &H400
Public Const WM_Tray = WM_USER + 2
Private 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
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
Public Const GWL_WNDPROC = (-4)
Public Const WM_KEYDOWN = &H100
Public Const WM_KEYUP = &H101
Public Const WM_CHAR = &H102
Public Const WM_SYSKEYDOWN = &H104
Public Const WM_SYSKEYUP = &H105
Public Const WM_SYSCHAR = &H106
Public Const WM_MOUSEMOVE = &H200
Public Const WM_LBUTTONDOWN = &H201
Public Const WM_LBUTTONUP = &H202
Public Const WM_RBUTTONDOWN = &H204
Public Const WM_RBUTTONUP = &H205
Dim moProc As Long
Public Function WndProc(ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
'这是自定义的窗口回调函数。调试时,最好不要在此过程中设置断点
If Msg = WM_Tray Then
If lParam = WM_RBUTTONUP Then Form1.PopupMenu Form1.mFast '在系统托盘图标上,d出快捷菜单
End If
WndProc = CallWindowProc(moProc, hWnd, Msg, wParam, lParam) '将窗口函数返还给 Windows 系统
End Function
Public Sub WinAddress(hWnd As Long, Optional nRevert As Boolean)
'设置或还原窗口函数的地址,nRevert=T 表示还原为原来的地址
If nRevert Then
SetWindowLong hWnd, GWL_WNDPROC, moProc '还原为原来的地址
Else
moProc = GetWindowLong(hWnd, GWL_WNDPROC) '记忆原来的地址
'将地址设置为 WndProc 过程,Windows 系统将自动调用本模块的 WndProc 过程
SetWindowLong hWnd, GWL_WNDPROC, AddressOf WndProc
End If
End Sub
现在借助Window Minimizer这款小工具的帮忙便实现以上 *** 作了。软件下载完毕后进行解压,由于是绿色软件并不需安
装,双击应用程序图标运行Window Minimizer,这时该软件程序图
标会显示在系统托盘区域中。如果想将某个任务栏窗口最小化到系
统托盘,可以直接在其窗口按钮上单击一下鼠标右键,然后在d出
的菜单中选择最下方的“最小化到托盘区”一项命令就行了,此时该
窗口图标便移动到托盘中去了。以后如想将其恢到任务栏中,再用
鼠标左键在托盘中点击一下该窗口图标就可以了。
声明多个NotifyIcon实例不就行了?通知区域中的图标是一些进程的快捷方式,这些进程在计算机后台运行,如防病毒程序或音量控制。这些进程不会具有自己的用户界面。NotifyIcon 类提供了编写此功能的方法。 Icon 属性定义显示在通知区域中的图标。 图标的d出菜单由 ContextMenu 属性确定。 Text 属性分配工具提示文本。 要在通知区域中显示图标,必须将 Visible 属性设置为 true。
示例下面的代码示例演示如何使用 NotifyIcon 类在通知区域中显示某一应用程序的图标。 该示例演示如何设置 Icon、 ContextMenu、 Text 和 Visible 属性以及如何处理 DoubleClick 事件。 一个带有 “退出”项的 ContextMenu 被分配给 NotifyIcon ..::.ContextMenu 属性,使用户可以关闭应用程序。 当发生 DoubleClick 事件时,将通过调用 Form ..::.Activate 方法来激活该应用程序窗体。
using System
using System.Drawing
using System.Windows.Forms
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.NotifyIcon notifyIcon1
private System.Windows.Forms.ContextMenu contextMenu1
private System.Windows.Forms.MenuItem menuItem1
private System.ComponentModel.IContainer components
[STAThread]
static void Main()
{
Application.Run(new Form1())
}
public Form1()
{
this.components = new System.ComponentModel.Container()
this.contextMenu1 = new System.Windows.Forms.ContextMenu()
this.menuItem1 = new System.Windows.Forms.MenuItem()
// Initialize contextMenu1
this.contextMenu1.MenuItems.AddRange(
new System.Windows.Forms.MenuItem[] {this.menuItem1})
// Initialize menuItem1
this.menuItem1.Index = 0
this.menuItem1.Text = "E&xit"
this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click)
// Set up how the form should be displayed.
this.ClientSize = new System.Drawing.Size(292, 266)
this.Text = "Notify Icon Example"
// Create the NotifyIcon.
this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components)
// The Icon property sets the icon that will appear
// in the systray for this application.
notifyIcon1.Icon = new Icon("appicon.ico")
// The ContextMenu property sets the menu that will
// appear when the systray icon is right clicked.
notifyIcon1.ContextMenu = this.contextMenu1
// The Text property sets the text that will be displayed,
// in a tooltip, when the mouse hovers over the systray icon.
notifyIcon1.Text = "Form1 (NotifyIcon example)"
notifyIcon1.Visible = true
// Handle the DoubleClick event to activate the form.
notifyIcon1.DoubleClick += new System.EventHandler(this.notifyIcon1_DoubleClick)
}
protected override void Dispose( bool disposing )
{
// Clean up any components being used.
if( disposing )
if (components != null)
components.Dispose()
base.Dispose( disposing )
}
private void notifyIcon1_DoubleClick(object Sender, EventArgs e)
{
// Show the form when the user double clicks on the notify icon.
// Set the WindowState to normal if the form is minimized.
if (this.WindowState == FormWindowState.Minimized)
this.WindowState = FormWindowState.Normal
// Activate the form.
this.Activate()
}
private void menuItem1_Click(object Sender, EventArgs e) {
// Close the form, which closes the application.
this.Close()
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)