Adding a Visual Studio toolbar button for a command that is only available as a keyboard shortcut

对着背影说爱祢 提交于 2019-12-19 04:03:03

问题


This question relates to this ReSharper YouTrack issue.

In Visual Studio 2010 with ReSharper 7.1.1 installed, if I go to Tools > Options > Environment > Keyboard, there is a command called ReSharper_SilentCleanupCode.

I would like to bind this command to a toolbar button.

This seems to be impossible using Tools > Customize > Commands because the only commands available within this dialog are for actions that already have an associated menu item. The particular ReSharper command I'm interested in (Silent Code Cleanup) doesn't appear in any menu, so it cannot be assigned to a toolbar button using the "GUI".

Is there any other way to bind a keyboard-only command to a toolbar button? (One of ReSharper's programmers thought the "VS script editor" could be used, but I'm not having any luck finding info on this.)

Edit

I should have mentioned this in the first place. While azhrei's macro solution is great for Visual Studio 2010, it will break once I upgrade to VS 2012, because macros are no longer supported. If someone has a solution that will continue to work in VS 2012, that would be preferable. (Or perhaps VS 2012 toolbars don't have the same limitation in the first place?)


回答1:


Add a macro that executes the command, then add the macro to a toolbar.

This works because it makes the keyboard-only command appear in the Macros menu in the Customize Commands dialog.

Details

Add a macro which does this:

    Sub _ReSharper_SilentCleanupCode()
        DTE.ExecuteCommand("ReSharper_SilentCleanupCode")
    End Sub

Put this macro in a module which appears in Customize..Commands..AddCommand..Categories..Macros, such as Samples or MyMacros.RecordingModule, but not MyMacros.Module1 (the default when using the macro IDE).

Go to Tools..Customize..Command and select the Toolbar you want.

Now Add Command... and select the Macros category.

Select your Macros.Samples._ReSharper_SilentCleanupCode macro.

Click Modify Selection and change the name to #-) or whatever text makes you think ReSharper Silent Code Cleanup without being too long for your toolbar. :-)

I tried this with Visual Studio 2010 and ReSharper 7.1.2.

Edit

Visual Commander is a apparently way to get this going on VS2012 as well - see comments below for more.



来源:https://stackoverflow.com/questions/15394784/adding-a-visual-studio-toolbar-button-for-a-command-that-is-only-available-as-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!