网站首页/技术开发列表/内容

对于任务栏式的托盘图标及其右键菜单的完成!

技术开发2022-05-09阅读
实现方法:(用VS.net的可视化实现)
新建一个项目(notifyicon)
把右键菜单(contextmenu)拖入表单中.加入菜单的子菜单项。比如“显示”、“隐藏”等菜单项。
双击各菜单项为各菜单项加入单击事件。
比如:(显示)me.show
 (隐藏)me.hide
把托盘控件拖入表单中。设置其显示文字(.text)、图标文件(icon)
设置托盘的右键菜单(contextmenu)为你刚才创建的菜单。
这样就很方便的实现以前VB需要好多的Win API才能实现的功能。

附上该form的源代码
Public Class notifyIcon
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents NotifyIcon1 As System.Windows.Forms.NotifyIcon
Friend WithEvents ContextMenu1 As System.Windows.Forms.ContextMenu
Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem
Friend WithEvents MenuItem2 As System.Windows.Forms.MenuItem
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container()
Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(notifyIcon))
Me.Button1 = New System.Windows.Forms.Button()
Me.NotifyIcon1 = New System.Windows.Forms.NotifyIcon(Me.components)
Me.ContextMenu1 = New System.Windows.Forms.ContextMenu()
Me.MenuItem1 = New System.Windows.Forms.MenuItem()
Me.MenuItem2 = New System.Windows.Forms.MenuItem()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(128, 48)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(88, 24)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"
'
'NotifyIcon1
'
Me.NotifyIcon1.ContextMenu = Me.ContextMenu1
Me.NotifyIcon1.Icon = New System.Drawing.Icon("C:\root.ico")
Me.NotifyIcon1.Text = "NotifyIcon and ContextMenu"
Me.NotifyIcon1.Visible = True
'
'ContextMenu1
'
Me.ContextMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem1, Me.MenuItem2})
'
'MenuItem1
'
Me.MenuItem1.Index = 0
Me.MenuItem1.Text = "显示"
'
'MenuItem2
'
Me.MenuItem2.Index = 1
Me.MenuItem2.Text = "隐藏"
'
'notifyIcon
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Name = "notifyIcon"

End Sub

#End Region


Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem1.Click
Me.Show()
End Sub

Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click
Me.Hide()
End Sub
End Class

……

相关阅读