The TaskBar and VB.NET

A few days ago, a student asked me whether or not it is possible to manipulate the TaskBar from within a VB.NET program, so I decided to create this sample application for her. With this article, I will show you how to create an application that can set and apply most taskbar settings. Features include:

  • Lock the TaskBar
  • AutoHide the TaskBar
  • Keep TaskBar on top
  • Group similar TaskBar buttons
  • Hide TaskBar contents, in other words the active window buttons
  • Hide the clock
  • Disable the Start button
  • Auto Hide Inactive Tray icons
  • Change Start button text
  • Show Desktop feature
  • Show TaskBar properties

Design

I have designed my form as shown in Figure 1. Feel free to give your buttons your own descriptive names.

Figure 1: The Design

Coding

Add the following Import on top of your code window because it will use some Registry functions throughout this article:

Imports Microsoft.win32                   'Registry Functions
Imports System.Runtime.InteropServices    'API functions

To avoid confusion and possible ambiguities, I have decided to start with all the APIs and their associated constants. Now, add them to your code window:

'Find External Window
Private Declare Function FindWindow Lib "user32.dll" Alias _
"FindWindowA" (ByVal lpClassName As String, _
               ByVal lpWindowName As String) As Int32

'Find Child Window Of External Window
Private Declare Function FindWindowEx Lib "user32.dll" Alias _
"FindWindowExA" (ByVal hWnd1 As Int32, _
                 ByVal hWnd2 As Int32, _
                 ByVal lpsz1 As String, _
                 ByVal lpsz2 As String) As Int32

'Show A Window
Private Declare Function ShowWindow Lib "user32.dll" _
   (ByVal hwnd As Int32, _
    ByVal nCmdShow As Int32) As Int32

'Post Message To Window
Private Declare Function PostMessage Lib "user32.dll" Alias _
"PostMessageA" (ByVal hwnd As Int32, ByVal wMsg As Int32, _
                ByVal wParam As Int32, _
                ByVal lParam As Int32) As Int32

'Enable A Window
Private Declare Function EnableWindow Lib "user32.dll" _
   (ByVal hwnd As Int32, ByVal fEnable As Int32) As Int32

'SendMessage lParam = String
Private Declare Function SendMessageSTRING Lib _
   "user32.dll" Alias "SendMessageA" _
   (ByVal hwnd As Int32, _
    ByVal wMsg As Int32, _
    ByVal wParam As Int32, _
    ByVal lParam As String) As Int32

'Send Message & Wait
Declare Auto Function SendMessageTimeout Lib "User32" ( _
   ByVal hWnd As Integer, _
   ByVal Msg As UInt32, _
   ByVal wParam As Integer, _
   ByVal lParam As Integer, _
   ByVal fuFlags As UInt32, _
   ByVal uTimeout As UInt32, _
   ByRef lpdwResult As IntPtr _
) As Long

'Normal SendMessage
Private Declare Function SendMessage Lib "user32.dll" _
   Alias "SendMessageA" _
   (ByVal hwnd As Int32, _
    ByVal wMsg As Int32, _
    ByVal wParam As Int32, _
    ByVal lParam As Int32) As Int32

'Get Handle To Desktop
Private Declare Function GetDesktopWindow Lib "user32" () As IntPtr

Private Const WM_WININICHANGE     = &H1A       'INI File Update
Private Const HWND_BROADCAST      = &HFFFF&    'Send To All
Private Const WM_SETTINGCHANGE    = &H1A       'Setting Change
Private Const SMTO_ABORTIFHUNG    = &H2        'Stop If Hang
Private Const WM_COMMAND As Int32 = &H111      'Send Command
Private Const WM_USER As Int32    = &H400      'User
Private Const WM_SETTEXT          = &HC        'Change Text
Private Const WM_GETTEXT          = &HD        'Get Text

If you read the comments, you will basically get an idea of each API's function. If this is the first time you encounter APIs, I'd advise you to read this article to understand them better. I will, of course, go in detail as you go on.

The TaskBar and VB.NET

