How to change the title bar text of Visual Studio
Asked Answered
S

10

62

We work on several different branches of the same code, and when working on two branches at once, it can become confusing and time wasting.

Presently, the VS title bar has the text <solution-name> - Visual Studio.

Is it possible for me to write an extension that will make that text <solution-name>: <branch-name> - <Visual Studio>?

Sheelagh answered 23/2, 2009 at 10:42 Comment(7)
All my visual studio instances has the title "projectname - -Microsoft Visual Studio". Do you only have "Visual Studio"?Thaddeus
Ever think about changing the name of your solution after branching?Dubuffet
@Stefan, mine has solutionname - Microsoft Visual Studio, but I want it to have solutionname - version+branch. I pretty much know it's Visual Studio.Sheelagh
@Will, I am not in control of the branching, and the guy that is has reasons to not use multiple solution names.Sheelagh
@Will - I often have multiple copies of the same solution checked out - it would be nice if this could be solved in a generic way to meet different needs - personally I'd like to show the path to the solution fileCoronado
I would love something like this as well. I have the exact same need - not just multiple branches but also multiple copies locally (i.e. multiple P4 workspaces). If you end up making an extension, please share it. :)Trihedral
This happened to my 2012 VS environment after installing a vs add in. It now says 'default Branch - Solution Name' which is quite annoying, as all my task bars icons say "default branch". Wish I knew which add on is the culprit, and how to disable/change it from within settingsMastigophoran
S
3

Trying to set MainWindow.Caption throws an exception. You have to use the Win32 SetWindowText function to change the title, but beware: Visual Studio resets the title bar text at the drop of a hat, so you should implement a Timer to keep setting your desired text. The following code from the Connect class of the add-in will permanently (or, as long as the add-in is running) keep the title bar text as "Hello World!"

public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
    _applicationObject = (DTE2)application;
    _addInInstance = (AddIn)addInInst;
    resetTitleTimer = new Timer(new TimerCallback(SetMainWindowTitle), "Hello world!", 0, 10);
}

[DllImport("user32.dll")]
private static extern bool SetWindowText(IntPtr hWnd, string lpString);
private void SetMainWindowTitle(object state)
{
    IntPtr hWnd = (IntPtr)_applicationObject.MainWindow.HWnd;
    SetWindowText(hWnd, "Hello World!");            
}
Sheelagh answered 23/2, 2009 at 17:12 Comment(1)
This no longer works with Visual Studio 2012... Only the title in the task bar will change.Sensitize
S
57

I just created a small Visual Studio extension that can help: http://visualstudiogallery.msdn.microsoft.com/f3f23845-5b1e-4811-882f-60b7181fa6d6

This small extension will detect whenever two instances of Visual Studio are running and change the window title of Visual Studio to include the parent folder name of the solution. It will therefore change SolutionFolder - Microsoft Visual Studio into SolutionFolderParent\SolutionFolder - Microsoft Visual Studio.

This is particularly useful when branching a solution: it becomes possible to easily identify which branch you are working on, in the case where both would have the same solution name.

Official page here: http://erwinmayer.com/labs/visual-studio-2010-extension-rename-visual-studio-window-title/

Sensitize answered 15/4, 2011 at 18:58 Comment(10)
Since this post, Erwin's updated this tool with options allowing for more control on what and when the title is updated. (For example, a single open solution can be renamed.) This is perfect without all the bloat of VS Commands 10. Thank you!Dzoba
@Erwin Mayer - this is the best solution. I continually look back for this extension every new install. Simple, straightforward. A great asset to the community. Be sure, folks, that you check the directory-level naming options. I name mine with the second ancestor, for example.Skitter
@Skitter Thanks for your kind compliment. I am trying to make this extension work with VS 2012 but the current implementation only changes the title in the taskbar, not at the top of the Window. I suspect Microsoft is no longer using the standard window type so SetWindowText is not effective. If anyone has an idea to overcome this please let me know!Sensitize
Fantastic extension. I'm sure you'll have it updated by the time I move to 2012. :)Stratum
@Stratum Thanks! I am still struggling to find a way to modify the title. I tried the most recent EnvDTE interfaces but to no avail. I was able to locate the TitleBar that contains the Visual Studio title (the other TitleBar that is more standard is kind of "hidden" but still there, with no title set) but I cannot access it through the UI Automation Framework either. I am hitting dead ends on the VS SDK side, and the Win32 methods are only partially working. I keep crossing my fingers and welcome any suggestion.Sensitize
One more Up/Down vote combo and this will be the top answer. Thank you SO MUCH Erwin.Bailiff
I wish I could put the full path of the active document. I got close with [parent2][parent1] but that doesn't show me all levels of the folder the document is inWonacott
No luck with [parentPath]?Sensitize
Have you tried to fiddle with the settings in Visual Studio under tools->Options->Rename VS Window Title. There are two settings "Closest parent folder depth" and "Farthest parent folder depth". I changed both of those to 2 and got the project and version number. Give that a shot?Beatific
Still works for VS2015 and 2013 I see, awesome. Still a great extension.Lorettelorgnette
A
18

