|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
PreambleBefore 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. IntroductionOne 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 JumpPorting Pavel's 2003 BandObjectLib code to 2005 was a relatively painless experience, so I'll just skim through a few of the minor details:
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.: [assembly: AssemblyVersion("1.0.0.0")]
Building Our First ToolbarSo 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 ControlSince the using BandObjectsLib;
using System.Runtime.InteropServices;
and [Guid("AE07101B-46D4-4a98-AF68-0333EA26E113")]
Step 2. Building the ToolbarYou have all the facilities of a standard Step 3. Embedded ResourcesTo 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. this.tsbtn1.Image = global::BandObjectsExample.Resources.redcircle;
this.tsbtn1.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.tsbtn1.ImageTransparentColor = System.Drawing.Color.Magenta;
Step 4. Code BehindIn my example, I've created a couple of sample buttons to pop open some websites using your system's default browser. 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 ToolbarOnce 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 // 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 ToolbarOnce 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, IdeasThe sky really is the limit for these band object controls. In my example, I've only shown the
|
||||||||||||||||||||||