I saw an interesting request on discussion forums today. A user wants to show the full path of currently edited file in the IDE main window caption. This used to be case in VS .NET 2003 but VS 2005 shows only solution name.
I thought it had to be easy to write macro which does the job. It turned it was more difficult than it seemed but finally it worked. Your title bar will look like this:
To create and apply this macro:
- Go to menu Tools - Macros - Macros IDE...
- In the Macros IDE Class View navigate to MyMacros - {} MyMacros - EnvironmentEvents. Open (double-click) EnvironmentEvents.
-
Paste this code inside module (just before End Module line):
Private timer As System.Threading.Timer Private ideTitle As String = Nothing Declare Auto Function SetWindowText Lib "user32" (ByVal hWnd As System.IntPtr, _ ByVal lpstring As String) As Boolean '''<summary>Called when any window in VS gets activated.</summary> '''<param name="GotFocus">Window that got focus.</param> '''<param name="LostFocus">Window that lost focus.</param> Private Sub WindowEvents_WindowActivated(ByVal GotFocus As EnvDTE.Window, ByVal LostFocus As EnvDTE.Window) Handles WindowEvents.WindowActivated Try If timer Is Nothing Then ' Create timer which refreshes the caption because ' IDE resets the caption very often Dim autoEvent As New System.Threading.AutoResetEvent(False) Dim timerDelegate As System.Threading.TimerCallback = _ AddressOf tick timer = New System.Threading.Timer(timerDelegate, autoEvent, 0, 200) End If If GotFocus.Document Is Nothing Then ideTitle = Nothing Else ideTitle = GotFocus.Document.FullName showTitle(ideTitle) End If Catch ex As System.Exception End Try End Sub ''' <summary>Dispose the timer on IDE shutdown.</summary> Public Sub DTEEvents_OnBeginShutdown() Handles DTEEvents.OnBeginShutdown If Not timer Is Nothing Then timer.Dispose() End If End Sub '''<summary>Called by timer.</summary> Public Sub tick(ByVal state As Object) Try If Not ideTitle Is Nothing Then showTitle(ideTitle) End If Catch ex As System.Exception End Try End Sub '''<summary>Shows the title in main window.</summary> Private Sub showTitle(ByVal title As String) SetWindowText(New System.IntPtr(DTE.MainWindow.HWnd), title & " - " & DTE.Name) End Sub
- That's all. Close macro IDE and restart VS.
How it works
This macro handles VS events. To do so, you must write it in special module: EnvironmentEvents. In this case we handle WindowEvents.WindowActivated event.
There were several problems that I needed to solve:
- You cannot use DTE.MainWindow.Caption to set the title bar. It throws an exception. This property can only be used for reading it. To set the title, we need to use API call to SetWindowText.
- VS IDE is very aggressive when setting the caption. Once we set our own caption in event handler when some document window is selected, the IDE sets its own caption immediatelly after that. So I had to set the timer which refreshes the title bar every 200 milliseconds. This seems to work perfectly. If you know better solution, please let us know here in comment section.
- We cannot use Timer class from Windows.Forms namespace. It simply doesn't work in macro. We need to use System.Threading.Timer.
You can of course change title bar text to anything you want. The macro code is really simple.