Hiding and Showing the Clock

In this segment, you actually will use three different windows, believe it or not! Technically speaking, it is actually one main window, the TaskBar, then a child window, the TaskBar's notification area, and then, a grand child window, the clock. The clock is inside the notification area, and the notification area is inside the TaskBar. You need to use the FindWindow API to obtain a handle to the TaskBar window (Shell_TrayWnd), and then use the FindWindowEx API to get the notification area (Tray_NotifyWnd), and its child, the clock (TrayClockWClass). Once you have found a handle to the clock window, you should use the ShowWindow API. If you supply a value of 1 to ShowWindow, the window will be hidden; if you supply a 0, the wanted window will be hidden. Add the following code to the button labeled Show Clock:

Private Sub btnClock_Click(ByVal sender As System.Object, _
   ByVal e As System.EventArgs) Handles btnClock.Click
Select Case btnClock.Text
Case "Show Clock"    'If Hidden
Dim TaskBarWin As Long, TrayWin As Long, ClockWin As Long

'Find TaskBar
TaskBarWin = FindWindow("Shell_TrayWnd", vbNullString)
'Find Tray Window
TrayWin = FindWindowEx(TaskBarWin, 0, "TrayNotifyWnd", vbNullString)
'Find Clock Window
ClockWin = FindWindowEx(TrayWin, 0, "TrayClockWClass", vbNullString)
ShowWindow(ClockWin, 1)    'Show Clock

btnClock.Text = "Hide Clock"
Case "Hide Clock"    'If Shown
Dim TaskBarWin As Long, TrayWin As Long, ClockWin As Long

TaskBarWin = FindWindow("Shell_TrayWnd", vbNullString)
TrayWin = FindWindowEx(TaskBarWin, 0, "TrayNotifyWnd", vbNullString)
ClockWin = FindWindowEx(TrayWin, 0, "TrayClockWClass", vbNullString)
ShowWindow(ClockWin, 0)    'Hide Clock

btnClock.Text = "Show Clock"
End Select
End Sub

This code checks the button's text value. If it says Show Clock, the clock will be shown; otherwise, it will be hidden. You may have to click this button twice because the initial text displayed on the button is Show Clock. Easy, wasn't it? Yes. If you were to run this application now, you will notice that the clock does indeed disappear and appear, as shown in Figure 2:

[EmptyClock.jpg]

Figure 2: The Clock Is Gone...

Locking and Unlocking the TaskBar

Again, the FindWindow API will be used here. One thing that you have to get sorted out here and now,is that if you want to refer to any external window, you always will use the FindWindow API. Once you have the handle of Shell_TrayWnd (the TaskBar), you need to post the appropriate message to the system. This message that you will send will tell the system what to do. To post messages to external windows, you can use the PostMessage API. With PostMessage, you need to specify to which window the message will be sent to, what type of message, the message itself, and additional parameters. In this case, you will not have any additional information except for sending the 1)Lock/unlock 2)message to your 3)window. Here is the code:

Private Sub btnLock_Click(ByVal sender As System.Object, _
   ByVal e As System.EventArgs) Handles btnLock.Click
Dim TaskBarWin As Long

'Find TaskBar
TaskBarWin = FindWindow("Shell_TrayWnd", vbNullString)
'Lock TaskBar
PostMessage(TaskBarWin, WM_COMMAND, 424, vbNullString)
End Sub

Grouping and Ungrouping Similar TaskBar Windows

This setting is not completely handled with API alone. You see, the the Grouping/Ungrouping TaskBar setting is actually stored in the Registry under this key:

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\
   Explorer\Advanced

The setting name is TaskBarGlomming. A value of 0 will ungroup the buttons, and obviously a value of 1 will group them. So, what you need to do is to write either a 1 or a 0 to this key, and then update the TaskBar window's display; otherwise, it will only be applied when explorer.exe is restarted—a reboot of the computer. To update the TaskBar, you can use the SendMessage API, and send an WM_WININIChange message to ShellTrayWnd. This is what the code would look like:

