Click here to Skip to main content
15,892,809 members
Articles / Multimedia / GDI+

Pretty IE Toolbar in C#

Rate me:
Please Sign up or sign in to vote.
4.89/5 (39 votes)
9 Sep 2008CPOL5 min read 173.8K   4.8K   121  
The article describes the work principles and ways of use of IEToolbarEngine. This is a toolbar for Internet Explorer, which has a reusable architecture.
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.ComponentModel;
using SHDocVw;
using Microsoft.Win32;

namespace IEToolbarEngine
{
    [ComVisible (true)]
    [Guid ("86A3CDAA-9B25-480e-B73F-C2D359B87966")]
    [ClassInterface (ClassInterfaceType.None)]
    public class ShowToolbarBHO : BandObjectLib.IObjectWithSite
    {

        DateTime LastRun
        {
            get
            {
                try
                {
                    using (RegistryKey rk = Registry.CurrentUser.CreateSubKey (IEToolbarEngine.SettingsKey))
                    {
                        string val = rk.GetValue (IEToolbarEngine.LastRunValue).ToString ();
                        DateTime result = new DateTime (Convert.ToInt64 (val));                        
                        return result;
                    }
                }
                catch(Exception)
                {
                    return DateTime.MinValue;
                }
            }
            set
            {
                using (RegistryKey rk = Registry.CurrentUser.CreateSubKey (IEToolbarEngine.SettingsKey))
                {
                    rk.SetValue (IEToolbarEngine.LastRunValue, value.Ticks.ToString());
                }
            }
        }

        bool FirstRun
        {
            get
            {
                bool result = LastRun <= IEToolbarEngine.InstallationDate;
                LastRun = DateTime.Now;
                return result;
            }
        }            

        #region IObjectWithSite Members

        private InternetExplorer explorer;
        /// <summary>

        /// Called, when the BHO is instantiated and when it is destroyed.

        /// </summary>

        /// <param name="site"></param>

        public void SetSite (Object site)
        {               
            if (site != null)
            {
                explorer = site as InternetExplorer;                
                if (FirstRun) 
                {                       
                    ShowBrowserBar (true);                    
                }                
            }   

        }
        public void GetSite (ref Guid guid, out Object ppvSite)
        {
            IntPtr punk = Marshal.GetIUnknownForObject (explorer);
            ppvSite = new object ();
            IntPtr ppvSiteIntPtr = Marshal.GetIUnknownForObject (ppvSite);
            int hr = Marshal.QueryInterface (punk, ref guid, out ppvSiteIntPtr);
            Marshal.Release (punk);
        }

        #endregion

        #region Helper Methods

        private void ShowBrowserBar (bool bShow)
        {
            try
            {
                Guid guid = typeof (IEToolbarEngine).GUID;
                object pvaClsid = (object) guid.ToString ("B");
                object pvarShow = (object) bShow;
                object pvarSize = null;
                if (bShow) /* hide Browser bar before showing to prevent erroneous behavior of IE*/
                {
                    object pvarShowFalse = (object) false;
                    explorer.ShowBrowserBar (ref pvaClsid, ref pvarShowFalse, ref pvarSize);
                }
                explorer.ShowBrowserBar (ref pvaClsid, ref pvarShow, ref pvarSize);
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show (ex.Message, "ShowBrowserBar Exception");
            }
        }
        #endregion

        #region Com Register/UnRegister Methods

        private const string BHOKeyName =
"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Browser Helper Objects";

        /// <summary>

        /// Called, when IE browser starts.

        /// </summary>

        /// <param name="t"></param>

        [ComRegisterFunction]
        public static void RegisterBHO (Type t)
        {
            //System.Windows.Forms.MessageBox.Show ("Register BHO");
            RegistryKey key = Registry.LocalMachine.OpenSubKey (BHOKeyName, true);
            if (key == null)
            {
                key = Registry.LocalMachine.CreateSubKey (BHOKeyName);
            }
            string guidString = t.GUID.ToString ("B");
            RegistryKey bhoKey = key.OpenSubKey (guidString, true);

            if (bhoKey == null)
            {
                bhoKey = key.CreateSubKey (guidString);
            }
            // NoExplorer:dword = 1 prevents the BHO to be loaded by Explorer

            string _name = "NoExplorer";
            object _value = (object) 1;
            bhoKey.SetValue (_name, _value);
            key.Close ();
            bhoKey.Close ();
        }

        /// <param name="t"></param>

        [ComUnregisterFunction]
        public static void UnregisterBHO (Type t)
        {
            RegistryKey key = Registry.LocalMachine.OpenSubKey (BHOKeyName, true);
            string guidString = t.GUID.ToString ("B");
            if (key != null)
            {
                key.DeleteSubKey (guidString, false);
            }
        }
        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Web Developer
Russian Federation Russian Federation
Alexandr Golovanov is a .NET developer at KB_Soft Group, an offshore software development company located in Russia, Novosibirsk. Here he has worked
on various .NET projects.

Comments and Discussions