How To Show Full File Path (or Anything Else) in VS 2005 Title Bar
- Details
- 31 Jul 2007 17:29
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.
Charles.
SetWindowText(D TE.MainWindow.H Wnd, ideTitle & " - " & DTE.Name)
HWND is integer whereas it is expected System.IntPtr.
Did you try this with 2003 as well? or is there some setting with that IDE?
Thanks for your time!!
SetWindowText(New System.IntPtr(D TE.MainWindow.H Wnd), ideTitle & " - " & DTE.Name)
I will update the main post as well.
1) Find in Files window has focus taken away from it when I click in it. The workaround is to first click the titlebar for the window and then I can navigate around in it, but the usability is really hampered here.
2) The IDE usually crashes on shutdown. The crash happens in the VS Macros host.
3) The macro only works if a source file is opened. Is there a way to have it use the path of the open Solution instead in that case?
Any ideas how these issues could be worked around?
when the WindowActivated event occurs, I want to adjust the activetated document window. I try to set the GotFocus.Width, it throws an Exception. Then I use the SetWindowPos for a try. But the value of GotFocus.HWnd is zero. The HWnd property is only used for Microsoft in MSDN. I'm not good at VB. I hope you can give me some suggestion about how to adjust the window in this situation. Please email me, yusheng9966
The crash on shutdown is due to the Timer not being Disposed(). I added the following to fix that:
Public Sub DTEEvents_OnBeg inShutdown() Handles DTEEvents.OnBeginShutdown
If Not timer Is Nothing Then
timer.Dispose()
End If
End Sub
Since I wanted the macro to display information on the solution, I changed the event to be this:
Public Sub SolutionEvents_ OnOpened() Handles SolutionEvents.Opened
SetupTitlebar()
End Sub
Private Sub SetupTitlebar()
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.Threadin g.TimerCallback = _
AddressOf tick
timer = New System.Threadin g.Timer(timerDe legate, autoEvent, 0, 1000)
End If
If DTE.Solution Is Nothing Then
ideTitle = Nothing
Else
Dim StartIndex As Integer
Dim EndIndex As Integer
Dim Found As Boolean
Found = False
ideTitle = DTE.Solution.FullName
StartIndex = InStr(ideTitle. ToLower(), ":\")
If (StartIndex > 0) Then
ideTitle = Mid(ideTitle, StartIndex + 2)
Found = True
StartIndex = InStr(ideTitle. ToLower(), "dev\")
If (StartIndex = 1) Then
ideTitle = Mid(ideTitle, StartIndex + 4)
End If
End If
If (Found) Then
EndIndex = InStr(ideTitle, "\")
If (EndIndex > 0) Then
ideTitle = Left(ideTitle, EndIndex - 1)
End If
End If
End If
showTitle(ideTitle)
Catch ex As System.Exception
End Try
End Sub
This also fixed the focus problems with FindInFiles
...
Dim sSolution = GotFocus.Document.DTE.Solution.FullName
Dim sDocument = GotFocus.Document.FullName
Dim iPrefixLen = CommonPrefixLen gth(sSolution, sDocument)
Dim sPrefix = sSolution.Subst ring(0, iPrefixLen)
ideTitle = sSolution.Subst ring(0, iPrefixLen) & " - " & sSolution.Subst ring(iPrefixLen ) & " - " & sDocument.Substring(iPrefixLen)
...
Private Function CommonPrefixLen gth(ByVal s1 As String, ByVal s2 As String) As Integer
Dim iMax = Math.Min(s1.Len gth, s2.Length)
For i = 0 To iMax - 1
If s1(i) s2(i) Then
Return i
End If
Next
Return iMax
End Function