Click here to Skip to main content
15,314,303 members
Articles / Programming Languages / C#
Article
Posted 17 May 2006

Stats

969.3K views
3.5K downloads
135 bookmarked

Band Objects - .NET 2.0 Redux

Rate me:
Please Sign up or sign in to vote.
4.70/5 (27 votes)
17 May 20066 min read
A .NET 2.0 version of Pavel Zolnikov's BandObjects project.

As Internet Explorer Toolbar

As Taskbar Toolbar

Preamble

Before you start, I highly recommend you read Pavel Zolnikov's article entitled "Extending Explorer with Band Objects using .NET & Windows Forms" first.

Pavel wrote a fantastic article explaining the ins & outs of using COM Interop to write your own customizable band objects for Explorer using .NET 2003. Without that article, this one wouldn't have come about, so kudos to Pavel.

Introduction

One of the niceties about the .NET 2.0 environment is the rich suite of form controls that have been included for form design. While the 2003 environment was great and all, it was missing a lot of the funky controls such as tool strips and dropdown buttons that we've come to expect as standard in a nice Explorer bar UI. My goal was to design a nice neat tool bar solution so that I had several shortcut links to the sites and systems I used most often, and also to provide 'at-a-glance' information by displaying data in a label which updated on a regular basis.

The 2003 to 2005 Jump

Porting Pavel's 2003 BandObjectLib code to 2005 was a relatively painless experience, so I'll just skim through a few of the minor details:

  1. The first thing to do was to create a standard Windows Class Library.
  2. The ComInterop.cs, BandObject.cs, and Attributes.cs files were taken verbatim from the 2003 project and copied to the 2005 project.
  3. All [assembly: ] attribute code was removed and placed in the AssemblyInfo.cs file instead.
  4. References to SHDocVw (COM tab), System.Drawing, and System.Windows.Forms (.NET tab) were added.
  5. And finally, a strongly named key was added to the library to allow it to be added to the GAC. This is an easier endeavor in the new IDE as you can directly create a .snk file for a project by right-clicking the project file, selecting 'Properties', selecting the 'Signing' tab, and choosing to sign the assembly with a new Strong Named Key.
  6. The final step was to add a post build event to the Project Properties > "Build Events", to add the successfully compiled assembly into the Global Assembly Cache. In VS2005, the gacutil.exe utility is no longer located in the framework folder but in the VS2005/SDK folder under C:\Program Files\. So you may need to alter this script depending on your installation and path setup.
cd $(ProjectDir)bin\Debug

"C:\Program Files\Microsoft Visual Studio 8\
      SDK\v2.0\Bin\gacutil" /if CustomToolbar.dll
"C:\Program Files\Microsoft Visual Studio 8\
      SDK\v2.0\Bin\gacutil" /if Interop.SHDocVw.dll

Note * If you plan on using this software yourself, be aware that Strongly Named Libraries in the GAC are treated as being distinct and separate if they have differing version numbers, so I'd recommend keeping the Assembly Version number in the lib as some static value. E.g.:

C#
[assembly: AssemblyVersion("1.0.0.0")]

Building Our First Toolbar

So we're ready to build our first toolbar. Setting up the project is easy enough. In the attached code, I've just created it as an additional project within the same solution. It's a class library project. It requires direct references to the strong named DLLs in the bin folder of our BandObjectLib project which have already been installed in the GAC. And it needs a strong named key and a static Assembly Version Number.

Step 1. Adding the BandObject Control

Since the BandObject class is essentially a beefed up derived class of UserControl, we can simply add a UserControl to our project and then alter the code-behind so that our control extends a BandObject instead. In this example, I'm creating a horizontal Explorer Toolbar. Since this control will be exposed through COM, we need to specify a GUID attribute to uniquely identify the class. The GUID attribute is part of the System.Runtime.InteropServices namespace.

C#
using BandObjectsLib;
using System.Runtime.InteropServices;

and

C#
[Guid("AE07101B-46D4-4a98-AF68-0333EA26E113")]
    [BandObject("My Toolbar", BandObjectStyle.Horizontal 
    | BandObjectStyle.ExplorerToolbar 
    | BandObjectStyle.TaskbarToolBar, HelpText = "My First Toolbar")]
    public class MyToolBar : BandObject
    { ...

Step 2. Building the Toolbar

You have all the facilities of a standard UserControl at your finger tips through the designer canvas, so feel free to add anything you want from simple buttons to mini-browsers to completely separate forms which popup on a click event. In my example, I've added a ToolStrip and populated it with some standard toolstrip controls: buttons, dropdown buttons, labels, textboxes, separators, and progress bars.

Step 3. Embedded Resources

To make the bar look a little more aesthetically pleasing, I've embedded some PNG images (thanks to www.famfamfam.com's silk icon collection) to an Embedded Resource File in my project and then set the button images. Simply set your toolstrip buttons to display "ImageAndText" or "Image", and specify an image from your resx file. When you compile your toolbar, all the image files will be embedded in the DLL, so there's no need for any installation directories and file bundles. Resource files are very convenient for embedding all sorts of dynamic resources or for making your toolbar multi-lingual.

