Friday, June 24, 2011

Visual Studio macro to hide/show the Output window after a build…

I do not like the behaviors allowed in Visual Studio regarding showing the Errors List and Output window during a build. So, I finally created a macro to get the exact behavior I want.. And, I love it!

Step #1:

First off, you need to turn off two options in Visual Studio settings, under Projects and Solutions | General.

Un-check the following:

  • Always show Error List if build finishes with errors
  • Show Output window when build starts

The script does the following:

  1. When a build starts it (optionally) shows the Output window (line #9).
  2. When an individual project is finished compiling, notify the user (line #22) and (optionally) stop building any further projects (line #25).
  3. When the entire build has finished and one or more projects failed, display the Output window (line #35). If there were no failures, show and then hide the Output window (line #38) and notify the user.

Note: When the build finishes successfully, the reason I show and then hide the Output window, is because it may already be open and I want to be sure I close it. So far, I haven’t found an easy way to close the Output window, when it may or may not have focus in the Visual Studio IDE. As it is, I only rarely see it flicker open then closed. If you have any ideas, please let me know at kody@bricksoft.com!

Read the comments in the code below for details on how its done!

   1:      Private BuildFailed As Boolean = False


   2:   


   3:      ' The build has started. Could be one or more projects or the whole solution.


   4:      Private Sub BuildEvents_OnBuildBegin() Handles BuildEvents.OnBuildBegin


   5:          ' Reset the global flag.


   6:          BuildFailed = False


   7:   


   8:          ' Optionally, show the Output window.


   9:          'DTE.ExecuteCommand("View.Output")


  10:      End Sub


  11:   


  12:      ' The current project has finished building.


  13:      Private Sub BuildEvents_OnBuildProjConfigDone(ByVal Project As String, _


  14:              ByVal ProjectConfig As String, ByVal Platform As String, _


  15:              ByVal SolutionConfig As String, ByVal Success As Boolean) _


  16:              Handles BuildEvents.OnBuildProjConfigDone


  17:          If (Not Success) Then


  18:              ' Set the global flag.


  19:              BuildFailed = True


  20:   


  21:              ' Notify the user the build failed.


  22:              NotifySuccess(False)


  23:   


  24:              ' Optionally, you can cancel any further project builds.


  25:              'DTE.ExecuteCommand("Build.Cancel")


  26:          End If


  27:      End Sub


  28:   


  29:      ' The build is finished. Show or hide the Output window based on 


  30:      ' whether it was successful or not.


  31:      Private Sub BuildEvents_OnBuildDone(ByVal Scope As EnvDTE.vsBuildScope, _


  32:              ByVal Action As EnvDTE.vsBuildAction) Handles BuildEvents.OnBuildDone


  33:          If BuildFailed Then


  34:              ' Show the output window.


  35:              DTE.ExecuteCommand("View.Output")


  36:          Else


  37:              ' The build succeeded, so hide the Output window.


  38:              DTE.ExecuteCommand("View.Output")


  39:              DTE.ExecuteCommand("Window.CloseToolWindow")


  40:   


  41:              ' Notify the user the build finished successfully.


  42:              NotifySuccess(True)


  43:          End If


  44:      End Sub


  45:   


  46:      Private Sub NotifySuccess(ByVal Success As Boolean)


  47:          If Success Then


  48:              'Beep()


  49:              'Threading.Thread.Sleep(250)


  50:              'Beep()


  51:          Else


  52:              'Beep()


  53:              'Threading.Thread.Sleep(250)


  54:              'Beep()


  55:              'Threading.Thread.Sleep(250)


  56:              'Beep()


  57:              'Threading.Thread.Sleep(250)


  58:              'Beep()


  59:          End If


  60:      End Sub


0 comments: