Vb. NET Programming Tray Program

Source: Internet
Author: User
Tags exit resource
Programming | program design and operating environment of this program

(1) Windows Service

(2) Net Framework SDK Official edition



The process of writing static tray program


The static pallet program refers to the tray program in which the icon in the system tray area is in the static state after the program is run. The dynamic pallet program is exactly the opposite, it refers to the system Tray area icon rendering animation effect of a class of tray program. Here is a discussion of how vb.net is implementing a static pallet program.

The. Net FrameWork SDK provides a component for writing a pallet program: the NotifyIcon component. The NotifyIcon component is a WinForm component, located in the namespace "System.Windows.Forms", in the VB.net program, as long as you create an instance of the NotifyIcon component, and the "Icon" for the NotifyIcon instance Property assignment, such a simple pallet program is complete. The following is the code for this simple pallet program (Form1.vb):

Public Class Form1
Inherits System.Windows.Forms.Form
#Region "code generated by the Windows forms Designer"
Public Sub New ()
MyBase.New ()
' This call is required by the Windows Forms Designer.
InitializeComponent ()
' Add any initialization after the InitializeComponent () call
End Sub
' Form overrides disposition to clean up the component list.
Protected Overloads Overrides Sub Dispose (ByVal disposing as Boolean)
If disposing Then
If not (components are nothing) Then
Components. Dispose ()
End If
End If
Mybase.dispose (disposing)
End Sub
' Required by the Windows Forms Designer
Private Components as System.ComponentModel.IContainer
' NOTE: The following procedure is required by the Windows Forms Designer
' You can use the Windows Forms Designer to modify this procedure.
' Do not modify it using the Code Editor.
Friend WithEvents NotifyIcon1 as System.Windows.Forms.NotifyIcon
' Create a NotifyIcon instance
Friend TrayIcon = New Icon ("Tray.ico")
' Create an Icon instance
<system.diagnostics.debuggerstepthrough () >
Private Sub InitializeComponent ()
Me.components = New System.ComponentModel.Container ()
Me.notifyicon1 = New System.Windows.Forms.NotifyIcon (me.components)
Me.NotifyIcon1.Text = "NotifyIcon1"
Me.NotifyIcon1.Visible = True
' Assign value to icon property of NotifyIcon instance, complete simple pallet 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
' Start Program
Module Module1
Sub Main ()
Application.Run (New Form1 ())
End Sub
End Module



But this tray program is not really a tray program, because it has a lot of specific features did not implement, listed below these features, and introduce specific implementation methods

(1). After the tray program is hidden, the program should not be displayed in the taskbar, and the window will not be displayed after running normally:

This is done by setting the properties of the form, as follows:

' Set the program should not be displayed on the taskbar
Me.showintaskbar = False
' Set the program to minimize after running
Me.windowstate = System.Windows.Forms.FormWindowState.Minimized



(2). Define menus and related events in the pallet program:

To add a menu to the NotifyIcon instance, you first create a ContextMenu instance, which is primarily a shortcut menu, where the menu item is implemented by creating a MenuItem instance, and several menu items are created for the menu in the Tray program to create several MenuItem instances. The menu items are then added to the ContextMenu instance, and the instance is assigned to the ContextMenu property of the NotifyIcon instance, so that the tray program right click on the pop-up menu is completed. Here is the specific code:

Create ContextMenu instances and MenuItem instances:
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 property 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.contextmenu1
Me.NotifyIcon1.Text = "VB." NET Tray program "
Me.NotifyIcon1.Visible = True
' Set pallet tray location Display icon
Me.NotifyIcon1.Icon = TrayIcon
' Add a menu item to the ContextMenu instance
ME.CONTEXTMENU1.MENUITEMS.ADD (ME.MENUITEM1)
ME.CONTEXTMENU1.MENUITEMS.ADD (ME.MENUITEM2)
ME.CONTEXTMENU1.MENUITEMS.ADD (ME.MENUITEM3)
Me.MenuItem1.Index = 0
Me.MenuItem1.Text = "Show 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 property of the NotifyIcon instance, the default state of the tray program is when the mouse right-clicks the tray icon and the corresponding menu pops up. In this case, you can define the corresponding events and the specific handling methods for each of the menu items. The process of writing a complete static pallet program is complete.