C#
this.tsbtn1.Image = global::BandObjectsExample.Resources.redcircle;
this.tsbtn1.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.tsbtn1.ImageTransparentColor = System.Drawing.Color.Magenta;

Step 4. Code Behind

In my example, I've created a couple of sample buttons to pop open some websites using your system's default browser.

C#
private void OpenWebPage(string url)
{
    System.Diagnostics.Process process = new System.Diagnostics.Process();
    process.StartInfo.FileName = url;
    process.Start();
}

As an example of a status polling facility, I've created a quick and dirty routine to poll my Winamp web interface for the currently playing track and to display it on a label at the end of the strip. This is performed on a timer which ticks every 30 seconds. Be careful though about having multiple instances of your toolbar (e.g., in multiple browsers) as this might have an adverse effect on your system if the task being performed on the timer occurs very often or is very processor intensive. For my Winamp ticker, I only ever have one instance running on my taskbar beside the system clock.

Step 5. Building Your Toolbar

Once you've finished designing your toolbar in the IDE designer, then it's time to build it and take it for a test drive. You'll need to add a post build job to this project also. The first task is to install your toolbar DLL in the Global Assembly Cache. Since it's going to be used through COM, you'll also need to register your assembly using the Regasm utility contained in the framework installation folder.

cd $(ProjectDir)bin\Debug

"C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\gacutil" /if MyToolbar.dll
"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\regasm" MyToolbar.dll

Note * By default, .NET 2.0 assemblies are set to be invisible to COM. In your AssemblyInfo.cs file, you'll find an attribute called [Assembly:ComVisible]. This attribute states whether or not the types in your .NET assembly should be exposed to COM. You have to set this value to true, otherwise the Regasm program will not be able to see your toolbar and the registration will fail.

C#
// Setting ComVisible to false makes the types in this assembly not visible 
// to COM components. If you need to access a type in this assembly from 
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(true)]

Step 6. Using the Toolbar

Once you've successfully built and registered your toolbar, it should appear on the right click menu of your taskbar and the toolbar context menu in the Internet Explorer. It's worth pointing out that Explorer caches COM objects when they are first loaded, so after doing a rebuild, you might not necessarily see the updates in your toolbar. There are a couple of ways of getting around this such as changing your folder settings to launch folders in new Explorer instances, or to kill the Explorer exe (not recommended :) ) from your Task Manager.

Ideas, Ideas, Ideas

The sky really is the limit for these band object controls. In my example, I've only shown the ToolStrip control, populated with some buttons and other simple controls. I've added some additional functionalities such as a ContextMenu and the timer control, but there's nothing stopping you from adding any control available. Post them to CodeProject if you come up with a good one :)

  • You might want to make a vertical toolbar with a mini WebBrowserControl for viewing your favorite web portal.
  • You could write your own RSS ticker to sit on your taskbar beside the clock and rotate through your favorite RSS-feeds.
  • Or maybe you could write an SMS sender to login to your mobile operator's website and send a text message through your account.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

cambo1982
Software Developer (Senior)
Ireland Ireland
Husband, son, brother, I grok technologiy. I write code. I take photos. I teach & practice Krav Maga. I play football. I eat good-food. I drink nice-wine.

Comments and Discussions

 
GeneralRe: Not show currectly Pin
Green Fuze4-Sep-06 4:53
MemberGreen Fuze4-Sep-06 4:53 
GeneralSolved - Thanks Pin
Sota198619-Sep-06 0:04
MemberSota198619-Sep-06 0:04 
AnswerRe: Not show up currectly Pin
Deza_29-Jul-06 7:26
MemberDeza_29-Jul-06 7:26 
QuestionRe: Not showing up Pin
Shakalama3-Aug-06 14:12
MemberShakalama3-Aug-06 14:12 
AnswerRe: Not showing up [modified] Pin
tl77777778-Aug-06 4:49
Membertl77777778-Aug-06 4:49 
GeneralRe: Not showing up Pin
FrozenCow_10-Aug-06 7:04
MemberFrozenCow_10-Aug-06 7:04 
GeneralRe: Not showing up Pin
Shakalama11-Aug-06 17:36
MemberShakalama11-Aug-06 17:36 
GeneralTaskToolBar not sticking after logging out Pin
izogi14-Jun-06 16:13
Memberizogi14-Jun-06 16:13 
Hi everyone.

