可以使用 IDE 底部的 Visual Studio 状态栏显示信息。
扩展状态栏时,可以在四个区域中显示信息和 UI:反馈区域、进度栏、动画区域和设计器区域。 反馈区域允许您显示文本并对其进行高亮显示。 进度栏显示短运行作(例如保存文件)的增量进度。 动画区域用于显示持续循环的动画,适用于长时间运行或长度不确定的操作,例如在解决方案中生成多个项目。 设计器区域显示游标位置的行号和列号。
可以使用IVsStatusbar接口(从SVsStatusbar服务)获取状态栏。 此外,在窗口框架上定位的任何对象都可以通过实现 IVsStatusbarUser 接口注册为状态栏客户端对象。 每当激活窗口时,Visual Studio 将查询该窗口上对象的IVsStatusbarUser接口。 如果找到,它会在返回的接口上调用 SetInfo 该方法,对象可以从该方法内部更新状态栏。 例如,当文档窗口变为活动窗口时,可以使用SetInfo方法更新设计器区域中的信息。
以下过程假定你了解如何创建 VSIX 项目并添加自定义菜单命令。 有关信息,请参阅 使用菜单命令创建扩展。
修改状态栏
此过程演示如何设置和获取文本、显示静态文本,并在状态栏的反馈区域中突出显示显示的文本。
读取和写入状态栏
创建名为 TestStatusBarExtension 的 VSIX 项目,并添加名为 TestStatusBarCommand 的菜单命令。
在 TestStatusBarCommand.cs中,将命令处理程序方法代码 (
MenuItemCallback) 替换为以下内容:private void MenuItemCallback(object sender, EventArgs e) { IVsStatusbar statusBar = (IVsStatusbar)ServiceProvider.GetService(typeof(SVsStatusbar)); // Make sure the status bar is not frozen int frozen; statusBar.IsFrozen(out frozen); if (frozen != 0) { statusBar.FreezeOutput(0); } // Set the status bar text and make its display static. statusBar.SetText("We just wrote to the status bar."); // Freeze the status bar. statusBar.FreezeOutput(1); // Get the status bar text. string text; statusBar.GetText(out text); System.Windows.Forms.MessageBox.Show(text); // Clear the status bar text. statusBar.FreezeOutput(0); statusBar.Clear(); }编译代码并开始调试。
在 Visual Studio 的实验实例中打开 “工具” 菜单。 选择 “调用 TestStatusBarCommand ”按钮。
状态栏中的文本现在显示 我们刚刚写入状态栏。 出现的消息框中使用了相同的文本。
更新进度栏
以下过程演示如何初始化和更新进度栏。
打开 TestStatusBarCommand.cs 文件,并将
MenuItemCallback方法替换为以下代码:private void MenuItemCallback(object sender, EventArgs e) { IVsStatusbar statusBar = (IVsStatusbar)ServiceProvider.GetService(typeof(SVsStatusbar)); uint cookie = 0; string label = "Writing to the progress bar"; // Initialize the progress bar. statusBar.Progress(ref cookie, 1, "", 0, 0); for (uint i = 0, total = 20; i <= total; i++) { // Display progress every second. statusBar.Progress(ref cookie, 1, label, i, total); System.Threading.Thread.Sleep(1000); } // Clear the progress bar. statusBar.Progress(ref cookie, 0, "", 0, 0); }编译代码并开始调试。
在 Visual Studio 的实验实例中打开 “工具” 菜单。 选择 “调用 TestStatusBarCommand ”按钮。
状态栏中的文本现在显示 “写入进度栏”。 进度栏每隔 20 秒更新一次,并清除状态和进度条。
显示动画
状态栏显示一个循环动画,指示长时间运行的操作(例如:在一个解决方案中编译多个项目)。 如果未看到此动画,请确保具有正确的 “工具>选项” 设置:
打开 “工具>选项 ”窗格,然后展开“ 所有设置>环境>视觉体验>效果 ”部分。
在“启用丰富的客户端视觉体验”下,选择“自动”(仅建议启用)。
打开 “工具>选项 ”对话框并展开“ 环境>常规 ”部分。
取消选中“基于客户端性能自动调整视觉体验”复选框。
选中“ 启用丰富的客户端视觉体验 ”子选项复选框。
选择“确定”。
在 Visual Studio 的实验实例中生成项目时,现在会显示动画。
此过程演示如何显示标准 Visual Studio 动画,该动画表示生成项目或解决方案。
打开 TestStatusBarCommand.cs 文件,并将
MenuItemCallback方法替换为以下代码:private void MenuItemCallback(object sender, EventArgs e) { IVsStatusbar statusBar =(IVsStatusbar)ServiceProvider.GetService(typeof(SVsStatusbar)); // Use the standard Visual Studio icon for building. object icon = (short)Microsoft.VisualStudio.Shell.Interop.Constants.SBAI_Build; // Display the icon in the Animation region. statusBar.Animation(1, ref icon); // The message box pauses execution for you to look at the animation. System.Windows.Forms.MessageBox.Show("showing?"); // Stop the animation. statusBar.Animation(0, ref icon); }编译代码并开始调试。
在 Visual Studio 的实验实例中打开 “工具” 菜单,然后选择 “调用 TestStatusBarCommand ”按钮。
看到消息框时,还应在最右侧的状态栏中看到动画。 关闭消息框时,动画将消失。