public void Exec(string cmdName, vsCommandExecOption executeOption, ref object variantIn, ref object variantOut, ref bool handled) { if (cmdName == null) { throw new ArgumentNullException("CmdName"); } if (cmdName == typeof(Connect).FullName + "." + Strings.RefactorCommandName) { TextSelection selection = (TextSelection)(applicationObject.ActiveDocument.Selection); if (applicationObject.ActiveDocument.ProjectItem.Object != null) { Common.BaseHardCodedString stringInstance = BaseHardCodedString.GetHardCodedString(applicationObject.ActiveDocument); if (stringInstance == null) { MessageBox.Show( Strings.UnsupportedFile + " (" + applicationObject.ActiveDocument.Language + ")", Strings.WarningTitle, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } Common.MatchResult scanResult = stringInstance.CheckForHardCodedString( selection.Parent, selection.AnchorPoint.AbsoluteCharOffset - 1, selection.BottomPoint.AbsoluteCharOffset - 1); if (!scanResult.Result && selection.AnchorPoint.AbsoluteCharOffset < selection.BottomPoint.AbsoluteCharOffset) { scanResult.StartIndex = selection.AnchorPoint.AbsoluteCharOffset - 1; scanResult.EndIndex = selection.BottomPoint.AbsoluteCharOffset - 1; scanResult.Result = true; } if (scanResult.Result) { stringInstance = stringInstance.CreateInstance(applicationObject.ActiveDocument.ProjectItem, scanResult.StartIndex, scanResult.EndIndex); if (stringInstance != null && stringInstance.Parent != null) { PerformAction(stringInstance); } } else { MessageBox.Show(Strings.NotStringLiteral, Strings.WarningTitle, MessageBoxButtons.OK, MessageBoxIcon.Error); } } } }
/// <summary> /// This function is the callback used to execute a command when the a menu item is clicked. /// See the Initialize method to see how the menu item is associated to this function using /// the OleMenuCommandService service and the MenuCommand class. /// </summary> private void MenuItemCallback(object sender, EventArgs e) { DTE2 applicationObject = Package.GetGlobalService(typeof(DTE)) as DTE2; TextSelection selection = (TextSelection)(applicationObject.ActiveDocument.Selection); if (applicationObject.ActiveDocument.ProjectItem.Object != null) { BaseHardCodedString stringInstance = BaseHardCodedString.GetHardCodedString(applicationObject.ActiveDocument); if (stringInstance == null) { MessageBox.Show( Strings.UnsupportedFile + " (" + applicationObject.ActiveDocument.Language + ")", Strings.WarningTitle, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } MatchResult scanResult = stringInstance.CheckForHardCodedString( selection.Parent, selection.AnchorPoint.AbsoluteCharOffset - 1, selection.BottomPoint.AbsoluteCharOffset - 1); if (!scanResult.Result && selection.AnchorPoint.AbsoluteCharOffset < selection.BottomPoint.AbsoluteCharOffset) { scanResult.StartIndex = selection.AnchorPoint.AbsoluteCharOffset - 1; scanResult.EndIndex = selection.BottomPoint.AbsoluteCharOffset - 1; scanResult.Result = true; } if (scanResult.Result) { stringInstance = stringInstance.CreateInstance(applicationObject.ActiveDocument.ProjectItem, scanResult.StartIndex, scanResult.EndIndex); if (stringInstance != null && stringInstance.Parent != null) { PerformAction(stringInstance); } } else { MessageBox.Show(Strings.NotStringLiteral, Strings.WarningTitle, MessageBoxButtons.OK, MessageBoxIcon.Error); } } }