Click here to Skip to main content
15,868,016 members
Articles / Desktop Programming / Win32
Article

Creating and Installing Internet Explorer Context Menu using C#

Rate me:
Please Sign up or sign in to vote.
3.67/5 (8 votes)
9 Feb 2008CPOL2 min read 65.3K   1.4K   52   3
Add an Internet Explorer context menu using an installer class

Introduction

This article basically illustrates how to create an Internet Explorer context menu item upon installation using C#. Usually such projects are not standalone, and are included upon other features, but this will show a step by step guide to create one.

Background

First, you need to understand how to manually add an Internet Explorer context menu item, and this is an interesting link to check.

Creating the Project

Your ingredients for such a project are:

  1. C# Class Library
  2. Setup Project

For the C# class library, delete the default class, right click on the project name and click Add New Item and choose installer class.

For the setup project, just add the primary output of the installer class project and add this output to all four default custom actions. (Right click on the setup project name, select View > Custom action.)

At the end, your solution should look like this:

solution.jpg

Quite simplistic. Now we just need to adjust one more thing in the setup project before tackling the code. Basically we need to grab the target directory submitted by the user upon installation, to do this.

Select the Primary output added under the Install Action, and type in the CustomActionData as depicted in the picture below:

targetDir.jpg

Creating the Installer Class

The main purpose of the installer is to create the registry keys, so here goes:

C#
//          
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
//DON'T FORGET TO ADD THIS ONE - used for the Registry Class
using Microsoft.Win32;

namespace IEContextInstallerClass
{
    [RunInstaller(true)]
    public partial class IEInstaller : Installer
    {
        public IEInstaller()
        {
            InitializeComponent();
        }

        public override void Install(System.Collections.IDictionary stateSaver)
        {
            base.Install(stateSaver);

            //grab the target director and add to install.installstate
            //note that the grabbed parameters are exactly the same 
            //as the one we set above
            stateSaver.Add("TargetDir", Context.Parameters["DP_TargetDir"].ToString());
        }

        public override void Commit(System.Collections.IDictionary savedState)
        {
            //retrieve the saved state
            base.Commit(savedState);
            //adding items to the context menu of IE
            RegistryKey key;
            //1,2,4,8,10,20 in heximal are 1,2,4,8,16,32 in decimal (respectively)
            string keyValueInt = "16";
            //the location of our key
            string subKey = 
        @"SOFTWARE\Microsoft\Internet Explorer\MenuExt\Sample Action";
            //create it
            key = Registry.CurrentUser.CreateSubKey(subKey);
            //set the value
            key.SetValue("Contexts", Convert.ToInt32(keyValueInt), 
                RegistryValueKind.DWord);
            //set the path to the action file
            key.SetValue(null, "file://" + savedState["TargetDir"].ToString() + 
                "\\action.html");
            //and close
            key.Close();
        }

        protected override void OnBeforeUninstall
            (System.Collections.IDictionary savedState)
        {
            base.OnBeforeUninstall(savedState);
            //removing items from the context menu of IE
            string subKey = 
        @"SOFTWARE\Microsoft\Internet Explorer\MenuExt\Sample Action";
            Registry.CurrentUser.DeleteSubKey(subKey);
        }
    }    
}

Now basically the installer class does the following:

  1. Upon installation, grabs the target directory and saves it so we can use it at a later stage
  2. Upon committing, creates the registry keys
  3. In case the user wishes to uninstall, simply deletes the keys

Creating the Action File

This action file is the actual code we are running when clicking on the context menu:

JavaScript
<SCRIPT LANGUAGE = "JavaScript"> 
// Get the window object where the context menu was opened.
var oWindow = window.external.menuArguments; 
// Get the document object exposed through oWindow.
varoDocument = oWindow.document;
 // Get the selection from oDocument.
// in oDocument.
varoSelect = oDocument.selection;
 // Create a TextRange from oSelect.
varo SelectRange = oSelect.createRange(); 
// Get the text of the selection.
var sNewText = oSelectRange.text; 
// If something is selected, alert showing the selected text.
if(sNewText.length != 0){
alert(sNewText);
}
</SCRIPT> 

The above is a simple JavaScript that grabs the selected text and show it in a message box... you can have other actions depending on your needs. (The code above is based on this.)

Installing

Once you have completed all of the above, build your solution and install.

You should have the following key added to your registry:

registry.jpg

If you open a new instance of Internet Explorer and right click on a selected text, you should be getting:

inAction.jpg

Recap

So basically you should now be able to create and delete registry keys upon installation with no problem at all.

Note

Now this is definitely not something innovative or out of the ordinary, but I couldn't find something similar on The Code Project, so I thought of adding it.
Additionally, this is a compilation of items I found while "Googling" here and there...

License

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


Written By
Other Born Interactive
Saudi Arabia Saudi Arabia
Follow me: http://riadafyouni.blogspot.com

Comments and Discussions

 
QuestionGood job my friend Pin
Bonus Kun13-Jan-12 6:06
Bonus Kun13-Jan-12 6:06 
QuestionHow to get image on page? Pin
Sosyopat18-Aug-10 4:21
Sosyopat18-Aug-10 4:21 
QuestionHow To get the yahoo mail inbox message title Pin
Dabara18-Jun-08 0:51
Dabara18-Jun-08 0:51 

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.