// Constants for command properties
private const string CommandName = "PasteBin" ;
private const string CommandCaption = "PasteBin" ;
private const string Url = "http://pastebin.com" ;
private const string RequestParameters = "parent_pid=&format={0}&code2={1}&poster=&paste=Send&expiry=f&email=" ;
// Constants for file formats
private static readonly Dictionary< string , string > FileFormat = new Dictionary< string , string >
{
{ "c" , "c" },
{ "h" , "c" },
{ "cpp" , "cpp" },
{ "cs" , "csharp" }
};
// Variables for IDE and add-in instances
private DTE _applicationObject;
private AddIn _addInInstance;
// Buttons that will be created on built-in command bars of Visual Studio
// We must keep them at class level to remove them when the add-in is unloaded
private CommandBarButton _pasteBinButton;
* This source code was highlighted with Source Code Highlighter .
// Try to retrieve the command, just in case it was already created, ignoring the
// exception that would happen if the command was not created yet.
try
{
myCommand = _applicationObject.Commands.Item(_addInInstance.ProgID + "." + CommandName, -1);
}
catch (Exception e)
{
Debug.Write(e.ToString());
}
// Add the command if it does not exist
if (myCommand == null )
{
myCommand = _applicationObject.Commands.AddNamedCommand(_addInInstance,
CommandName, CommandCaption, null , true , 59, ref contextUIGuids,
( int )(vsCommandStatus.vsCommandStatusSupported | vsCommandStatus.vsCommandStatusEnabled));
}
* This source code was highlighted with Source Code Highlighter .
// Constants for names of built-in command bars of Visual Studio
const string vsCodeWindowCommandbarName = "Code Window" ;
// Retrieve the collection of command bars
// Note:
// - In VS.NET 2002/2003 (which uses the Office.dll reference)
// DTE.CommandBars returns directly a CommandBars type, so a cast
// to CommandBars is redundant
// - In VS 2005 or higher (which uses the new Microsoft.VisualStudio.CommandBars.dll reference)
// DTE.CommandBars returns an Object type, so we do need a cast to CommandBars
var commandBars = (CommandBars)_applicationObject.CommandBars;
// Retrieve some built-in command bars
codeCommandBar = commandBars[vsCodeWindowCommandbarName];
* This source code was highlighted with Source Code Highlighter .
// Create the buttons from the commands
// Note:
// - In VS.NET 2002/2003 (which uses the Office.dll reference)
// Command.AddControl returns directly a CommandBarControl type, so a cast
// to CommandBarControl is redundant
// - In VS 2005 or higher (which uses the new Microsoft.VisualStudio.CommandBars.dll reference)
// Command.AddControl returns an Object type, so we do need a cast to CommandBarControl
string ns = GetType().Namespace;
Assembly currentAssembly = GetType(). Assembly ;
Stream
imgStreamPic = currentAssembly.GetManifestResourceStream(ns + "." + "klipper.png" ),
imgStreamMask = currentAssembly.GetManifestResourceStream(ns + "." + "klipper-mask.png" );
if (imgStreamPic != null && imgStreamMask != null )
{
Image
pasteBinImage = Image.FromStream(imgStreamPic),
pasteBinImageMask = Image.FromStream(imgStreamMask);
// ------------------------------------------------------------------------------------
// Button on the "Code Window" context menu
// ------------------------------------------------------------------------------------
// Add a button to the built-in "Code Window" context menu
_pasteBinButton = (CommandBarButton) myCommand.AddControl(codeCommandBar,
codeCommandBar.Controls.Count + 1);
// Change some button properties
_pasteBinButton.Caption = CommandCaption;
_pasteBinButton.BeginGroup = true ; // Separator line above button
_pasteBinButton.Style = MsoButtonStyle.msoButtonIconAndCaption; // It could be also msoButtonIcon
_pasteBinButton.Picture = (stdole.StdPicture) ImageConverter.ImageToIpicture(pasteBinImage);
_pasteBinButton.Mask = (stdole.StdPicture) ImageConverter.ImageToIpicture(pasteBinImageMask);
}
* This source code was highlighted with Source Code Highlighter .
public void Exec( string cmdName, vsCommandExecOption executeOption, ref object varIn,
ref object varOut, ref bool handled)
{
handled = false ;
if (executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
{
if (cmdName == string .Format( "{0}.{1}" , _addInInstance.ProgID, CommandName))
{
handled = true ;
if (!((TextSelection)_applicationObject.ActiveDocument.Selection).IsEmpty)
{
// parameters: name1=value1&name2=value2
var webRequest = (HttpWebRequest)WebRequest.Create(Url);
webRequest.ContentType = "application/x-www-form-urlencoded" ;
webRequest.Method = "POST" ;
// formatting request string from document type and selection
string request = string .Format(RequestParameters,
FileFormat[_applicationObject.ActiveDocument.Name.Split( '.' )[1]],
System.Web.HttpUtility.UrlEncode(((TextSelection)_applicationObject.ActiveDocument.Selection).Text));
Stream os = null ;
try
{ // send the POST
webRequest.ContentLength = request.Length; //Length of request to send
os = webRequest.GetRequestStream();
os.Write(System.Text. Encoding .GetEncoding( "windows-1251" ).GetBytes(request), 0, request.Length); //Send it
}
catch (WebException ex)
{
MessageBox.Show(ex.Message, "HttpPost: Request error" ,
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
if (os != null )
{
os.Close();
}
}
try
{ // get the response
var webResponse = (HttpWebResponse)webRequest.GetResponse();
if (webResponse != null )
{ // copy to clipboard
Clipboard.SetText(webResponse.ResponseUri.ToString());
MessageBox.Show( "Upload successfully. Url is copied in your clipboard." );
}
}
catch (WebException ex)
{
MessageBox.Show(ex.Message, "HttpPost: Response error" ,
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
}
* This source code was highlighted with Source Code Highlighter .
Source: https://habr.com/ru/post/80716/
All Articles