Sunday, March 13, 2011

Custom Build sounds in Dev Studio 2008

Matthieu St. Pierre, one of the rendering coders at work, forwarded this to me at work. So I thought I'd share. It's a sweet piece of code that allows you to customize your build results to play a custom sound when your build is a success or when it fails. It's pretty damn cool and completely open to customization. So without further ado:
  1. Open up Tools | Macro | Macro IDE menu item
  2. Double-click the “MyMacros” project in the Project Explorer, and double-click the “EnvironmentEvents” item to show that module
  3. Copy and paste the code below into the macro editor window
  4. Put it right above the End Module declaration
  5. Once you have added the code, save the module and try to build something. If you don't hear anything, you may not have any sounds defined for the “Build Failed” and “Build Succeeded” events in the “Sounds and Audio Devices Properties” control panel.
Public bBuildErr As Boolean = False
Public iCnt As Integer = 0

' The DLL Import that plays the sounds
Declare Function sndPlaySound Lib "winmm.dll" _
   Alias "sndPlaySoundA" (ByVal lpszSoundName As String, _
   ByVal uFlags As Long) As Long

' A convenience method to play the sound
Private Sub PlaySound(ByVal sSoundFile As String)
   'Play async, ignore file not found
   sndPlaySound(sSoundFile, &H1 Or &H2 Or &H20000)
End Sub

Private Sub BuildEvents_OnBuildDone(ByVal Scope As EnvDTE.vsBuildScope, ByVal Action As EnvDTE.vsBuildAction) Handles BuildEvents.OnBuildDone
   Dim sSound As String

   If iCnt > 0 Then
       If bBuildErr Then
           sSound = My.Computer.Registry.GetValue( _
               "HKEY_CURRENT_USER\AppEvents\Schemes\Apps\devenv\VS_BuildFailed\.current", _
               "", "").ToString()
           sSound = My.Computer.Registry.GetValue( _
               "HKEY_CURRENT_USER\AppEvents\Schemes\Apps\devenv\VS_BuildSucceeded\.current", _
               "", "").ToString()
       End If
   End If
   bBuildErr = False
   iCnt = 0
End Sub

Private Sub BuildEvents_OnBuildProjConfigDone(ByVal Project As String, _
                                       ByVal ProjectConfig As String, ByVal Platform As String, ByVal SolutionConfig As String, _
                                       ByVal Success As Boolean) Handles BuildEvents.OnBuildProjConfigDone
   iCnt += 1
   If Not Success Then
       bBuildErr = True
   End If
End Sub
That's it! Simple and easy to install and use.