Click here to Skip to main content
Click here to Skip to main content

Internet Explorer Toolbar Creation

By , 17 Apr 2008
Rate this:
Please Sign up or sign in to vote.

Introduction

This article provides the base code for the development of a professional toolbar for Internet explorer.
While the other articles provide a good starter for toolbar concept, they still lack some bits that are needed for professional applications and supporting look & feel for Vista as well as XP along with complete setup.
This article is best suited for newcomers to toolbars that want to get a working project in 5 minutes.
I still recommend further reading in the related articles for those of you that want to get more thorough understanding about the internals of the BandObject component.

Background

While developing my Password Manager toolbar ,I started looking for code samples and articles in the Net. I found some very useful ones (like Pavel Zolnikov's article) that helped me a lot to start basic toolbar implementation.

But then I found some bits of problems such as :
- toolbar do not show in IE7
- toolbar problems in Vista and bad look & feel on Vista
- problems with the installation
- toolbar do not show after installation and more .....

Since I had to deal with all these problems in order to get it working professionally,I spent days to find out all the comments and forums that solve those issues.

I try here to provide the most "Clean" toolbar implementation for all of you guys ,beginners and experts , that want to experience the fun of creating toolbars without the plumbing hassles.

There are still some small issues remaining and I hope that with the help of some keen coders we can manage to solve all issues and provide the perfect Toolbar class.

Using the Code

Simply extend the Toolbar class . you can rename it to whatever name you want.

I provided in the sample a simple button and label . it is a regular WinForm ToolStrip so you can put here all ToolStrip items.

Accessing the Browser and DOM

Use the Explorer property to access the WebBrowserClass.I added for simplicity the Navigate(string url) and NewWindow(string url) methods.

The "Constants" class:

class Constants 
{

  public const string ToolbarName = "Sample Toolbar"; 

  public const string ToolbarHelpText = "Sample Toolbar Help Text";   

  public const string ToolbarGUID = "4D02E7E6-5930-4b51-B9B0-9F21B3789400"; 


  public const int ToolBarWidth = 160; 

  public const string Assembly = "Toolbar, Version=1.0.0.0, Culture=neutral, 
PublicKeyToken=f2e11770db40f5b0"; 

}

You MUST create a new GUID for your toolbar instead of the one supplied with oolbarGUID.

It is advised to sign the released assembly with different .snk file (ToolbarKey.snk) . in that case the PublicKeyToken will be changed, so you will need to revise it in the Assembly const string.

To extract the public key token from dll you can use sn -T from VS2005 tools.

Debugging , Rebuilding & Development

In order to debug the toolbar you can remove the remark from the constructor

         public Toolbar() 
     {
            //Debugger.Launch();

        ......

Alternatively you can use the "Attach to Process" option of VS2005 IDE and attach to IE.

When you update the toolbar's code , you do not need to unistall and reinstall it ,you just need to re-register it in the GAC.

go to Start-->Setting-->Control Panel-->Administrative Tools

select "Microsoft .NET Framework 2.0 Configuration" .

right-click on the "Assebly Cache" , choose "Add" and add the toolbar dll.

Points of Interest

The use of BHO to show toolbar after installation

BHO (Browser Helper Object) is simmilar to Toolbar (BandObject) except of the GUI capabilities.

I use the BHO only for the purpose of showing the toolbar immediatly after installation.

At the setup I register the BandObject as BHO by adding it to the registry:

     RegistryKey bhoKey = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft
\Windows\CurrentVersion\Explorer\Browser Helper Objects\" + guid); 
     bhoKey.SetValue("NoExplorer", 1, RegistryValueKind.DWord);    

At first time the SetSite method is called for the BHO object the ShowToolbar method is called.

       
public virtual void SetSite(object pUnkSite) 
{ 
  if (pUnkSite is IInputObjectSite) { 
     _SetSite(pUnkSite); 
  }
  else if (pUnkSite is InternetExplorer) 
  {
    // this code is executed by the BHO in order to show the toolbar immediatly 
    // after the installation 
    ShowToolBar(pUnkSite); 
  } 
}
  

The ShowToolbar is responsible to Show the toolbar using the ShowBrowserBar

method , and then remove the BHO from the registry since it have no use any more.

        
Registry.LocalMachine.DeleteSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion
\Explorer\Browser Helper Objects\{" + Constants.ToolbarGUID + "}");
    

Painting the toolbar's background according to the theme

When I first tested the toolbar on Vista I got it pretty ugly gray background instead of the smooth gradient style of IE7 of Vista. I digged to find a solution to this and found the fantastic "Geer's Blog" that gave me just the answer I was looking for.

The code snippet that is responsible to inherit the background of the toolbar looks like that :

[DllImport("uxtheme", ExactSpelling = true)] 
public extern static Int32 DrawThemeParentBackground(IntPtr hWnd, IntPtr hdc,
 ref Rectangle pRect);

protected override void OnPaintBackground(PaintEventArgs e) 
{
 if (this.BackColor == Color.Transparent) 
 {
   IntPtr hdc = e.Graphics.GetHdc(); 
   Rectangle rec = new Rectangle(e.ClipRectangle.Left, e.ClipRectangle.Top,
 e.ClipRectangle.Width, e.ClipRectangle.Height);
   DrawThemeParentBackground(this.Handle, hdc, ref rec);
   e.Graphics.ReleaseHdc(hdc);
 }
 else 
 {
  base.OnPaintBackground(e); 
 }
}      

Toolbar location

I admit that I didn't get it perfect yet ,the ability to locate the toolbar everywhere I like.I had it working pretty good on XP but then found out that it do not work well on Vista. I also tried to remember the location of the toolbar by implementing IPersistStream ,but it didn't work.

Finally I comporized on a static toolbar and that works fine both in Vista & XP .

The flag in GetBandInfo that is responsible for the location and other layout behaviors is:

  dbi.dwModeFlags = DBIMF.NORMAL | DBIMF.VARIABLEHEIGHT | DBIMF.ALWAYSGRIPPER; 
 

Process Scope

All BandObjects that are created with "new window" or "new tab" are in the same process so you can use a static member in order to communicate between them.for convenience I added a static list of all the toolbars in the process: ToolbarsCollection .

I used this collection in my Password Manager to synchronize login\logout and other operations between the toolbars.

Launching IE from the desktop or the quick-launch create new process so in that ase the collection won't work.

About the Setup

I used the very basic setup project provided by VS2005. Almost all the setup code that is responsible for registry operations : COM registration, BandObject registration & BHO registration is done in the ToolbarInstaller calss which derives from System.Configuration.Install.Installer

Another thing I do in the setup is the registration in the GAC by explicitely selecting the GAC folder in the File System. I could register the toolbar in the GAC with code using the Publish class. but this is out of the scope

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Sharon Salmon
CEO LogOnce - www.logonce.com
Israel Israel
I have been writing code for more than 12 years in C C++ Java & C#.
Since 2004 I had been specializing in .Net C# .
ever since I discovered C# I am a big fan of it.
I live in Tel-Aviv Israel
I recently founded new software startup.
My Company site is www.logonce.com

 
e-mail: sharon@logonce.com

Comments and Discussions

 
QuestionIssue in creating setup PinmemberMember 37671609-Oct-13 1:52 
QuestionCouple of Issues with IE 9 on Windows 7 PinmemberPrutha Tester11-Sep-12 3:06 
QuestionIt's not working in windows 2008 R2 64bit. IE 8 Pinmembersj.abhilash10-Jul-11 19:54 
GeneralInstall Question PinmemberMember 287088111-Dec-10 23:16 
GeneralToolbar Pinmemberunderscore_dot11-Jan-10 4:13 
QuestionDo you support windows server 2000 2003 and server 2008 ? PinmemberBattosaiii6-Oct-09 0:53 
AnswerRe: Do you support windows server 2000 2003 and server 2008 ? PinmemberBattosaiii6-Oct-09 23:00 
GeneralUsing a TextBox PinmemberFlorian Kunz1-Feb-09 22:29 
GeneralRe: Using a TextBox Pinmembernst8884-Feb-09 4:14 
GeneralRe: Using a TextBox PinmemberMathelem16-Apr-09 6:58 
QuestionHow to change label text after it loaded Pinmembermakseek23-Dec-08 10:32 
AnswerRe: How to change label text after it loaded PinmemberPhilipp Kratzer13-Feb-09 0:37 
QuestionImplementing OS skin for toolbar in Vista Pinmembernvikas71-Dec-08 18:05 
AnswerRe: Implementing OS skin for toolbar in Vista PinmemberSharon Salmon1-Dec-08 21:26 
Questionhow to generate OnDocumentComplete Event in this example Pinmemberronak.softlink20-Nov-08 20:37 
GeneralThe Backspace key can not use Pinmemberwosi29-Jul-08 22:09 
GeneralRe: The Backspace key can not use PinmemberSinja223-Jun-10 18:40 
GeneralIE7 and XP PinmemberMember 163330828-Jul-08 7:19 
GeneralRe: IE7 and XP PinmemberMember 163330828-Jul-08 8:02 
QuestionToolbar will not display Pinmembermarc22110-Jul-08 3:44 
AnswerRe: Toolbar will not display PinmemberSharon Salmon11-Jul-08 21:29 
QuestionRe: Toolbar will not display Pinmembermarc22115-Jul-08 9:45 
AnswerRe: Toolbar will not display Pinmembercharithmkd13-Oct-10 17:15 
Questiontoolbar can not move PinmemberMember 35224788-Jul-08 18:48 
AnswerRe: toolbar can not move PinmemberSharon Salmon11-Jul-08 21:14 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140415.2 | Last Updated 17 Apr 2008
Article Copyright 2008 by Sharon Salmon
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid