Helixoft Blog

Peter Macej - lead developer of VSdocman - talks about Visual Studio tips and automation

How To Show Full File Path (or Anything Else) in VS 2005 Title Bar

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.

Visual Studio 2005 title bar showing 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:

New VS 2005 title bar with full file path

To create and apply this macro:

  1. Go to menu Tools - Macros - Macros IDE...
  2. In the Macros IDE Class View navigate to MyMacros - {} MyMacros - EnvironmentEvents. Open (double-click) EnvironmentEvents.
  3. Paste this code inside module (just before End Module line):
  4.     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

  5. 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:

  1. 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.
  2. 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.
  3. 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.

9 Responses to “How To Show Full File Path (or Anything Else) in VS 2005 Title Bar”

  1. kyong:

    tried it out, but having to refresh every 200 milliseconds kills the mojo for me… maybe if there was a simpler way.. =) but nice work!!

  2. Charles:

    This is GREAT! It works well… now how about something similar for Visual Studio 6? For long paths, all I get is “[C:\…\DStringUtils.h]”

    Charles.

  3. santosh:

    The Macro is not compiling [ok I am trying this for VS 2003]
    SetWindowText(DTE.MainWindow.HWnd, 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!!

  4. Peter Macej:

    You are right. You have probably set Option Strict On in your macros. The solution is simple. Just use:
    SetWindowText(New System.IntPtr(DTE.MainWindow.HWnd), ideTitle & ” - ” & DTE.Name)
    I will update the main post as well.

  5. Jimmy:

    I love this macro but I noticed it causes some undesirable side effects in the IDE:

    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?

  6. yusheng:

    This article is very useful for me. I want to write a samilar macros, however, there are some errors.
    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@gmail.com

  7. Jimmy:

    I solved all of the problems I mentioned in my Feb 5 post.

    The crash on shutdown is due to the Timer not being Disposed(). I added the following to fix that:

    Public Sub DTEEvents_OnBeginShutdown() 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.Threading.TimerCallback = _
    AddressOf tick
    timer = New System.Threading.Timer(timerDelegate, 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

  8. Peter Macej:

    Thank you Jimmy. You are right, the timer needs to be disposed when IDE closes. I have updated the code with your function.

  9. Derek Morin:

    Seems to work like a charm. Too bad it is flashy, but sounds like that is visual studios problem, and you’ve managed a good workaround.

Leave a Reply

authimage

Can't read the code? Try reloading the page before adding your comment to get a new code image.

 

VSdocman

Code Commenter and Generator of Class Documentation for VB .NET and C#

Create XML comments, generate MSDN documentation, IntelliSense, F1 help and more for your VB .NET and C# code.

More... | Download | Buy


.



 
© Copyright 2007 Helixoft All rights reserved. | Privacy Statement