Check out latest release of VSCommands 2010 Lite. It introduced a feature called Friendly Solution Name where you can set up a regex pattern to extract branch name from folder structure and have it placed in Visual Studio main window title. More details: http://vscommands.com/releasenotes/3.6.8.0 and http://vscommands.com/releasenotes/3.6.9.0

MSDN Download Page

Amperehour answered 29/4, 2011 at 8:29 Comment(4)
This is just awesome. Saved my team from a lot of edits to the wrong branch!Leahy
Does the job... adds a considerable amount of extra items if all you're looking for is the rename. I recommend Erwin's extension over this.Dzoba
I love the Attach To Local IIS button! Only saves a few clicks, but still!!Abbotsen
Thank you. Once I figured out that it was VSCommands changing the TitleBar it was easy to undo.Tectonic
S
3

Trying to set MainWindow.Caption throws an exception. You have to use the Win32 SetWindowText function to change the title, but beware: Visual Studio resets the title bar text at the drop of a hat, so you should implement a Timer to keep setting your desired text. The following code from the Connect class of the add-in will permanently (or, as long as the add-in is running) keep the title bar text as "Hello World!"

public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
    _applicationObject = (DTE2)application;
    _addInInstance = (AddIn)addInInst;
    resetTitleTimer = new Timer(new TimerCallback(SetMainWindowTitle), "Hello world!", 0, 10);
}

[DllImport("user32.dll")]
private static extern bool SetWindowText(IntPtr hWnd, string lpString);
private void SetMainWindowTitle(object state)
{
    IntPtr hWnd = (IntPtr)_applicationObject.MainWindow.HWnd;
    SetWindowText(hWnd, "Hello World!");            
}
Sheelagh answered 23/2, 2009 at 17:12 Comment(1)
This no longer works with Visual Studio 2012... Only the title in the task bar will change.Sensitize
B
1

I added a symbolic link with a different name targeting the solution file. Open the solution with the symbolic link and the window title has the symbolic link name.

In windows: mklink BlawBranch.sln Blaw.sln

EDIT: Found that a hard link breaks if target .sln file is updated by our source control. A symbolic link doesn't have the same problem.

Brie answered 4/10, 2012 at 18:40 Comment(4)
That works at first, but when you edit your solution and Visual Studio rewrites the .sln file it deletes it before writing it, which severs the link and you end up losing edits because your source control system is unaware that you've made any changes. Been there, done that. :-)Muddlehead
@Muddlehead I made a change to the solution file in Visual Studio 2010 by adding a project, then close/saving the solution. The changes were made to the hardlink target and the link was not broken. Did you by by chance see the behavior you speak of in an earlier version of VS?Brie
I was using VS 2005, so hopefully it's been fixed since then.Muddlehead
Where is mklink.exe? I tried where mklink but got the error ' INFO: Could not find files for the given pattern(s).'Hare
S
0

To be honest, I am not sure I am understanding your question correctly, but I had asked one here on SO that seems to be about a similar problem:

Working with different versions/branches of the same Visual Studio 2005 solution

Smallman answered 27/2, 2009 at 9:43 Comment(0)
H
0

Perhaps a simpler solution would be to use virtual desktops? Spacial arrangement is easier to remember, you could group any related windows with the corresponding VS, and switching would be simpler.

Houston answered 27/2, 2009 at 16:49 Comment(0)
D
0

there is a property by name AppName for any visual studio based IDE, that should do the trick.

Detrain answered 23/2, 2012 at 11:1 Comment(1)
Which object would have such a member? EnvDTE has no such member.Sensitize
W
0

From http://www.helixoft.com/blog/archives/32 sets the title to the current filename. It also works on Visual Studio 10

  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
Wendolyn answered 14/8, 2012 at 21:43 Comment(0)
A
0

In 2012, you have to set System.Windows.Application.Current.MainWindow.Title in order for this to work. This will update both the TaskBarItem title and the MainWindow title.

This is only possible from the main thread and since the title will get updated at various points by Visual Studio, you have to hook up to some events and reset it to whatever you wanted it to be (in my AddIn, I use some EnvDTE.SolutionEvents among others).

Hope this helps.

Azoic answered 4/1, 2013 at 12:54 Comment(0)
S
-1

In the VS automation model there is

_DTE.MainWindow.Capation

which you could start with.

See http://msdn.microsoft.com/en-us/library/envdte._dte.mainwindow.aspx

Speedway answered 23/2, 2009 at 11:43 Comment(1)
Caption can only be edited with ToolWindows. Not the MainWindow.Sensitize

© 2022 - 2024 — McMap. All rights reserved.