Thanks for the port, and thanks for the excellent article explaining it. I've been having a bit of a problem, though.

I've recently inherited the maintenance of a BandObjects toolbar that's used for intranet navigation within our organisation. I think I've now figured out most of what's going on, but there are a couple of things that haven't been working.

We discovered that the original toolbar made MSIE unstable (meaning frequent crashing), as soon as the .Net 2.0 framework was installed on the PC. I'm not sure exactly why -- maybe some confilct with IE wanting to force the toolbar to load in 2.0 when it's not compiled for that -- but the good news is that anecdotal testing seems to suggest that it's acting much more stable now that I've ported our toolbar to use cambo1982's recent port of BandObjects. (yay, thanks)

Unfortunately, porting our toolbar to the new BandObjects has introduced another problem. This time it's with the Taskbar invocation of the toolbar (which we also use).

What happens is that if I switch on the toolbar to display on the taskbar (it's WindowsXP, for the record), then log out and log in, *all* of the toolbars that were on the taskbar disappear, including prepackaged ones that I'd expect to have no serious problems... such as the Windows QuickLaunch bar. I can switch them all on again manually, including our custom toolbar, and they work without apparent problems, but I'm confused as to why they disappeared in the first place.

I've spent a couple of days experimenting with changes to the code, the way the various assemblies are registered, and so on, but so far with no luck. To me it seems it could mean that either the toolbar preferences aren't being saved at logoff, or alternatively that the toolbars aren't being loaded at logon due to some kind of unrecoverable and unreported error. To investigate further, I've experimented with putting some short debugging code at the top of the BandObject() constructor, and noted that it doesn't get called at all, implying that there's no attempt to load the toolbars. Consequently, I'm wondering if the BandObjects toolbar could be preventing the TaskBar toolbar preferences from being saved, or somehow causing them to be deleted.

To simplify the problem, I've also tried compiling and installing the MyBandObjectToolbar of the BandObjectsExample project. For me at least, it exhibits exactly the same problems: Having it enabled in the taskbar when a user logs off prevents any taskbar toolbars from loading at the next logon.


Has anyone else noticed this type of behaviour, or is it local to our systems? Even better, can anyone suggest where I might look to fix it?

Thanks.

Mike.
GeneralRe: TaskToolBar not sticking after logging out Pin
cambo198214-Jun-06 22:17
Membercambo198214-Jun-06 22:17 
GeneralRe: TaskToolBar not sticking after logging out Pin
izogi15-Jun-06 13:02
Memberizogi15-Jun-06 13:02 
GeneralRe: TaskToolBar not sticking after logging out Pin
jules7810-Aug-06 6:10
Memberjules7810-Aug-06 6:10 
GeneralRe: TaskToolBar not sticking after logging out Pin
izogi28-Apr-07 15:18
Memberizogi28-Apr-07 15:18 
GeneralRe: TaskToolBar not sticking after logging out Pin
izogi19-Oct-06 14:02
Memberizogi19-Oct-06 14:02 
GeneralRe: TaskToolBar not sticking after logging out Pin
izogi28-Apr-07 15:14
Memberizogi28-Apr-07 15:14 
GeneralRe: TaskToolBar not sticking after logging out Pin
ghost_078-Feb-10 23:41
Memberghost_078-Feb-10 23:41 
GeneralProblem registering assembly in x64 world Pin
manfbraun28-May-06 6:49
Membermanfbraun28-May-06 6:49 
GeneralRe: Problem registering assembly in x64 world Pin
cambo198214-Jun-06 21:52
Membercambo198214-Jun-06 21:52 
GeneralRe: Problem registering assembly in x64 world Pin
manfbraun15-Jun-06 0:40
Membermanfbraun15-Jun-06 0:40 
GeneralRe: SetSite( object pUnkSite ) Pin
cambo198222-May-06 4:43
Membercambo198222-May-06 4:43 
GeneralGood article Pin
Aboub sofiane17-May-06 12:19
MemberAboub sofiane17-May-06 12:19 
GeneralRe: Good article Pin
Vertyg018-May-06 10:54
MemberVertyg018-May-06 10:54 
GeneralRe: Good article Pin
cambo198222-May-06 4:36
Membercambo198222-May-06 4:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.