| < Free Open Study > |
|
Not everyone will want to use this, but I will show you how easy it is to create anovel, yet very simple UI in the system tray. Surprisingly, it can be as complex a menu as you want to create, but it is the simplest to create. You can do most of the work right in the Windows Forms Designer.
For this demonstration, you will create another new add-in called UISystray. By now, you should not need any instruction on creating an add-in with the Add-in Wizard. The only difference between this add-in and all of the others you have created is that you do not select the option to have the wizard create a UI menu for you.
Once you've created the add-in, the code for it will be very short and simple, because you did not have a UI built in. The code for the UISystray add-in is shown in Listing 7-15.
Listing 7-15: UISystray Add-in Code Generated by the Wizard
Imports Microsoft.Office.Core imports Extensibility imports System.Runtime.InteropServices Imports EnvDTE #Region " Read me for Add-in installation and setup information. " ' When run, the Add-in wizard prepared the registry for the Add-in. ' At a later time, if the Add-in becomes unavailable for reasons such as: ' 1) You moved this project to a computer other than the one it was ' originally created on. ' 2) You chose 'Yes' when presented with a message asking if you _ ' wish to remove the Add-in. ' 3) Registry corruption. ' you will need to re-register the Add-in by building the _ ' UISystraySetup project ' by right-clicking the project in the Solution Explorer, _ ' then choosing install. #End Region <GuidAttribute("C95EA7C5-2E2E-4A89-BE99-8027DFA3C358"), _ ProgIdAttribute("UISystray.Connect")> _ Public Class Connect Implements Extensibility.IDTExtensibility2 Implements IDTCommandTarget Dim applicationObject As EnvDTE.DTE Dim addInInstance As EnvDTE.AddIn Public Sub OnBeginShutdown(ByRef custom As System.Array) _ Implements Extensibility.IDTExtensibility2.OnBeginShutdown End Sub Public Sub OnAddInsUpdate(ByRef custom As System.Array) _ Implements Extensibility.IDTExtensibility2.OnAddInsUpdate End Sub Public Sub OnStartupComplete(ByRef custom As System.Array) _ Implements Extensibility.IDTExtensibility2.OnStartupComplete End Sub Public Sub OnDisconnection(ByVal RemoveMode As _ Extensibility.ext_DisconnectMode, _ ByRef custom As System.Array) _ Implements Extensibility.IDTExtensibility2.OnDisconnection End Sub Public Sub OnConnection(ByVal application As Object, _ ByVal connectMode As Extensibility.ext_ConnectMode, _ ByVal addInInst As Object, _ ByRef custom As System.Array) _ Implements Extensibility.IDTExtensibility2.OnConnection applicationObject = CType(application, EnvDTE.DTE) addInInstance = CType(addInInst, EnvDTE.AddIn) End Sub End Class
You should make several changes and additions to the Connect class code created by the wizard before putting it into Listing 7-15. The changes have been boldfaced in Listing 7-16 for ease of recognition and discussion.
Listing 7-16: Connect Class Code for System Tray Activation
Imports Microsoft.Office.Core imports Extensibility imports System.Runtime.InteropServices Imports EnvDTE #Region " Read me for Add-in installation and setup information. " ' When run, the Add-in wizard prepared the registry for the Add-in. ' At a later time, if the Add-in becomes unavailable for reasons such as: ' 1) You moved this project to a computer other than which is was ' originally created on. ' 2) You chose 'Yes' when presented with a message asking if you _ ' wish to remove the Add-in. ' 3) Registry corruption. ' you will need to re-register the Add-in by building the _ ' UISystraySetup project ' by right-clicking the project in the Solution Explorer, _ ' then choosing install. #End Region <GuidAttribute("C95EA7C5-2E2E-4A89-BE99-8027DFA3C358"), _ ProgIdAttribute("UISystray.Connect")> _ Public Class Connect Implements Extensibility.IDTExtensibility2 Implements IDTCommandTarget Dim oVB As EnvDTE.DTE Dim addInInstance As EnvDTE.AddIn Dim oFrm As Object Public Sub OnBeginShutdown(ByRef custom As System.Array) _ Implements Extensibility.IDTExtensibility2.OnBeginShutdown End Sub Public Sub OnAddInsUpdate(ByRef custom As System.Array) _ Implements Extensibility.IDTExtensibility2.OnAddInsUpdate End Sub Public Sub OnStartupComplete(ByRef custom As System.Array) _ Implements Extensibility.IDTExtensibility2.OnStartupComplete End Sub Public Sub OnDisconnection(ByVal RemoveMode As _ Extensibility.ext_DisconnectMode, _ ByRef custom As System.Array) _ Implements Extensibility.IDTExtensibility2.OnDisconnection oFrm.Dispose() End Sub Public Sub OnConnection(ByVal application As Object, _ ByVal connectMode As Extensibility.ext_ConnectMode, _ ByVal addInInst As Object, _ ByRef custom As System.Array) _ Implements Extensibility.IDTExtensibility2.OnConnection Dim frmIcon As New Form1() oVB = CType(application, EnvDTE.DTE) addInInstance = CType(addInInst, EnvDTE.AddIn) oFrm = frmIcon End Sub End Class
First, you make the usual name change from applicationObject to oVB, just to shorten it and in keeping with the convention you've used thus far. Because you're going to add a form to the project, you dimension oFrm as Object. You don't dimension it as New Form1, because doing that will cause the form object to be instantiated too soon in the activation of the add-in and you'll actually instantiate two copies of the form, and therefore have two copies of the add-in's icon in the system tray. You need this object dimensioned at the module level so that the OnDisconnection method can get to it in order to dispose of the form. Next, you'll add the one line of code to the OnDisconnection method that will dispose the form as the add-in is disconnected.
In the OnConnection method, you add several lines of code. You dimension a local object, frmIcon, as New Form1. This is an executable statement in VB .NET, which is different from VB 6.0. When this Dim statement is executed, the instance of Form1 is instantiated; therefore, there is no reason to even "Show" the form. The only other line of code added is to set the module-level object, oFrm, to the local instance of Form1, frmIcon. This creates the module-level object that the OnDisconnection method will use to dispose of the form when the add-in is shut down. The Connect class is now ready to load the form that will actually place the icon in the system tray.
Now, you will create a form that will place the icon in the system tray. This form will also have a context menu on it that will be displayed by a right-click on the add-in's icon in the system tray.
You start by adding a form to the project. First, select a NotifyIcon control from the Toolbox and place it on the form. Next, add an icon to the project. (I have selected a smiley-face icon.) You can copy any icon you want into the project directory and add it to the project by pressing Ctrl-D. You can also right-click the project in the Solution Explorer and choose Add New Item. Once the icon is in the project, go to the properties page for the NotifyIcon control and set the icon property to point to the icon.
Next, drag a ContextMenu control onto the form. Then add the menus as shown in Figure 7-6. To edit the ContextMenu, simply right-click the ContextMenu control and select the Edit Menu option. Finally, double-click each of the menu items to create an event handler for the respective menu item. Into each resulting menu item event handler, insert a display of a message box. Obviously, this menu is not going to do anything meaningful, but it demonstrates the handling of the Click event for each of the menu items.
Figure 7-6: Form with a NotifyIcon and a ContextMenu
The completed code for Form1 appears in Listing 7-17.
Listing 7-17: Form1 Code
Imports System.ComponentModel Imports System.Drawing Imports System.Windows.Forms Imports System.Resources Public Class Form1 Inherits System.Windows.Forms.Form Private mSmileyDisplayed As Boolean Public Sub New() MyBase.New() Form1 = Me 'This call is required by the Windows Form Designer. InitializeComponent() ' we don't use this form so hide it Me.Hide() 'set up the tray icon 'InitializeNotifyicon() End Sub Public Sub DisplayMsg1(ByVal sender As Object, ByVal e As System.EventArgs) MessageBox.Show("You clicked Display Message1 Menu") End Sub Public Sub DisplayMsg2(ByVal sender As Object, ByVal e As System.EventArgs) MessageBox.Show("You clicked Dispaly Message2 Menu Item") 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 Private components As System.ComponentModel.IContainer #Region " Windows Form Designer generated code " 'Required by the Windows Form Designer Dim WithEvents Form1 As System.Windows.Forms.Form '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. Private WithEvents notifyicon 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 WithEvents MenuItem5 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem6 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem4 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem7 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem8 As System.Windows.Forms.MenuItem Private Sub InitializeComponent() Me.components = New System.ComponentModel.Container() Dim resources As System.Resources.ResourceManager = New _ System.Resources.ResourceManager(GetType(Form1)) Me.notifyicon = New System.Windows.Forms.NotifyIcon(Me.components) Me.ContextMenu1 = New System.Windows.Forms.ContextMenu() Me.MenuItem1 = New System.Windows.Forms.MenuItem() Me.MenuItem5 = New System.Windows.Forms.MenuItem() Me.MenuItem6 = New System.Windows.Forms.MenuItem() Me.MenuItem2 = New System.Windows.Forms.MenuItem() Me.MenuItem3 = New System.Windows.Forms.MenuItem() Me.MenuItem4 = New System.Windows.Forms.MenuItem() Me.MenuItem7 = New System.Windows.Forms.MenuItem() Me.MenuItem8 = New System.Windows.Forms.MenuItem() ' 'notifyicon ' Me.notifyicon.ContextMenu = Me.ContextMenu1 Me.notifyicon.Icon = CType(resources.GetObject("notifyicon.Icon"), _ System.Drawing.Icon) Me.notifyicon.Text = "Right-click for menu" Me.notifyicon.Visible = True ' 'ContextMenu1 ' Me.ContextMenu1.MenuItems.AddRange(New System.Windows.Forms. MenuItem() {Me.MenuItem1, Me.MenuItem2, Me.MenuItem3, Me.MenuItem4}) ' 'MenuItem1 ' Me.MenuItem1.Index = 0 Me.MenuItem1.MenuItems.AddRange(New System.Windows.Forms. MenuItem() {Me.MenuItem5, Me.MenuItem6}) Me.MenuItem1.Text = "Messages 1" ' 'MenuItem5 ' Me.MenuItem5.Index = 0 Me.MenuItem5.Text = "Display Messag1" ' 'MenuItem6 ' Me.MenuItem6.Index = 1 Me.MenuItem6.Text = "Display Messag3" ' 'MenuItem2 ' Me.MenuItem2.Index = 1 Me.MenuItem2.Text = "Mesage 2" ' 'MenuItem3 ' Me.MenuItem3.Index = 2 Me.MenuItem3.Text = "-" ' 'MenuItem4 ' Me.MenuItem4.Index = 3 Me.MenuItem4.MenuItems.AddRange(New System.Windows.Forms. MenuItem() {Me.MenuItem7, Me.MenuItem8}) Me.MenuItem4.Text = "Message 4" ' 'MenuItem7 ' Me.MenuItem7.Index = 0 Me.MenuItem7.Text = "Display Msg5" ' 'MenuItem8 ' Me.MenuItem8.Index = 1 Me.MenuItem8.Text = "Display Msg6" ' 'Form1 ' Me.AccessibleRole = System.Windows.Forms.AccessibleRole.None Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(200, 168) Me.ControlBox = False Me.Enabled = False Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None Me.MaximizeBox = False Me.MinimizeBox = False Me.Name = "Form1" Me.Opacity = 0 Me.ShowInTaskbar = False Me.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide Me.StartPosition = System.Windows.Forms.FormStartPosition.Manual End Sub #End Region Private Sub ContextMenu1_Popup(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles ContextMenu1.Popup End Sub Private Sub MenuItem1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MenuItem1.Click End Sub Private Sub MenuItem2_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MenuItem2.Click MsgBox("Display msg2") End Sub Private Sub MenuItem4_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) notifyicon.Visible = False Me.Close() End Sub Private Sub MenuItem5_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MenuItem5.Click MsgBox("You clicked to display Msg1") End Sub Private Sub MenuItem6_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MenuItem6.Click MsgBox("You clicked to display Msg3") End Sub Private Sub MenuItem7_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MenuItem7.Click MsgBox("You clicked to display Msg5") End Sub Private Sub MenuItem8_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MenuItem8.Click MsgBox("You clicked to display Msg6") End Sub End Class
| < Free Open Study > |
|