![]() |
General Programming »
Macros and Add-ins »
Visual Studio .NET Addins
Intermediate
License: The Code Project Open License (CPOL)
wiicwgp (what is in the clipboard is what get pasted) Add-inBy Rama Krishna VavilalaAn add-in that allows HTML element text to be pasted exactly as it is in the clipboard, disallowing VS.NET to add any extra attributes. |
C++, C#, Windows, .NET 1.0, .NET 1.1VS.NET2003, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
Chris Maunder posted in the Lounge about what he considers an annoying feature in VS.NET, that it adds ID attribute to an HTML element when pasted from a clipboard. (Actually, it adds the ID attribute only to some subset of elements like the INPUT element.) Here is what you need to see the feature in action:-
<input name="test" value="">
<input name="test" value="" id=Text1>. The text in the clipboard is still <input name="test" value=""> which can be verified by pasting in any other text editor. The purpose of this add-in is to not allow the above to happen.
Fortunately, Visual Studio .NET Extensibility API is quite rich and provides an amazing level of control. The solution turned out to be pretty simple. Visual Studio .NET fires two events before any command get executed AfterExecute and BeforeExecute. The BeforeExecute event allows the handlers to optionally cancel the command execution. So the add-in can handle the BeforeExecute event and write the text as it is and cancel the default command execution. Here is a detailed description of the add-in code.
BeforeExecute event for the Edit.Paste command. For this we need to find the Guid and ID of the command that Visual Studio .NET maintains for the command. This can be obtained from the command object corresponding the the Edit.Paste command. The following code gets the Command object.
Command cmd;
cmd = applicationObject.Commands.Item("Edit.Paste", 0);
BeforeExecute event of this command.
CommandEvents events;
events = applicationObject.Events.get_CommandEvents(cmd.Guid, cmd.ID);
events.BeforeExecute += new
_dispCommandEvents_BeforeExecuteEventHandler(this.OnBeforePaste);
OnBeforePaste method which looks like:
void OnBeforePaste(string guid, int id,
object customIn, object customOut, ref bool cancel)
If the cancel parameter value is set to true in the function, the command execution will be canceled.
OnBeforePaste function is called, we need to check whether the user is using the HTML editor. Here is how this is done. HTMLWindow htmlWindow =
applicationObject.ActiveWindow.Object as HTMLWindow;
if (htmlWindow != null)
{
//This means that user is in the HTML editor
if (htmlWindow.CurrentTab == vsHTMLTabs.vsHTMLTabsSource)
{
IDataObject dataObject = Clipboard.GetDataObject();
string text = (string)dataObject.GetData(DataFormats.Text);
TextWindow tw = (TextWindow)htmlWindow.CurrentTabObject;
tw.Selection.Insert(text,
(int)vsInsertFlags.vsInsertFlagsInsertAtStart);
}
else
{
mshtml.IHTMLDocument2 doc =
(mshtml.IHTMLDocument2)htmlWindow.CurrentTabObject;
doc.execCommand("Paste", false, null);
}
OnDisconnection event of the add-in. events.BeforeExecute -= new
_dispCommandEvents_BeforeExecuteEventHandler(this.OnBeforePaste);Follow these simple steps to start using the add-in:-
| You must Sign In to use this message board. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 17 May 2003 Editor: Smitha Vijayan |
Copyright 2003 by Rama Krishna Vavilala Everything else Copyright © CodeProject, 1999-2009 Web11 | Advertise on the Code Project |