The pallet program has many advantages, such as not occupying the screen, running in the background, facilitating control, etc. So now more and more programs have made a tray program. In VB.NET, writing a tray program is relatively convenient and simple. This is because VB.NET does not have a self-class library, and the class library it uses is the public category library -.NET Framework SDK provided for all .NET platform development languages in the .NET framework. In this class library, for the writing tray programs, the specific classes are provided, and these classes can be used to implement the tray effect of the program. Therefore, it is also getting rid of its predecessor VB in handling such problems. Below, you will learn about the two specific examples and master how to write a tray program with VB.NET.
Design and operating environment of this article
(1) Windows 2000 Service
(2) NET FRAMEWORK SDK official version
Static tray program writing process
The so-called static pallet program is a tray program that is in a stationary state after the program is running. The dynamic tray program is just in contrast, it refers to the system tray area icon
An animation effect of a type of tray program. Let's explore how VB.NET implements a static tray program.
The .NET Framework SDK provides a component for writing a tray program: Notifyicon component. The NotifyiCon component is a WinForm component, located in the namespace "System.Windows.Forms", in the VB.NET program, just create an instance of a Notifyicon component, and assign a value for the "icon" attribute of the Notifyicon instance, such a simple tray The program is completed. Here is the code for this simple tray program (Form1.vb):
Public Class Form1
Inherits System.Windows.Forms.form
#Region "Windows Form Designer Generated Code"
Public Sub New ()
Mybase.new ()
'This call is required for the Windows Form Designer.
InitializeComponent ()
'Add any initialization after INITIALIZECOMPONENT ()
End Sub
'Form rewriting disposal to clean 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
'Windows Form Designer
Private Components as System.comPonentModel.icontainer
'Note: The following process is necessary for the Windows Form Designer.
'You can modify this process using the Windows Form Designer.
'Don't modify it using the code editor.
Friend Withevents Notifyicon1 As System.Windows.Forms.notifyicon
'Creating a Notifyicon instance
Friend trayicon = new icon ("tray.ico")
'Creating an icon instance
Private subinitializeComponent ()
Me.components = new system.componentmodel.container ()
Me.Notifyicon1 = new system.windows.Forms.notifyicon (me.components) me.notifyicon1.text = "notifyicon1"
Me.Notifyicon1.visible = TRUE
'Assign a value for the ICON attribute of the Notifyicon instance, complete the simple tray program
Me.notifyicon1.icon = trayicon
Me.autoscalebasesize = new system.drawing.size (6, 14)
Me.ClientSize = new system.drawing.size (292, 273)
Me.Name = "Form1"
Me.Text = "Form1"
End Sub
#End region
END CLASS
'starting program
Module Module1
Sub main ()
Application.run (New Form1 ())
End Sub
End module
However, this pallet program is still not a truly tray program because it still has many specific features that have not been implemented, and these features are listed, and the specific implementation method is introduced.
(1). After the tray program is hidden window, the program should not be displayed in the taskbar, and the window is not displayed until it is running:
This is done by setting the properties of the form, as follows:
'Settings the program should not be displayed in the taskbar
Me.showintaskbar = false
'Minimize after setting the program
Me.WindowState = system.windows.forms.formwindowState.minimized
(2). Define menus and related events in tray programs:
Add a menu to the Notifyicon instance, first create a contextMenu instance, this instance is the primary role to show the shortcut menu, where the menu item is implemented by creating a MenuItem instance, there are several menu items in the board in the tray program, create a few menuItem Example. Then add these menu items to the ContextMenu instance, and assign this instance to the CONTEXTMENU attribute of the Notifyicon instance, so that the pallet is right-click on the menu that the pop-up is complete. Below is specific code:
Create a ContextMenu instance and a MenuItem instance:
Friend Withevents ContextMenu1 As System.Windows.Forms.ContextMenu
Friend Withevents Menuitem1 As System.Windows.Forms.MenuItem
Friend Withevents Menuitem2 As System.Windows.Forms.MenuItem
Friend Withevents Menuitem3 As System.Windows.Forms.MenuItem
Add these menu items to the ContextMenu instance and assign the contextMenu instance to the CONTEXTMENU attribute of the Notifyicon instance:
Me.Menuitem1 = new system.windows.Forms.MenuItem ()
Me.Menuitem2 = new system.windows.Forms.MenuItem ()
Me.Menuitem3 = new system.windows.Forms.MenuItem ()
Me.Notifyicon1.contextMenu = me.contextMenu1me.notifyicon1.text = "VB.NET tray"
Me.Notifyicon1.visible = TRUE
'Set the tray program tray area location display icon
Me.notifyicon1.icon = trayicon
'Add menu item in the ContextMenu instance
Me.ContextMenu1.MenuItems.Add (me.Menuitem1)
Me.ContextMenu1.MenuItems.Add (me.Menuitem2)
Me.ContextMenu1.MenuItems.Add (Me.MenuItem 3)
Me.Menuitem1.index = 0
Me.Menuitem1.text = "Display Form"
Me.Menuitem2.index = 1
Me.Menuitem2.text = "Hide Form"
Me.Menuitem3.index = 2
Me.Menuitem3.text = "Exit"
When the ContextMenu instance is assigned to the CONTEXTMENU attribute of the Notifyicon instance, the default state of the tray program is that the corresponding menu will pop up when the mouse is on the pallet icon. At this time, each menu item can be defined for the corresponding event and the specific processing method. A complete preparation process of a complete static tray program is complete.
Finally, readers should be invited to note that since the icon of the tray program in this article is not implemented by creating a resource file, it is done by creating an ICON instance. So when the program is running, there must be an icon file in the current directory of the program, and the name of this icon file is "TRAY.ICO". Below is the complete code list of this static tray program (FORM2.VB):
Public Class Form1
Inherits System.Windows.Forms.form
#Region "Windows Form Designer Generated Code"
Public Sub New ()
Mybase.new ()
'This call is required for the Windows Form Designer.
InitializeComponent ()
'Add any initialization after INITIALIZECOMPONENT ()
End Sub
'Form rewriting disposal to clean 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
'Windows Form Designer
Private Components as System.comPonentModel.icontainer
'Note: The following process is necessary for the Windows Form Designer.
'You can modify this process using the Windows Form Designer.
'Don't modify it using the code editor.
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.MenuitemFriend Withevents Menuitem3 as system.windows.Forms.MenuItem
Friend trayicon = new icon ("tray.ico")
Private subinitializeComponent ()
Me.components = new system.componentmodel.container ()
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 ()
Me.Menuitem3 = new system.windows.Forms.MenuItem ()
Me.Notifyicon1.contextMenu = me.contextMenu1
Me.Notifyicon1.text = "VB.NET tray"
Me.Notifyicon1.visible = TRUE
'Set the tray program tray area location display icon
Me.notifyicon1.icon = trayicon
'Add menu item in the ContextMenu instance
Me.ContextMenu1.MenuItems.Add (me.Menuitem1)
Me.ContextMenu1.MenuItems.Add (me.Menuitem2)
Me.ContextMenu1.MenuItems.Add (Me.MenuItem 3)
Me.Menuitem1.index = 0
Me.Menuitem1.text = "Display Form"
Me.Menuitem2.index = 1
Me.Menuitem2.text = "Hide Form"
Me.Menuitem3.index = 2
Me.Menuitem3.text = "Exit"
Me.autoscalebasesize = new system.drawing.size (6, 14)
Me.ClientSize = new system.drawing.size (292, 273)
Me.Name = "Form1"
'Settings the program should not be displayed in the taskbar
Me.showintaskbar = false
Me.Text = "VB.NET WinForm Programming Tray Program"
'Minimize after setting the program
Me.WindowState = system.windows.forms.formwindowState.minimized
End Sub
#End region
'Show tray programs window
Private Sub Menuitem1_Click (Byval Sender As System.Object,
Byval e as system.eventargs) Handles Menuitem1.click
Me.WindowState = FormWindowState.Normal
Me.show ()
End Sub
'Hide Tray Program Window
Private Sub Menuitem2_Click (Byval e as system.eventargs) Handles Menuitem2.click
Me.hide ()
End Sub
'Launch the tray program window
Private Sub Menuitem3_Click (byval sender as object,
Byval e as system.eventargs) Handles Menuitem3.click
Notifyicon1.dispose ()
Application.exit ()
End Sub
'Double-click icon display form
Private sub notifyicon1_doubleclick (Byval Sender As Object,
Byval e as system.eventargs) Handles Notifyicon1.doubleClick
Me.WindowState = FormWindowState.Normal
Me.show ()
End Sub
END CLASS
'starting program
Module Module1
Sub main ()
Application.run (New Form1 ())
End Sub
End module
FORM2.VB has been compiled by the following commands:
VBC /R :system.dll /r:system.windows.froms.dll /r:system.drawing.dll form2.vb
You can get Form2.exe, the picture below is the interface running from the from2.exe:
Dynamic tray program writing process
The reason why the tray icon in the dynamic tray program can be present
Animation effect is because of a timer group in the program
The tray icon is constantly switched every other time. This article shows the dynamic effect by cutting the two icons. If there is a good, continuous icon, the reader can set the switching of multiple icons, and this only needs to modify the "Tick" event in the Timer1 timer. The code is OK. Below is the specific steps of this dynamic tray program:
To create an instance and variables in a program:
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
Friend Withevents Menuitem3 As System.Windows.Forms.MenuItem
'Creating an Icon instance for switching icons
Friend icon1 = new icon ("icon1.ico")
Friend icon2 = new icon ("icon2.ico")
'Provide identifiers for switching for different icons
DIM Beginflag as Boolean = TRUE
'Timer
Friend Withevents Timer1 As System.Windows.Forms.Timer
Initialization example:
Me.components = new system.componentmodel.container ()
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 ()
Me.Menuitem3 = new system.windows.Forms.MenuItem ()
Me.Timer1 = new system.windows.Forms.timer (me.components)
Me.Notifyicon1.contextMenu = me.contextMenu1
Me.Notifyicon1.text = "VB.NET tray"
Me.Notifyicon1.visible = TRUE
'Setting the tray program tray area location display default icon
Me.notifyicon1.icon = icon1
'Add menu item in the ContextMenu instance
Me.ContextMenu1.MenuItems.Add (me.Menuitem1)
Me.ContextMenu1.MenuItems.Add (me.Menuitem2)
Me.ContextMenu1.MenuItems.Add (Me.MenuItem 3)
Me.Menuitem1.index = 0
Me.Menuitem1.text = "Start Tone"
Me.Menuitem2.index = 1
Me.Menuitem2.text = "Stop rotation"
Me.Menuitem3.index = 2
Me.Menuitem3.text = "Exit"
Me.autoscalebasesize = new system.drawing.size (6, 14)
Me.ClientSize = new system.drawing.size (292, 273)
Me.Name = "Form1"
Me.showintaskbar = false
Me.Text = "VB.NET WinForm Programming Dynamic Tray Program"
Me.WindowState = system.windows.forms.formwindowState.minimized
Define events corresponding to menu items in tray programs, and specific processing methods:
'Turn the pallet icon
Private Sub Menuitem1_Click (Byval Sender As System.Object,
Byval e as system.eventargs) Handles Menuitem1.click
Timer1.enabled = TRUE
End Sub
'Turning the tray icon
Private Sub Menuitem2_click (byval sender as object,
Byval e as system.eventargs) Handles Menuitem2.click
Timer1.enabled = FALSE
End Sub
'Close the program, clear tray resources
Private Sub Menuitem3_Click (byval sender as object,
Byval e as system.eventargs) Handles menuitem3.clicknotifyicon1.dispose ()
Application.exit ()
End Sub
'According to the identifier, determine the tray icon type
Private sub timer1_tick (byval sender as object,
Byval e as system.eventargs) Handles Timer1.Tick
If Beginflag = True Then
Notifyicon1.icon = icon2
Beginflag = false
Else
Notifyicon1.icon = icon1
Beginflag = TRUE
END IF
End Sub
The main steps of writing the dynamic tray program are introduced, and the above static tray programs are the same, when running this program, it must ensure that this program current directory has two ICON files, and the name is "icon1.ico" and "ICON2. ICO. The full code list of the dynamic tray program is not given here. The reader only needs to cover the code of these key steps to the corresponding location in Form2.vb, you can get the source of the dynamic tray program to obtain the source file FORM3.vb. This should not be difficult, below is compiling form3.vb command:
VBC /R :system.dll /r:system.windows.froms.dll /r:system.drawing.dll form2.vb
Successfully compiled, get Form3.exe after connection, the following figure is the run interface of Form3.exe:
to sum up
The tray program is a type of program that is now more popular. The two programs introduced in this article are also two of the more typical tray programs. I hope to understand and master the written tray programs.