visual-studio 如何更改 Visual Studio 的标题栏文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/577188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 10:11:52  来源:igfitidea点击:

How to change the title bar text of Visual Studio

visual-studiovisual-studio-2008

提问by ProfK

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.

目前,VS 标题栏的文本为<solution-name> - Visual Studio.

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

我是否可以编写一个扩展程序来制作该文本<solution-name>: <branch-name> - <Visual Studio>

采纳答案by ProfK

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 Connectclass of the add-in will permanently (or, as long as the add-in is running) keep the title bar text as "Hello World!"

尝试设置 MainWindow.Caption 会引发异常。您必须使用 Win32 SetWindowText 函数来更改标题,但要注意:Visual Studio 会立即重置标题栏文本,因此您应该实现一个 Timer 以继续设置所需的文本。来自Connect加载项类的以下代码将永久(或者,只要加载项正在运行)将标题栏文本保留为“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!");            
}

回答by Erwin Mayer

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

我刚刚创建了一个可以提供帮助的小型 Visual Studio 扩展: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 Studiointo 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.

这个小扩展将检测何时运行两个 Visual Studio 实例并更改 Visual Studio 的窗口标题以包含解决方案的父文件夹名称。因此,它会将SolutionFolder - Microsoft Visual Studio更改 为 SolutionFolderParent\SolutionFolder - Microsoft Visual Studio

这在分支解决方案时特别有用:在两个分支具有相同解决方案名称的情况下,可以轻松识别您正在处理的分支。

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

官方页面在这里:http: //erwinmayer.com/labs/visual-studio-2010-extension-rename-visual-studio-window-title/

回答by Registered User

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.0and http://vscommands.com/releasenotes/3.6.9.0

查看VSCommands 2010 Lite 的最新版本。它引入了一项称为友好解决方案名称的功能,您可以在其中设置正则表达式模式以从文件夹结构中提取分支名称并将其放置在 Visual Studio 主窗口标题中。更多详情:http: //vscommands.com/releasenotes/3.6.8.0http://vscommands.com/releasenotes/3.6.9.0

MSDN Download Page

MSDN下载页面

回答by conical

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

在 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.

编辑:发现如果目标 .sln 文件由我们的源代码管理更新,则硬链接会中断。符号链接没有同样的问题。

回答by pasztorpisti

Just another extension to change the Visual Studio titlebar by defining it as an expression: http://visualstudiogallery.msdn.microsoft.com/2e8ebfe4-023f-4c4d-9b7a-d05bbc5cb239

通过将其定义为表达式来更改 Visual Studio 标题栏的另一个扩展:http: //visualstudiogallery.msdn.microsoft.com/2e8ebfe4-023f-4c4d-9b7a-d05bbc5cb239

The setup that makes use of a "title expression" makes this plugin quite flexible.

使用“标题表达式”的设置使这个插件非常灵活。

回答by kata

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

在 2012 年,您必须进行设置System.Windows.Application.Current.MainWindow.Title才能使其正常工作。这将更新 TaskBarItem 标题和 MainWindow 标题。

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.SolutionEventsamong others).

这只能在主线程中实现,并且由于 Visual Studio 会在不同时间点更新标题,因此您必须连接到某些事件并将其重置为您想要的任何内容(在我的 AddIn 中,我使用了EnvDTE.SolutionEvents其中的一些)。

Hope this helps.

希望这可以帮助。

回答by js.

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:

老实说,我不确定我是否正确理解了您的问题,但是我在 SO 上问过一个似乎与类似问题有关的问题:

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

使用同一 Visual Studio 2005 解决方案的不同版本/分支

回答by Eevee

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.

也许更简单的解决方案是使用虚拟桌面?空间排列更容易记住,你可以将任何相关的窗口与对应的VS组合在一起,切换会更简单。

回答by Soundararajan

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

任何基于 Visual Studio 的 IDE 都有一个名为 AppName 的属性,应该可以解决问题。

回答by Carlo V. Dango

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

http://www.helixoft.com/blog/archives/32将标题设置为当前文件名。它也适用于 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