[Sample of Feb 25th] Customize Visual Studio Status Bar

 

Homepage image
Sample of the Day RSS Feed

Sample Download: https://code.msdn.microsoft.com/Customize-VS-status-bar-in-0990ed78

imageToday’s code sample demonstrates customizing the status bar of Visual Studio.  The Visual Studio status bar, the horizontal region at the bottom of the Visual Studio design surface, provides a convenient way to convey information about the current state of the integrated development environment (IDE). In this sample, we will demo: 1) Write highlighted text in its feedback region 2) Read text from the feedback region 3) Show progress bar in status bar 4) Show animation in the status bar 5) Write row, column and char to designer region.  If you are learning Visual Studio Extensibility or your project is about customizing Visual Studio with VSX, the code sample could be helpful to you.

The sample was written by our star Sample writer: Ruiz Yi.

imageYou can find more code samples that demonstrate the most typical programming scenarios by using Microsoft All-In-One Code Framework Sample Browser or Sample Browser Visual Studio extension. They give you the flexibility to search samples, download samples on demand, manage the downloaded samples in a centralized place, and automatically be notified about sample updates. If it is the first time that you hear about Microsoft All-In-One Code Framework, please watch the introduction video on Microsoft Showcase, or read the introduction on our homepage https://1code.codeplex.com/.

Introduction

The Visual Studio status bar, the horizontal region at the bottom of the Visual Studio design surface, provides a convenient way to convey information about the current state of the integrated development environment (IDE). The status bar comprises four programmable regions, as shown in the following table:
 
Feedback: Displays text. You can set and retrieve text, display static text, and highlight the displayed text.
 
Progress Bar: Displays incremental progress for quick operations, such as saving a single file to disk.
 
Animation: Displays a continuously looped animation, which indicates either a lengthy operation or an operation of indeterminate length (for example, building multiple projects in a solution).

Designer: Displays information pertinent to editing, such as the line number or column number of the cursor location.
 
In this sample, we will demo:

  1. Write highlighted text in feedback region
  2. Read text from feedback region
  3. Show progress bar in status bar
  4. Show animation in the status bar
  5. Write row, column and char to designer region

 

Building the Sample

VS 2010 SP1 SDK must be installed on the machine. You can download it from:

Visual Studio 2010 SP1 SDK

Set the Start Action and Start Options of Debug.

Start Action: C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\devenv.exe (32bit OS) orC:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\devenv.exe (64bit OS)

Start Option: /rootsuffixExp

image

 

Running the Sample

1. Press F5 to debug this project.
 
2. In the new instance of Visual Studio, click View=>Other Windows => Status Bar Demo, you will see the Tool Window.

image

3. Click the Write Feedback in Status Bar button on the Tool Window, you will see following message in the status bar.

image

4. Click the Read Feedback in Status Bar button on the Tool Window, you will see following message box.

image

5. Click the Show Progress Bar button on the Tool Window, you will see following progress bar in the status bar.

image

6. Click the Show Animation in Status Bar button on the Tool Window, you will see following animation in the status bar.

imageimage

image

7. Click the Update Designer Region button on the Tool Window, you will see following message in the status bar.

image

 

Using the Code

1. Create a VS Package Project.

Use Visual Studio Integration Package Wizard to create a simple VSPackage project. You can follow the steps in Walkthrough: Creating a Tool Window

2. Modify the Command Table (.vsct file).

 <!-- Add a button to  View=>Other Window--> 
<Button guid="guidVBVSPackageStatusBarCmdSet" id="cmdidStatusBarDemo" priority="0x0100" type="Button"> 
    <Parent guid="guidSHLMainMenu" id="IDG_VS_WNDO_OTRWNDWS1"/> 
    <Icon guid="guidImages" id="bmpPic2" /> 
    <Strings> 
        <CommandName>cmdidStatusBarDemo</CommandName> 
        <ButtonText>Status Bar Demo</ButtonText> 
    </Strings> 
</Button> 