Private Sub btnGroup_Click(ByVal sender As System.Object, _
   ByVal e As System.EventArgs) Handles btnGroup.Click
Select Case btnGroup.Text
Case "Group Similar TaskBar Buttons"
Dim GroupRet As Long    'Used With SendMessage
Dim TaskBarWin As Long

'Find TaskBar
TaskBarWin = FindWindow("Shell_TrayWnd", vbNullString)

'Open Registry Key
Dim GroupKey As RegistryKey = _
Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows _
   \\CurrentVersion\\Explorer\\Advanced", True)

'Set Grouping On
GroupKey.SetValue("TaskBarGlomming", 1, RegistryValueKind.DWord)
'Store New Setting
GroupRet = SendMessage(TaskBarWin, WM_WININICHANGE, 0&, 0&)
btnGroup.Text = "Don't Group Similar TaskBar Buttons"
Case "Don't Group Similar TaskBar Buttons"
Dim GroupRet As Long
Dim TaskBarWin As Long

TaskBarWin = FindWindow("Shell_TrayWnd", vbNullString)

Dim GroupKey As RegistryKey = _
Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows _
   \\CurrentVersion\\Explorer\\Advanced", True)

'Set Grouping Off
GroupKey.SetValue("TaskBarGlomming", 0, RegistryValueKind.DWord)
GroupRet = SendMessage(TaskBarWin, WM_WININICHANGE, 0&, 0&)
btnGroup.Text = "Group Similar TaskBar Buttons"
End Select
End Sub

The TaskBar and VB.NET

Hiding and Showing Active TaskBar Windows

The whole logic behind this is actually almost the same as hiding/showing the clock. Luckily, you are dealing only with the TaskBar window, and the "Buttons" window, which is known as ReBarWindow32. So, all you need to do is to obtain a handle to ReBarWindow32, and then use the ShowWindow API to hide/show the active open windows' buttons.

Private Sub btnContents_Click(ByVal sender As System.Object, _
   ByVal e As System.EventArgs) Handles btnContents.Click
Select Case btnContents.Text
Case "Show TaskBar Contents"
Dim TaskBarWin As Long, TaskButtonWin As Long

'Find TaskBar
TaskBarWin = FindWindow("Shell_TrayWnd", vbNullString)
'Find TaskBar Button Area
TaskButtonWin = _
   FindWindowEx(TaskBarWin, 0, "ReBarWindow32", vbNullString)
ShowWindow(TaskButtonWin, 1)    'Show Active Buttons
btnContents.Text = "Hide TaskBar Contents"

Case "Hide TaskBar Contents"
Dim TaskBarWin As Long, TaskButtonWin As Long

TaskBarWin = FindWindow("Shell_TrayWnd", vbNullString)
TaskButtonWin = _
   FindWindowEx(TaskBarWin, 0, "ReBarWindow32", vbNullString)
ShowWindow(TaskButtonWin, 0)    'Hide Active Buttons
btnContents.Text = "Show TaskBar Contents"

End Select
End Sub

When this button is clicked, your TaskBar will be empty, even though you may have a number of windows open, and it will look like Figure 3.

[EmptyTaskBar.jpg]

Figure 3: The TaskBar is Empty

Activating and Deactivating the Hide Inactive Icons Setting

As you will see with the next code segment, it works exactly the same way as the TaskBar grouping setting.

Private Sub btnInactiveTrayIcons_Click( _
   ByVal sender As System.Object, _
   ByVal e As System.EventArgs) Handles btnInactiveTrayIcons.Click
Select Case btnInactiveTrayIcons.Text
Case "AutoHide Inactive Tray Icons Off"
Dim TrayWinRet As Long
Dim TrayWindow As Long

'Find TaskBar
TrayWindow = FindWindow("Shell_TrayWnd", vbNullString)

Dim InactiveKey As RegistryKey = _
Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows _
   \\CurrentVersion\\Explorer\\Advanced", True)

