|
|||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionThis 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. BackgroundFirst, you need to understand how to manually add an Internet Explorer context menu item, and this is an interesting link to check. Creating the ProjectYour ingredients for such a project are:
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:
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
Creating the Installer ClassThe main purpose of the installer is to create the registry keys, so here goes: //
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:
Creating the Action FileThis action file is the actual code we are running when clicking on the context menu: <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.) InstallingOnce you have completed all of the above, build your solution and install. You should have the following key added to your registry:
If you open a new instance of Internet Explorer and right click on a selected text, you should be getting:
RecapSo basically you should now be able to create and delete registry keys upon installation with no problem at all. NoteNow 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.
|
||||||||||||||||||||||||||||||||||||||||