Single layout for 'edit' and 'debug' in Visual Studio

后端 未结 4 445
后悔当初
后悔当初 2021-01-30 20:12

In Visual Studio 2008, \'Start Debugging\' switches Visual Studio to a different layout. How can I force Visual Studio to use a single layout at all times?

I could atte

4条回答
  •  温柔的废话
    2021-01-30 20:49

    As others have pointed out, you can't use one settings group to control both design and debug views. Visual Studio doesn't make it easy to get the most out of window placement settings, but the approach I use to manage layouts might help.

    Instead of frequently adjusting window placements by hand, try to think of a fixed number of different views you want to work with. Eclipse has perspectives, window layouts you can switch between. Think of Visual Studio this way. For example, I use two layouts in Visual Studio: one to take advantage of two monitors when I'm sitting at my physical workstation, another for one monitor when I'm working remotely. If you can constrain yourself to using a group of layouts that makes sense for you without manually adjusting windows, you can make design and debug window placements the same for each layout.

    However, switching between layouts is painful with Visual Studio out of the box. You have to go to Tools->Import and Export Settings and select the layout manually. It takes more than five mouse clicks and sometimes 15 seconds to switch layouts this way on my workstation. We can do much better!

    1. Save the window positions you want for each layout to settings files
    2. Make a macro to load each settings file
    3. Bind the macros to keyboard shortcuts or toolbar buttons

    Save window positions to a settings file

    Arrange windows the way you want them for a specific layout. Visual Studio saves the location for nearly every window (e.g. Solution Explorer, Output, Find and Replace), so be thorough. Visual Studio saves design and debug layouts in a single file so arrange windows in both views.

    Go to Tools->Import and Export Settings. Choose Export selected environment settings and click Next.
    The next dialog prompts you to select the settings to export. Uncheck all settings except General Settings/Window Layouts so only window placements are saved, like in these screen shots (I can't expand the window so here are two shots of the same dialog):

    Choose settings to export

    Choose settings to export

    Enter a name for this settings file and save it. Repeat until you have a settings file for each layout. There is no limit to how many settings files you may have.

    Make a macro to load each settings file

    Go to Tools->Macros->Macro Explorer to show your macros. There should already be a macro project named MyMacros. Create an empty macro project if none are visible. Double click any module in any of these projects to open up the macro editor.

    Enter this into the editor. You want one main sub that takes a path to a settings file and loads the file, and one sub for each individual file that calls the main sub. If you save your settings files to the same folder you can have the per-file subs pass just the file name instead of the whole path.

    Imports System
    Imports EnvDTE
    Imports EnvDTE80
    Imports EnvDTE90
    Imports System.Diagnostics
    
    Public Module Module1
    
        Private RootFolder As String = "C:\Path\To\Folder\With\Settings\Files\"
    
        Private Sub ImportSettingsFile(ByVal FileName As String)
            FileName = IO.Path.Combine(RootFolder, FileName & ".vssettings")
            DTE.ExecuteCommand("Tools.ImportandExportSettings", "-import:""" & FileName & """")
        End Sub
    
        'Corresponds to file layoutA.settings    
        Public Sub ImportLayoutA()
            ImportSettingsFile("layoutA")
        End Sub
    
        'Corresponds to file layoutB.settings
        Public Sub ImportLayoutB()
            ImportSettingsFile("layoutB")
        End Sub
    
        'Repeat for each settings file
    
    End Module
    

    Close the macro editor and go back to Visual Studio. You're done! Running any of these macros will load the settings files automatically. You can double click any of the subs in Macro Explorer to run them. If you display Macro Explorer at all times this might be sufficient, but if you don't or would rather not have to click the macros to run them we can do even better ...

    Bind the macros to keyboard shortcuts or toolbar buttons

    Go to Tools->Options->Environment->Keyboard. This window allows you to change any keyboard bindings. Type "Macro" without quotes into the Show commands containing text box. This will show the macros you created. Select any macro, click in the text box titled Press shortcut keys, and enter the keyboard shortcut you want to use to run the macro. Hit Assign, then OK. You can now use this keyboard command to load the settings file.

    enter image description here

    Alternately, you can use a toolbar button instead of or in addition to a keyboard binding. Go to Tools->Customize. Select the Commands tab, select Toolbar, and select the toolbar you want to add the button to (Standard is the main toolbar). Click Add Command, select the Macros category, select the macro you want to add a button for, and click OK. You will have a new button on the toolbar that loads the window layout from that macro.

    enter image description here

    enter image description here

    Keep in mind that keyboard shortcuts and toolbar buttons are themselves settings. If you import a settings file that overwrites either of them you will have to redo this last step. The window placement settings files won't overwrite these values because you only exported window locations. It's a good idea to periodically export and back up all settings, not just window settings, in case something like this happens and you want to recover non-window settings.

提交回复
热议问题