'Set Enable AutoTray Off
InactiveKey.SetValue("EnableAutoTray", 0, RegistryValueKind.DWord)
'Store
TrayWinRet = SendMessage(TrayWindow, WM_WININICHANGE, 0&, 0&)
btnInactiveTrayIcons.Text = "AutoHide Inactive Tray Icons On"

EnvRefresh() 'Refresh Explorer.exe
Case "AutoHide Inactive Tray Icons On"
Dim TrayWinRet As Long
Dim TrayWindow As Long

TrayWindow = FindWindow("Shell_TrayWnd", vbNullString)

Dim InactiveKey As RegistryKey = _
Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows _
   \\CurrentVersion\\Explorer\\Advanced", True)

'Set Enable AutoTray On
InactiveKey.SetValue("EnableAutoTray", 1, RegistryValueKind.DWord)
TrayWinRet = SendMessage(TrayWindow, WM_WININICHANGE, 0&, 0&)
btnInactiveTrayIcons.Text = "AutoHide Inactive Tray Icons Off"

EnvRefresh()
End Select
End Sub

EnvRefresh

The last line of the previous code segment called a function named EnvRefresh. What is this? This function looks like the following:

Public Sub EnvRefresh()    ' Refresh Explorer
Dim EnvResult As IntPtr    'Result
SendMessageTimeout(HWND_BROADCAST, _
Convert.ToUInt32(WM_SETTINGCHANGE), _
0, 0, _
Convert.ToUInt32(SMTO_ABORTIFHUNG), _
Convert.ToUInt32(2000), _
EnvResult)                 'Broadcast A Setting Change To All
End Sub

Add it to your form's class. This function is responsible for applying the modified Registry settings, without needing to reboot the computer. It uses the SendMessageTimeout API to broadcast all WM_SETTINGCHANGE messages. If something goes wrong, it aborts; otherwise, it will try to apply the changes to explorer.exe within two seconds.

The TaskBar and VB.NET

Applying AutoHide and Always On Top

With these two TaskBar properties, I've decided to follow a different route than what you have become accustomed to by now. The reason for this is that you can actually create a Desktop Toolbar object with your code, and manipulate it the same way as you can with the TaskBar. If you were to right-click on the TaskBar, and choose to create a new toolbar, that will be known as a desktop toolbar, in normal computer user terms. This desktop toolbar is known as an AppBar for programmers. An AppBar has the same capabilities as the TaskBar—it can hold buttons & menus, it can be hidden, it can be resized and moved.

To make your lives easier, I have decided to create a new AppBar class. This class can be added as a new class, or even inside your Form's class. Whichever way you decide to do it, give it a descriptive name such as TBAppBar, and add the following code to it:

Public Class TBAppBar

'Get Message Sent By App Bar
Private Declare Function GetAppBarMessage Lib "shell32" _
   Alias "SHAppBarMessage" _
   (ByVal dwMessage As Integer, _
    ByRef pData As APPBARDATA) = As Integer

'Send Message To App BAr
Private Declare Function SetAppBarMessage Lib "shell32" _
   Alias "SHAppBarMessage" _
   (ByVal dwMessage As Integer, _
    ByRef pData As APPBARDATA) As Integer

'AppBar Structure
Private Structure APPBARDATA
Dim cbSize As Integer
Dim hwnd As Integer
Dim uCallbackMessage As Integer
Dim uEdge As Integer
Dim rc As Rectangle
Dim lParam As Integer
End Structure

'Get Current State
Private Const ABM_GETSTATE As Int32      = &H4
'Get TaskBar Position
Private Const ABM_GETTASKBARPOS As Int32 = &H5
'Apply Setting(s)
Private Const ABM_SETSTATE As Int32      = &HA
'Autohide
Private Const ABS_AUTOHIDE As Int32      = &H1
'Always on Top
Private Const ABS_ALWAYSONTOP As Int32   = &H2

Private TBAppBAutoHide As Boolean
Private TBAppBarAlwaysOnTop As Boolean

Public Sub New()
Me.GetState() 'Get Current State
End Sub