Finally, the reader should note that because the icon for 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, you must have an icon file in the current directory of the program, and the name of the icon file is "Tray.ico". Here is the complete code listing for this static pallet program
(Form2.vb):
Public Class Form1
Inherits System.Windows.Forms.Form
#Region "code generated by the Windows forms Designer"
Public Sub New ()
MyBase.New ()
' This call is required by the Windows Forms Designer.
InitializeComponent ()
' Add any initialization after the InitializeComponent () call
End Sub
' Form overrides disposition to clean up the component list.
Protected Overloads Overrides Sub Dispose (ByVal disposing as Boolean)
If disposing Then
If not (components are nothing) Then
Components. Dispose ()
End If
End If
Mybase.dispose (disposing)
End Sub
' Required by the Windows Forms Designer
Private Components as System.ComponentModel.IContainer
' NOTE: The following procedure is required by the Windows Forms Designer
' You can use the Windows Forms Designer to modify this procedure.
' Do not 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.MenuItem
Friend WithEvents MenuItem3 as System.Windows.Forms.MenuItem
Friend TrayIcon = New Icon ("Tray.ico")
<system.diagnostics.debuggerstepthrough () >
Private Sub InitializeComponent ()
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 program "
Me.NotifyIcon1.Visible = True
' Set pallet tray location Display icon
Me.NotifyIcon1.Icon = TrayIcon
' Add a menu item to the ContextMenu instance
ME.CONTEXTMENU1.MENUITEMS.ADD (ME.MENUITEM1)
ME.CONTEXTMENU1.MENUITEMS.ADD (ME.MENUITEM2)
ME.CONTEXTMENU1.MENUITEMS.ADD (ME.MENUITEM3)
Me.MenuItem1.Index = 0
Me.MenuItem1.Text = "Show 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"
' Set the program should not be displayed on the taskbar
Me.showintaskbar = False
Me.Text = "VB." NET WinForm Programming Tray program "
' Set the program to minimize after running
Me.windowstate = System.Windows.Forms.FormWindowState.Minimized
End Sub
#End Region
' Show Tray program 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 sender as Object,
ByVal e as System.EventArgs) Handles Menuitem2.click
Me.hide ()
End Sub
' Eject 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 to 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
' Start Program
Module Module1
Sub Main ()
Application.Run (New Form1 ())
End Sub
End Module

Form2.vb after compiling and connecting with the following commands:

Vbc/r:system.dll/r:system.windows.froms.dll/r:system.drawing.dll Form2.vb

You can get Form2.exe, and the following figure is the From2.exe run interface:








Figure 01: Tray Program Run Interface 01




The process of writing dynamic pallet program


The tray icon in the dynamic tray program can render the animation effect because a timer group in the program

Every once in a while, the tray icon is constantly switched. This article is through the two icon switch to show the dynamic effect, if the reader has a good, continuous icon, can also set multiple icon switch, and this only need to modify the Timer1 timer in the "Tick" event in the code on it. The following are the specific steps for this dynamic pallet program:

To create an instance and variable 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
' Create an icon instance to toggle icons
Friend Icon1 = New Icon ("Icon1.ico")
Friend Icon2 = New Icon ("Icon2.ico")
' provides identifiers for switching between icons
Dim Beginflag as Boolean = True
' Timer
Friend WithEvents Timer1 as System.Windows.Forms.Timer



Initialize instance:

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 program "
Me.NotifyIcon1.Visible = True
' Set pallet tray location display default icon
Me.NotifyIcon1.Icon = Icon1
' Add a menu item to the ContextMenu instance
ME.CONTEXTMENU1.MENUITEMS.ADD (ME.MENUITEM1)
ME.CONTEXTMENU1.MENUITEMS.ADD (ME.MENUITEM2)
ME.CONTEXTMENU1.MENUITEMS.ADD (ME.MENUITEM3)
Me.MenuItem1.Index = 0
Me.MenuItem1.Text = "Start Turning"
Me.MenuItem2.Index = 1
Me.MenuItem2.Text = "Stop Turning"
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 Pallet Program "
Me.windowstate = System.Windows.Forms.FormWindowState.Minimized



Define the events for the menu items in the pallet program and how to handle them:

' Start the rotation of the tray icon
Private Sub Menuitem1_click (ByVal sender as System.Object,
ByVal e as System.EventArgs) Handles Menuitem1.click
timer1.enabled = True
End Sub
' Stop the rotation of 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 and clear the pallet resource
Private Sub Menuitem3_click (ByVal sender as Object,
ByVal e as System.EventArgs) Handles Menuitem3.click
Notifyicon1.dispose ()
Application.exit ()
End Sub
' According to the identifier, to determine the pallet 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



This is the main step to write dynamic pallet program, and the above static tray program is the same, in the running of this program must ensure that the current directory exists two icon files, and the name is "Icon1.ico" and "Icon2.ico." Here does not give a dynamic tray program complete code list, the reader only need to put the above key steps of the code to the corresponding location in the Form2.vb can get dynamic tray program source program file Form3.vb. This should not be difficult, the following is the command to compile Form3.vb:

Vbc/r:system.dll/r:system.windows.froms.dll/r:system.drawing.dll Form2.vb



Successfully compiled, connected to get Form3.exe, the following figure is the Form3.exe run interface:








Figure 02: Tray Program Run interface 02




Summarize


Tray program is now a more popular type of program. This article describes the two programs, but also a typical tray program two, I hope you understand and master the preparation of the tray program to help.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.