3. Handle the cmdidStatusBarDemo command in Package.

 ''' <summary> 
''' Initialization of the package; this method is called right after the package is sited, so this is the place 
''' where you can put all the initilaization code that rely on services provided by VisualStudio. 
''' </summary> 
Protected Overrides Sub Initialize() 
    Trace.WriteLine(String.Format(CultureInfo.CurrentCulture, "Entering Initialize() of: {0}", Me.GetType().Name)) 
    MyBase.Initialize() 
 
 
    ' Add our command handlers for menu (commands must exist in the .vsct file) 
    Dim mcs As OleMenuCommandService = TryCast(GetService(GetType(IMenuCommandService)), OleMenuCommandService) 
    If Not mcs Is Nothing Then 
        ' Create the command for the tool window 
        Dim toolwndCommandID As New CommandID(GuidList.guidVBVSPackageStatusBarCmdSet, CInt(PkgCmdIDList.cmdidStatusBarDemo)) 
        Dim menuToolWin As New MenuCommand(New EventHandler(AddressOf ShowToolWindow), toolwndCommandID) 
        mcs.AddCommand(menuToolWin) 
    End If 
End Sub 

4. Open the Tool Window

 ''' <summary> 
''' This function is called when the user clicks the menu item that shows the  
''' tool window. See the Initialize method to see how the menu item is associated to  
''' this function using the OleMenuCommandService service and the MenuCommand class. 
''' </summary> 
Private Sub ShowToolWindow(ByVal sender As Object, ByVal e As EventArgs) 
    ' Get the instance number 0 of this tool window. This window is single instance so this instance 
    ' is actually the only one. 
    ' The last flag is set to true so that if the tool window does not exists it will be created. 
    Dim window As ToolWindowPane = Me.FindToolWindow(GetType(MyToolWindow), 0, True) 
    If (window Is Nothing) Or (window.Frame Is Nothing) Then 
        Throw New NotSupportedException(Resources.CanNotCreateWindow) 
    End If 
 
 
    Dim windowFrame As IVsWindowFrame = TryCast(window.Frame, IVsWindowFrame) 
    Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(windowFrame.Show()) 
End Sub 

5. Set the content of the Tool Window and set its content.

 Public Class MyToolWindow 
    Inherits ToolWindowPane 
 
 
    ''' <summary> 
    ''' Standard constructor for the tool window. 
    ''' </summary> 
    Public Sub New() 
        MyBase.New(Nothing) 
        ' Set the window title reading it from the resources. 
        Me.Caption = Resources.ToolWindowTitle 
        ' Set the image that will appear on the tab of the window frame 
        ' when docked with an other window 
        ' The resource ID correspond to the one defined in the resx file 
        ' while the Index is the offset in the bitmap strip. Each image in 
        ' the strip being 16x16. 
        Me.BitmapResourceID = 301 
        Me.BitmapIndex = 1 
 
 
        'This is the user control hosted by the tool window; Note that, even if this class implements IDisposable, 
        'we are not calling Dispose on this object. This is because ToolWindowPane calls Dispose on  
        'the object returned by the Content property. 
        Me.Content = New MyControl() 
    End Sub 
 
 
    ''' <summary> 
    ''' Initialize the SvcStatusBar property of the control. 
    ''' </summary> 
    Public Overrides Sub OnToolWindowCreated() 
        MyBase.OnToolWindowCreated() 
        TryCast(MyBase.Content, MyControl).SvcStatusBar = TryCast(GetService(GetType(SVsStatusbar)), IVsStatusbar) 
    End Sub 
 
 
End Class 

6. Write the Feedback region of the Visual Studio Status bar

  • Obtain an instance of the IVsStatusbar interface, which is made available through the SVsStatusbar service.
  • SetColorText only displays white text on a dark blue background.
  • SetText to set text in the feedback region
 Private Sub btnWriteFeedback_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles btnWriteFeedback.Click 
    ' Checks to see if the status bar is frozen  
    ' by calling the IsFrozen method. 
    Dim frozen As Integer 
    SvcStatusBar.IsFrozen(frozen) 
    If frozen = 0 Then 
        ' SetColorText only displays white text on a  
        ' dark blue background. 
        SvcStatusBar.SetColorText("Here's some highlighted text", 0, 0) 
        MessageBox.Show("Pause") 
        SvcStatusBar.SetText("Done") 
    End If 
End Sub 

7. Read the Feedback region of the Visual Studio Status bar

  • Obtain an instance of the IVsStatusbar interface, which is made available through the SVsStatusbar service.
  • GetText to get the text from the feedback region
 Private Sub btnReadFeedback_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles btnReadFeedback.Click 
    ' Retrieve the status bar text. 
    Dim text As String 
    SvcStatusBar.GetText(text) 
    MessageBox.Show(text) 
 
 
    ' Clear the status bar text. 
    SvcStatusBar.FreezeOutput(0) 
    SvcStatusBar.Clear() 
End Sub 

8. Show progress bar in the status bar

  • Obtains an instance of the IVsStatusbar interface from the SVsStatusbar service.
  • Initializes the progress bar to given starting values by calling the Progress method.
  • Simulates an operation by iterating through a for loop and updating the progress bar values using the Progress method.
  • Clears the progress bar using the Clear method.
 Private Sub btnShowProgressBar_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles btnShowProgressBar.Click 
    Dim cookie As UInteger = 0 
    Dim label As String = "Progress bar label..." 
 
 
    ' Initialize the progress bar. 
    SvcStatusBar.Progress(cookie, 1, "", 0, 0) 
 
 
    Dim i As UInteger = 0 
    Dim total As UInteger = 100 
    Do While i <= total 
        ' Display incremental progress. 
        SvcStatusBar.Progress(cookie, 1, label, i, total) 
        System.Threading.Thread.Sleep(10) 
        i += 1 
    Loop 
 
 
    ' Clear the progress bar. 
    SvcStatusBar.Progress(cookie, 0, "", 0, 0) 
End Sub 

9. Use the animation region of status bar

  • Obtain an instance of the IVsStatusbar interface, which is made available through the SVsStatusbar service.
  • Start the animation by calling the Animation method of the status bar. Pass in 1 as the value of the first parameter, and a reference to an animated icon as the value of the second parameter.
  • Stop the animation by calling the Animation method of the status bar. Pass in 0 as the value of the first parameter, and a reference to the animated icon as the value of the second parameter.
 Private Sub btnShowAnimation_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles btnShowAnimation.Click 
    Dim icon As Object = CShort(Microsoft.VisualStudio.Shell.Interop.Constants.SBAI_Deploy) 
 
 
    ' Display the animated Visual Studio icon in the Animation region. 
    ' Start the animation by calling the Animation method of the status bar.  
    ' Pass in 1 as the value of the first parameter, and a reference to an  
    ' animated icon as the value of the second parameter. 
    SvcStatusBar.Animation(1, icon) 
 
 
    MessageBox.Show("Click OK to end status bar animation.") 
 
 
    ' Stop the animation by calling the Animation method of the status bar.  
    ' Pass in 0 as the value of the first parameter, and a reference to the  
    ' animated icon as the value of the second parameter. 
    SvcStatusBar.Animation(0, icon) 
End Sub 
 

10. Program the designer region of the status bar

  • Obtain an instance of the IVsStatusbar interface, which is made available through the SVsStatusbar service.
  • Update the Designer region of the status bar by calling the SetInsMode and SetLineColChar methods of the IVsStatusbar instance.
 Private Sub btnUpdateDesignerRegion_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles btnUpdateDesignerRegion.Click 
    ' Set insert/overstrike mode. 
    Dim mode As Object = 1 ' Insert mode 
    SvcStatusBar.SetInsMode(mode) 
 
 
    ' Display Ln ## Col ## Ch ## information. 
    Dim ln As Object = "##", col As Object = "##", ch As Object = "##" 
    SvcStatusBar.SetLineColChar(ln, col, ch) 
End Sub 

 

More Information

Walkthrough: Creating a Tool Window

Visual Studio Command Table (.Vsct) Files

MSDN: Status Bar