Private Sub GetState()

'What Setting?
Dim AppBarSetting As New APPBARDATA

'Initialise
AppBarSetting.cbSize = Marshal.SizeOf(AppBarSetting)

'Get Current State
Dim AppBarState As Int32 = _
   GetAppBarMessage(ABM_GETSTATE, AppBarSetting)

Select Case AppBarState

'Nothing Set
Case 0
TBAppBAutoHide = False
TBAppBarAlwaysOnTop = False

'Always On Top
Case ABS_ALWAYSONTOP
TBAppBAutoHide = False
TBAppBarAlwaysOnTop = True

Case Else
'AutoHide
TBAppBAutoHide = True

End Select

End Sub

'Apply Settings
Private Sub SetState()

'Setting We Want To Apply
Dim AppBarSetting As New APPBARDATA
'Initialise
AppBarSetting.cbSize = Marshal.SizeOf(AppBarSetting)

If Me.AutoHide Then
AppBarSetting.lParam = ABS_AUTOHIDE    'AutoHide
End If

'Always On Top
If Me.AlwaysOnTop Then
AppBarSetting.lParam = AppBarSetting.lParam Or ABS_ALWAYSONTOP
End If

SetAppBarMessage(ABM_SETSTATE, AppBarSetting)

End Sub

Public Property AutoHide() As Boolean    'Autohide
Get
Return TBAppBAutoHide
End Get
Set(ByVal Value As Boolean)
TBAppBAutoHide = Value
Me.SetState()
End Set
End Property

Public Property AlwaysOnTop() As Boolean    'Always On Top
Get
Return TBAppBarAlwaysOnTop
End Get
Set(ByVal Value As Boolean)
TBAppBarAlwaysOnTop = Value
Me.SetState()
End Set
End Property

End Class

Usage

Remember to create a new TBAppBar object in your form class:

Private AB As New TBAppBar    'AppBar Object

Add the following code to these two form buttons, to apply the appropriate properties from the TBAppBar class:

Private Sub btnAutoHide_Click(ByVal sender As System.Object, _
   ByVal e As System.EventArgs) Handles btnAutoHide.Click

Select Case btnAutoHide.Text
Case "Auto Hide TaskBar"
btnAutoHide.Text = "Auto Hide TaskBar Off"
AB.AutoHide      = True     'Set AutoHide On
Case "Auto Hide TaskBar Off"
btnAutoHide.Text = "Auto Hide TaskBar"
AB.AutoHide      = False    'Set AutoHide Off
End Select
End Sub

Private Sub btnTop_Click(ByVal sender As System.Object, _
   ByVal e As System.EventArgs) Handles btnTop.Click
Select Case btnTop.Text
Case "Keep TaskBar on Top"
AB.AlwaysOnTop = True     'Set Always On Top On
btnTop.Text    = "Don't Keep TaskBar on Top"
Case "Don't Keep TaskBar on Top"
AB.AlwaysOnTop = False    'Set Always On Top Off
btnTop.Text    = "Keep TaskBar on Top"
End Select
End Sub

Although the AppBar class has a lot more methods and values (API Constants), I'm only going to explain AutoHide and AlwaysOnTop. If you haven't noticed, this class has two APIs associated with it:

  1. GetAppBarMessage
  2. SetAppBarMessage

GetAppBarMessage gets the current state of the toolbar. SetAppBarMessage is used to change the current state of the toolbar. By current state, I mean the toolbar properties, location, visibility, and so forth. In this class, you have created two properties: one for AutoHide, and one for AlwysOnTop. Because these two settings work very close to one another, it is easy to manipulate either through the use of the SetState method. The onus is on you to do a bit more research on AppBars.

You have now basically completed the TaskBar area. One TaskBar area you haven't touched upon is the good old Start button, and the Start menu.

The TaskBar and VB.NET

Start Button Settings

The title of this section is actually misleading. The Start button doesn't really have properties you can set through Windows XP, but, you can change stuff like the Start button text, as well as to enable and disable the Start button. Funky stuff!

