VB. NET Programming-pallet Program

Source: Internet
Author: User

A pallet program is a program that creates an icon for this program in the system tray area (or state area) after the program runs, you can click the menu that appears to control the running status of the program. The pallet program has many advantages, such as no screen occupation, background operation, and easy control. So now more and more programs are made into pallets. In VB. NET, it is convenient and simple to compile the program. This is because VB. NET does not have its own class library. The class library used is a public class library provided by the. Net FrameWork for all. Net platform development languages-. Net FrameWork SDK. In this class library, a specific class is provided for compiling the pallet program. Calling these classes can achieve the program tray effect. This frees us from the hassle of its predecessor, VB, when dealing with such problems. The following describes how to use VB. NET to write a pallet program.

The program design and running environment in this article

(1) Windows 2000 Service

(2) Net Framework SDK official version

Compiling process of static pallets

The so-called static pallet program refers to the pallet program whose icons in the system tray area are in the static state after the program runs. The dynamic pallet program is the opposite. It refers to a type of pallet program that displays animation effects in the system tray area icon. Next we will discuss how VB. NET implements the static pallet program.

The. Net FrameWork SDK provides the policyicon component for compiling pallets. The yyicon component is a WinForm component located in the namespace "System. windows. in Forms, in VB. in the. NET program, you only need to create an instance of the yyicon component and assign a value to the "Icon" attribute of the NotifyIcon instance. In this way, a simple pallet program is completed. The following is the code for this simple pallet Program (Form1.vb ):

Public Class Form1
Inherits System. Windows. Forms. Form
# Region "code generated by Windows Form Designer"
Public Sub New ()
MyBase. New ()
'The call is required by the Windows Form Designer.
InitializeComponent ()
'Add any initialization after InitializeComponent () is called.
End Sub
'Form rewriting disposal to clear 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 forms designer required
Private components As System. ComponentModel. IContainer
'Note: The following procedure is required by the Windows Forms designer
'You can use the Windows Form Designer to modify this process.
'Do not use the code editor to modify it.
Friend WithEvents policyicon1 As System. Windows. Forms. policyicon
'Create a policyicon instance
Friend TrayIcon = New Icon ("Tray. ico ")
'Create an Icon instance
<System. Diagnostics. DebuggerStepThrough ()>
Private Sub InitializeComponent ()
Me. components = New System. ComponentModel. Container ()
Me. policyicon1 = New System. Windows. Forms. policyicon (Me. components)
Me. policyicon1.text = "policyicon1"
Me. policyicon1.visible = True
'Assign a value to the Icon attribute of the policyicon instance to complete the simple tray program.
Me. policyicon1.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 the program
Module Module1
Sub Main ()
Application. Run (new Form1 ())
End sub
End Module

But this pallet program is not really a pallet program, because it still has many specific functions not implemented, the following lists these functions and introduces the specific implementation methods

(1). After the pallet program hides the window, the program should not be displayed in the taskbar, and generally does not display the window after running:

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

'The Setting program should not be displayed on the taskbar
Me. ShowInTaskbar = False
'Set minimum after running the program
Me. WindowState = System. Windows. Forms. FormWindowState. Minimized

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

To add a menu to a policyicon instance, you must first create a ContextMenu instance. This instance is mainly used to represent a shortcut menu. The menu items are implemented by creating a MenuItem instance, there are several menu items in the tray program, and several MenuItem instances are created. Add these menu items to the ContextMenu instance, and assign this instance to the ContextMenu attribute of the yyicon instance. In this way, right-click the pop-up menu of the tray program and complete the process. The code below is as follows:

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 policyicon instance:

Me. menuitem1 = new system. Windows. Forms. menuitem ()
Me. menuitem2 = new system. Windows. Forms. menuitem ()
Me. menuitem3 = new system. Windows. Forms. menuitem ()
Me. policyicon1.contextmenu = me. contextmenu1
Me. policyicon1.text = "VB. NET pallet program"
Me. policyicon1.visible = true
'Set the position of the pallet application tray area.
Me. policyicon1.icon = trayicon
'Add a single dish in 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 = "display form"
Me. menuitem2.index = 1
Me. menuitem2.text = "hide form"
Me. menuitem3.index = 2
Me. menuitem3.text = "quit"

After the contextmenu instance is assigned the contextmenu attribute of the notifyicon instance, the default state of the pallet program is that when you right-click the tray icon, the corresponding menu will pop up. In this case, you can define corresponding events and specific handling methods for each menu item. The compilation of a complete static tray program is complete.
Note that the icons of the pallet program in this article are not implemented by creating resource files, but by creating an icon instance. Therefore, when running the program, an icon file must exist in the current directory of the program, and the name of this icon file is "tray. ICO ". The following is a complete code list (form2.vb) of the static pallet program ):

Public class form1
Inherits system. Windows. Forms. Form
# Region "code generated by Windows Form Designer"
Public sub new ()
Mybase. New ()
'The call is required by the Windows Form Designer.
Initializecomponent ()
'Add any initialization after initializecomponent () is called.
End sub
'Form rewriting disposal to clear 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 forms designer required
Private components As System. ComponentModel. IContainer
'Note: The following procedure is required by the Windows Forms designer
'You can use the Windows Form Designer to modify this process.
'Do not use the code editor to modify it.
Friend WithEvents policyicon1 As System. Windows. Forms. policyicon
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. policyicon1 = new system. Windows. Forms. policyicon (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. policyicon1.contextmenu = me. contextmenu1
Me. policyicon1.text = "VB. NET pallet program"
Me. policyicon1.visible = true
'Set the position of the pallet application tray area.
Me. policyicon1.icon = trayicon
'Add a single dish in 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 = "display form"
Me. menuitem2.index = 1
Me. MenuItem2.Text = "hide form"
Me. MenuItem3.Index = 2
Me. MenuItem3.Text = "quit"
Me. AutoScaleBaseSize = New System. Drawing. Size (6, 14)
Me. ClientSize = New System. Drawing. Size (292,273)
Me. Name = "Form1"
'The Setting program should not be displayed on the taskbar
Me. ShowInTaskbar = False
Me. Text = "VB. NET WinForm programming tray program"
'Set minimum after running the program
Me. WindowState = System. Windows. Forms. FormWindowState. Minimized
End Sub
# End Region
'Display the pallet 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 the pallet program window
Private Sub MenuItem2_Click (ByVal sender As Object,
ByVal e As System. EventArgs) Handles MenuItem2.Click
Me. Hide ()
End Sub
'Launch the pallet program window
Private Sub MenuItem3_Click (ByVal sender As Object,
ByVal e As System. EventArgs) Handles MenuItem3.Click
Policyicon1.dispose ()
Application. Exit ()
End Sub
'Double-click the icon to display the form.
Private Sub policyicon#doubleclick (ByVal sender As Object,
ByVal e As System. EventArgs) Handles policyicon1.doubleclick
Me. WindowState = FormWindowState. Normal
Me. Show ()
End Sub
End Class
'Start the program
Module Module1
Sub Main ()
Application. Run (new Form1 ())
End sub
End Module

After Form2.vb is compiled and connected using the following commands:

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

You can get form2.exe, which is the interface for running from2.exe:

Figure 01: pallet program running interface 01

Compiling process of dynamic pallets

The animation effect of the tray icon in the dynamic tray program is displayed because a timer group in the program

Switch the tray icon every other time. This article shows the dynamic effect through switching between two icons. If the reader has good and continuous icons, he can also set switching between multiple icons, you only need to modify the code in the "Tick" event in the Timer1 timer. The following describes how to compile the dynamic pallet program:

To create instances and variables in a program:

Friend WithEvents policyicon1 As System. Windows. Forms. policyicon
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 switch the Icon
Friend Icon1 = New Icon ("icon1.ico ")
Friend Icon2 = New Icon ("icon2.ico ")
'Provides an identifier for switching between different icons
Dim BeginFlag As Boolean = True
'Timer
Friend WithEvents Timer1 As System. Windows. Forms. Timer

Initialize an instance:

Me. components = New System. ComponentModel. Container ()
Me. policyicon1 = New System. Windows. Forms. policyicon (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. policyicon1.contextmenu = Me. ContextMenu1
Me. policyicon1.text = "VB. NET pallet program"
Me. policyicon1.visible = True
'Set the position of the pallet program tray area to display the default icon
Me. policyicon1.icon = icon1
'Add a single dish in 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 = "starting to rotate"
Me. menuitem2.index = 1
Me. menuitem2.text = "Stop rotation"
Me. menuitem3.index = 2
Me. menuitem3.text = "quit"
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 event corresponding to the menu items in the pallet program and the Specific Handling Method:

'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 tray Resources
Private sub menuitem3_click (byval sender as object,
Byval e as system. eventargs) handles menuitem3.click
Policyicon1.dispose ()
Application. Exit ()
End sub
'Determine the tray icon type based on the identifier
Private sub timereffectick (byval sender as object,
Byval e as system. eventargs) handles timer1.tick
If beginflag = true then
Policyicon1.icon = icon2
Beginflag = false
Else
Policyicon1.icon = icon1
Beginflag = true
End if
End sub

At this point, the main steps for writing a dynamic pallet program are described. It is the same as the above static pallet program. When running this program, you must ensure that the current directory of this program contains two icon files, and the names are "icon1.ico" and "icon2.ico ". The complete code list of the dynamic pallet program is not provided here. The reader only needs to overwrite the code of the above key steps to the corresponding position in form2.vb to obtain the source code file form3.vb of the dynamic pallet program. This should not be difficult. The following is the command for compiling form3.vb:

Vbc/R: system. dll/R: system. Windows. froms. dll/R: system. Drawing. dll form2.vb

After successful compilation and compilation, you will get form3.exe, which is the running interface of form3.exe:

Figure 02: pallet program running interface 02

Summary

Pallets are a popular program type. The two programs described in this article are also typical of the two. I hope you can understand and understand how to compile the program.

 

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.