You will create an application which can Log Off, Restart or Shut Down your Computer from the Task Tray / Notification Area, and have the application load on Startup.
Printer Friendly Download Tutorial (185KB) Download Source Code (13.1KB)
Step 1
Start Microsoft Visual Basic 2008 Express Edition, then select File then New Project... Choose Windows Forms Application from the New Project Window, enter a name for the Project and then click OK, see below:
Step 2
A Blank Form named Form1 should then appear, see below:
Step 3
With Form1 selected goto the Properties box and change the Name from Form1 to frmMain, Opacity to 0% and ShowInTaskBar to False
Step 4
Then from the Common Controls tab on the Toolbox select the NotifyIcon component:
Step 5
Draw a NotifyIcon on the Form, or double-click on the NotifyIcon entry in the Toolbox, and it will appear in the pane, beneath the form, see below:
Step 6
Then from the Menus & Toolbars tab on the Toolbox select the ContextMenuStrip Control, see Below:
Step 7
Draw the ContextMenuStrip onto the Form or double-click on the ContextMenuStrip Control entry in the Menu & Toolbars tab on the Toolbox, see below:
Step 8
Select or click on the ContextMenuStrip if it is not already selected, then goto the Properties box and change the Name to "cmuMain", see below:
Step 9
Select or click on the NotifyIcon, then goto the Properties box and change the Name to "trayIcon", the Text to "Shutdown" and then from the ContextMenuStrip select "cmuMain", all without the quotes, see below:
Step 10
Select or click on the ContextMenuStrip again then click or select where the ContextMenuStrip says "Type Here", an editable Textbox will appear, then type in "Log Off", then below that "Restart", then "Shutdown" all without quotes then type in "-" for a Seperator, then below that "Show at Startup", then "-" again for another Seperator, then finally type below that "Exit", see below:
Step 11
Right Click on the Form or the entry of the Form in Solution Explorer and choose the "View Code" option then above the "Public Class frmMain" type the following:
Imports Microsoft.Win32
Then below "Public Class frmMain" enter the following Declaration:
Dim Startup As Microsoft.Win32.RegistryKey = _ Registry.CurrentUser.CreateSubKey _ ("SOFTWARE\Microsoft\Windows\CurrentVersion\Run")
See Below:
Step 12
Double Click on the Menu Item Labeled "Log Off" (LogOffToolStripMenuItem) in the ContextMenuStrip, and type the following in the LogOffToolStripMenuItem_Click() Sub:
System.Diagnostics.Process.Start("Shutdown", "/L")
Double Click on the Menu Item Labeled "Restart" (RestartToolStripMenuItem) in the ContextMenuStrip, and type the following in the RestartToolStripMenuItem_Click() Sub:
System.Diagnostics.Process.Start("Shutdown", "/R")
Double Click on the Menu Item Labeled "Shutdown" (ShutdownToolStripMenuItem) in the ContextMenuStrip, and type the following in the ShutdownToolStripMenuItem_Click() Sub:
System.Diagnostics.Process.Start("Shutdown", "/S")
See Below:
Step 13
Double Click on the Menu Item Labeled "Show at Startup" (ShowAtStartupStripMenuItem) in the ContextMenuStrip, and type the following in the ShowAtStartupToolStripMenuItem_Click() Sub:
If ShowAtStartupToolStripMenuItem.Checked Then Startup.DeleteValue("StartupShutdown") ShowAtStartupToolStripMenuItem.Checked = False Else With Startup .OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", True) .SetValue("StartupShutdown", Windows.Forms.Application.ExecutablePath) End With ShowAtStartupToolStripMenuItem.Checked = True End If
See Below:
Step 14
Double Click on the Menu Item Labeled "Exit" (ExitToolStripMenuItem) in the ContextMenuStrip, and type the following in the ExitToolStripMenuItem_Click() Sub:
Me.Close
See Below:
Step 15
Return to the Design view by selecting the [Design] tab or Right Click on the "View Designer" option in Solution Explorer for frmMain. With the Form Displayed, Double Click on the Form (frmMain) and type the following in the frmMain_Load() Sub
trayIcon.Icon = Me.Icon trayIcon.Visible = True ShowAtStartupToolStripMenuItem.Checked = _ Not Startup.GetValue("StartupShutdown") Is Nothing
See Below:
Step 16
Return to the Code View, by double-clicking on the Form, this will show the frmMain_Load Sub from the previous step, in Code view are two drop-down boxes, select "Explorer" from the first drop-down with "(frmMain Events)", and then from the other "(Declarations)" select "FormClosed" and type the following in the frmMain_FormClosed(...) Sub:
trayIcon.Visible = False
End
See Below:
Step 17
Save the Project as you have now finished the application, then click on Start:
When you do the following will appear in the Task Tray:
Step 18
Now right click on the icon shown in the Task Tray and the ContextMenu with Exit on it should appear, see below:
Step 19
Click on Exit on the Menu to end the application.
This is a very simple application showing how you can Log Off, Restart and Shutdown your Computer from the Task Tray using the Shutdown process, plus Build and Run the Application and use the "Show at Startup" option to make sure it always remains in your Task Tray each time. You may also change the icon by changing the Form Icon to anything you want!