Enabling and Disabling the Start Button

By now, you should know the familiar recipe. All you need to do is to obtain a handle to the Start button's window inside the TaskBar, and then use EnableWindow to disable and enable the Start button window, by supplying either True (enabled) or False (disabled).

Private Sub btnStartButton_Click(ByVal sender As System.Object, _
   ByVal e As System.EventArgs) Handles btnStartButton.Click
Select Case btnStartButton.Text
Case "Disable Start Button"
Dim TaskBarWin As Long, StartButtonWin As Long

'Find TaskBar
TaskBarWin = FindWindowEx(0, 0, "Shell_TrayWnd", Nothing)
'Find Start Button
StartButtonWin = FindWindowEx(TaskBarWin, 0, "Button", Nothing)
'Disable Start Button
EnableWindow(StartButtonWin, False)
btnStartButton.Text = "Enable Start Button"
Case "Enable Start Button"
Dim TaskBarWin As Long, StartButtonWin As Long

TaskBarWin     = FindWindowEx(0, 0, "Shell_TrayWnd", Nothing)
StartButtonWin = FindWindowEx(TaskBarWin, 0, "Button", Nothing)
EnableWindow(StartButtonWin, True)    'Enable Start Button
btnStartButton.Text = "Disable Start Button"
End Select

End Sub

Changing the Start Button Text

I'm willing to bet that this would be the first topic in this article that will be read.

Once you have obtained the handle to the Start button window (button), you need to use a modified version of the SendMessage API to send the new text to the window. If you look closely at the APIs that you are using, you will notice these two:

SendMessage

'Normal SendMessage
Private Declare Function SendMessage Lib "user32.dll" _
   Alias "SendMessageA" (ByVal hwnd As Int32, _
                         ByVal wMsg As Int32, _
                         ByVal wParam As Int32, _
                         ByVal lParam As Int32) As Int32

SendMessageSTRING

'SendMessage lParam = String
Private Declare Function SendMessageSTRING Lib "user32.dll" _
   Alias "SendMessageA" (ByVal hwnd As Int32, _
                         ByVal wMsg As Int32, _
                         ByVal wParam As Int32, _
                         ByVal lParam As String) As Int32

The only real difference between these two is that with the latter, the lParam parameter is a string and not an Int32. The reason for this is that you will be sending a string message instead of a numeric message for the Start button text.

Add the following sub to your code:

Private Sub SetStartCaption(ByVal NewStr As String)
Dim TaskBarWin As Long
Dim StartWin As Long
Dim StartText As String

'Find TaskBar
TaskBarWin = FindWindow("Shell_TrayWnd", vbNullString)
'Find Start Button
StartWin = FindWindowEx(TaskBarWin, 0&, "button", vbNullString)
'Set StartButton Text
StartText = Microsoft.VisualBasic.Left(NewStr, 5)
'Send The Message
SendMessageSTRING(StartWin, WM_SETTEXT, 256, StartText)

Exit Sub    'Don't Do Anything Else
End Sub

This sub is responsible for updating the Start button's text. And yes, you find its handle, and then simply send the string message to the Start button window. What will be sent will depend on what you have entered into an InputBox in the Change Start button text button's code:

Private Sub btnText_Click(ByVal sender As System.Object, _
   ByVal e As System.EventArgs) Handles btnText.Click
Select Case btnText.Text
Case "Change Start Button Text"
Dim NewText As String
NewText = InputBox("Enter New Text")    'Get New Start Button Text
SetStartCaption(NewText)
btnText.Text = "Change Back Start Button Text"
Case "Change Back Start Button Text"
SetStartCaption("Start")                'Revert Back To "Start"
btnText.Text = "Change Start Button Text"
End Select

End Sub

After entering some text, you will see a Start button similar to Figure 4.

[StartButton.jpg]

Figure 4: Modified Start Button.

The only bad thing about this is the fact that you cannot enter more characters than what can be shown inside that window.

The TaskBar and VB.NET

Displaying the TaskBar Properties Window

