Developing a Browser Toolbar: Microsoft Internet Explorer (3/5)

This post series reflects my article “Developing a Browser Toolbar” published in the ASPects in January 2010 (Volume 23, Issue 1), a magazine of the Association of Shareware Professionals (ASP).

The first decision to make is which programming language you want to use for developing the Internet Explorer toolbar. I did some research in the web and found examples for C++, Visual Basic 6 and .NET. There seem to be some visual styles issues; it obviously does not look so good on Windows Vista and Windows 7 systems by using the C++ version. And as I really don’t like to develop in Visual Basic 6 anymore, I decided to go the .NET way. The only disadvantage of this decision is that the user needs to have the .NET Framework 2.0 (or higher) installed on the system. But I can live with that, because it is very common already.

Since almost all of my products are developed in .NET, I already had the Visual Studio 2008 installed on my computer. If not, you’ll need at least the Express Edition (version 2005 should be enough, too) for the next steps. I decided to develop the toolbar in C#, but Visual Basic .NET could do the same thing, of course.

As a good starting point I found this and this article at the Code Project webpage. If you want to read more about the COM component, you can also read the C++ tutorial, but that’s not necessarily needed. By using the demo source code of those articles you should get a working toolbar – at least on a Windows XP system. But there were some issues I came across that needed to be fixed and therefore I will describe them in more detail here:

Making Toolbar Show up Correctly on All Operating Systems

By using the demo code of the articles mentioned above, I got the toolbar to work quite fast on my Windows XP testing machine. I was naively thinking that if the toolbar works on Windows XP with Internet Explorer 8, it will also work on Windows Vista and Windows 7 with the same Internet Explorer version. But indeed, I had to learn that this is absolutely not the case! It either just does not show up at all or so far on the right side, that you can only see the toolbar if you have a horizontal resolution of at least 2,000 pixels. So I had to search for a solution, but most changes to the code resulted in the toolbar working either on the one or on the other operating system or browser version combination. After some hours of frustrated testing, I changed the GetBandInfo procedure of the BandObjectsLib class in the following way. Although I’m still not absolutely sure why, it works fine now on all operating system:

public virtual void GetBandInfo(UInt32 dwBandID, UInt32 dwViewMode, ref DESKBANDINFO dbi)
{
    if ((dbi.dwMask & DBIM.MINSIZE) != 0)
    {
        dbi.ptMinSize.X = this.MinimumSize.Width;
        dbi.ptMinSize.Y = this.MinimumSize.Height;
    }

    if ((dbi.dwMask & DBIM.MAXSIZE) != 0)
    {
        dbi.ptMaxSize.X = this.MaximumSize.Width;
        dbi.ptMaxSize.Y = this.MaximumSize.Height;
    }

    if ((dbi.dwMask & DBIM.ACTUAL) != 0)
    {
        dbi.ptActual.X = this.Size.Width;
        dbi.ptActual.Y = this.Size.Height;
    }

    if ((dbi.dwMask & DBIM.BKCOLOR) != 0)
    {
        dbi.dwMask &= ~DBIM.BKCOLOR;
    }

    dbi.dwModeFlags = DBIMF.BREAK;
}

Nicer Appearance on Windows XP and above

If you don’t add the following event handler to the BandObjectsLib class, your toolbar will still work, but will just not look very good on Windows XP and above. The if-statement in the event function additionally makes sure that the toolbar still works on older operating systems like Windows 98, 2000 and ME. Otherwise it would throw an exception on these operating systems. Here is the code I’ve used to get it to work correctly on all operating systems:

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

protected override void OnPaintBackground(PaintEventArgs e)
{
    if (System.Environment.OSVersion.Platform >= PlatformID.Win32NT &&
           (Environment.OSVersion.Version.Major == 5 &&
            Environment.OSVersion.Version.Minor >= 1 ||
            Environment.OSVersion.Version.Major >= 6) &&
           this.BackColor == Color.Transparent)
    {
        // Only if operating system is Windows XP or higher
        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);
    }
}

No Serialization Allowed

When developing a Firefox toolbar you can use the Mozilla preferences service to store and load user-defined values inside Firefox itself. Because there is no equivalent in Internet Explorer and I’m using XML serialization in many of my other .NET products, I wanted to use it in the toolbar project, too. This worked fine at first sight, but if the Protected Mode is enabled in Internet Explorer 8 (and it is by default, at least since Windows Vista), it shows an ugly warning dialog to the user whenever the toolbar is initialized. I won’t explain the reason in detail here, but just don’t use serialization in Internet Explorer toolbars. You can save user-defined values either without serialization in the file system below the local application data directory or simply by using the registry.

Automatic Online Update

As I did in the Firefox version, I also wanted the Internet Explorer toolbar to come with an automatic update feature. Because there is no update system for toolbars in Firefox, you have to implement it yourself. But this is quite easy, because you can use, as in any other .NET application, the WebClient class for doing a HTTP request to first ask if there is a new update available and then to receive the update file if needed. On the server side I have a small PHP script handling the update requests. The update file which is downloaded is the normal setup executable that just overrides all files with the new ones. The only thing you have to care about is the way you call the executable setup file after downloading, because otherwise a User Account Control (UAC) prompt will be shown. I’ve written this procedure that you can use for executing a file from within the toolbar code without any warning displayed to the user:

private void startProcess(string fileName, string arguments)
{
    System.Diagnostics.ProcessStartInfo startInfo =
        new System.Diagnostics.ProcessStartInfo();
    startInfo.UseShellExecute = true;
    startInfo.WorkingDirectory = Environment.CurrentDirectory;
    startInfo.FileName = fileName;
    if (Environment.OSVersion.Platform >= PlatformID.Win32NT &&
            Environment.OSVersion.Version.Major >= 6)
        // Only if operating system is Windows Vista or higher
        startInfo.Verb = "runas";
    else
        startInfo.Verb = "open";
    startInfo.Arguments = arguments;
    startInfo.ErrorDialog = true;
    try
    {
        System.Diagnostics.Process process = System.Diagnostics.Process.Start(startInfo);
        process.WaitForExit();

        MessageBox.Show(objToolbarData["msg.restartBrowserText"],
                        objToolbarData["msg.restartBrowserTitle"],
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
    catch (Exception ex)
    {
        if (debugMode) MessageBox.Show(ex.ToString());
    }
}

Uninstall Button Inside the Toolbar

That’s the only thing which is easier to implement in Internet Explorer than in Firefox; it is enough to call the uninstall executable (again with the startProcess procedure above) of the installation system (see next post###) to uninstall the toolbar again. It will disappear then after browser has been restarted (just as on Firefox).

It’s done; the Internet Explorer version of our toolbar is ready, too! It will work on Internet Explorer 6 or higher running on Windows 98 or higher with an installed .NET Framework 2.0.

In the next post I’ll explain how we get the toolbars installed.

Contents of Post Series “Developing a Browser Toolbar”:

This post is also available in Deutsch.

One thought on “Developing a Browser Toolbar: Microsoft Internet Explorer (3/5)

  1. Pingback: Developing a Browser Toolbar: Installation System (4/5) | AB-WebLog.com

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>