'TaskBar & Start Menu Properties
Private Sub btnTaskProp_Click( _
   ByVal sender As System.Object, _
   ByVal e As System.EventArgs) Handles btnTaskProp.Click
Process.Start("rundll32.exe", "shell32.dll,Options_RunDLL 1")
End Sub

Via the use of RunDll32.exe, you show the TaskBar properties window with the help of shell32.dll. This article explains in detail how this process works.

[TaskBarProperties.jpg]

Figure 5: TaskBar Properties

Showing and Hiding a Start Menu Menu

In this section, you will hide and showe the Favourites window from the Start menu. You will be updating a value in the Registry under the same key you used when changing the Grouping values. The only difference is that you will be changing the StartMenuFavorites value. A value of 1 will show it; a value of 0 will hide the Favourites menu. To see the changes immediately, you will call the EnvRefresh function here as well. Here is the code:

Private Sub btnFavourites_Click( _
   ByVal sender As System.Object, _
   ByVal e As System.EventArgs) Handles btnFavourites.Click
Select Case btnFavourites.Text
Case "Show Favourites In Menu"

Dim FavKey As RegistryKey = _
Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows _
   \\CurrentVersion\\Explorer\\Advanced", True)
'Show Favourites Menu
FavKey.SetValue("StartMenuFavorites", 1, RegistryValueKind.DWord)

EnvRefresh()    'Refresh Explorer.exe
btnFavourites.Text = "Hide Favourites In Menu"
Case "Hide Favourites In Menu"

Dim FavKey As RegistryKey = _
Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows _
   \\CurrentVersion\\Explorer\\Advanced", True)
'Hide Favourites
FavKey.SetValue("StartMenuFavorites", 0, RegistryValueKind.DWord)

EnvRefresh()
btnFavourites.Text = "Show Favourites In Menu"
End Select
End Sub

Nice!

Creating Your Own Show Desktop Button

Yes, you are free to argue that this is not really a TaskBar or Start button function, but it's useful nonetheless. The whole methodolgy is different. What you need to do is to set a COM Reference to Microsoft Shell Controls And Automation (Shell32.dll). To set a reference, you select Project, Add Reference, and then select the COM tab. From there, you just need to scroll to this library. After you have added this, you should create a new Shell object and use its ToggleDesktop method to show the desktop window, by minimizing everything. This is the code:

Private Sub btnShowDesktop_Click( _
   ByVal sender As System.Object, _
   ByVal e As System.EventArgs) Handles btnShowDesktop.Click
'Create Instance Of Shell Class
'Referenced COM Library "Microsoft Shell Controls And Automation"
'(shell32.dll)
Dim objShell As New Shell32.ShellClass()
'Show Desktop
DirectCast(objShell, Shell32.IShellDispatch4).ToggleDesktop()

End Sub

Any time you click this button, it will show the Desktop.

Conclusion

This was fun (for me, at least)! Sadly, all good things have to to come to an end, and this is it. I hope you have learnt from this article, and I sincerely hope to see you reading more of my articles in the future; until that time, cheers!



About the Author

Hannes du Preez

Hannes du Preez is a Microsoft MVP for Visual Basic. He is a trainer at a South African-based company. He is the co-founder of hmsmp.co.za, a community for South African developers.

Downloads

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • The explosion in mobile devices and applications has generated a great deal of interest in APIs. Today's businesses are under increased pressure to make it easy to build apps, supply tools to help developers work more quickly, and deploy operational analytics so they can track users, developers, application performance, and more. Apigee Edge provides comprehensive API delivery tools and both operational and business-level analytics in an integrated platform. It is available as on-premise software or through …

  • Historically, IT investments were made simply in the interest of increasing competitiveness and with little insight into the hard, fact-based financial case. As organizations worldwide have been forced to tighten belts and slash budgets, management increasingly require financial evidence for sound IT purchases that fully align with business goals and strategies. This webinar defines and describes total cost of ownership - an important metric for assessing and tracking the risks, costs, and benefits of …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds