菜单栏、工具栏与状态栏
开发专题 · 传统 WinForms 与 Krypton 现代化两套做法
本页在同一专题内给出两套可运行路径:案例 A 用传统 Form + MenuStrip / ToolStrip / StatusStrip;案例 B 用现代化 KryptonForm + KryptonToolStrip / KryptonStatusStrip(菜单按 LCode 工具箱实际项说明)。面向 .NET Framework 4.8;凡有代码均同时给出 C# 5.0 与 VB.NET。设计器操作以中文界面为准,工具箱显示名见下文对照表。
概述:传统 vs 现代化(Krypton)
菜单栏放命令入口,工具栏放高频快捷按钮,状态栏显示就绪/进度等信息。LCode 工具箱(SharpDevelopControlLibrary.sdcl)对两类控件均有收录:
| 角色 | 传统控件(WinForms) | 工具箱显示名 | 现代化控件(Krypton) | 工具箱显示名 |
|---|---|---|---|---|
| 窗体基类 | Form |
(模板默认) | KryptonForm |
窗体 |
| 菜单栏 | MenuStrip |
菜单条 | 无独立 KryptonMenuStrip |
— |
| 工具栏 | ToolStrip |
工具条 | KryptonToolStrip |
工具条 |
| 状态栏 | StatusStrip |
状态条 | KryptonStatusStrip |
状态条 |
| 右键菜单 | ContextMenuStrip |
上下文菜单条 | KryptonContextMenu |
上下文菜单 |
| 功能区(可选) | — | — | KryptonRibbon |
Ribbon |
选型建议:要经典「文件 / 编辑」顶栏菜单,用案例 A,或在
KryptonForm 上继续放传统 MenuStrip;要跟主题的工具栏/状态栏,用案例 B 的 KryptonToolStrip / KryptonStatusStrip;要 Office 风格功能区,用 KryptonRibbon(须放在 KryptonForm 上)。右键菜单现代化请用 KryptonContextMenu,它不能替代顶栏 MenuStrip。
案例 A · 传统控件
目标:在默认 Form 上组合菜单栏、工具栏与状态栏,并实现「文件 → 退出」与状态文字刷新。假设已按 窗体应用入门 建好「Windows 应用程序」项目,主窗体为 MainForm。
设计器步骤
- 在项目浏览器中双击
MainForm,底部切到设计页签。 - 打开工具箱,在「Windows Forms」相关分类中找到并拖放到设计面:
- 菜单条(
MenuStrip)— 通常停靠顶部;拖入后窗体MainMenuStrip会指向它。 - 工具条(
ToolStrip)— 停靠在菜单下方(Dock = Top)。 - 状态条(
StatusStrip)— 停靠底部(Dock = Bottom)。
- 菜单条(
- 在菜单条上点「在此处键入」,建顶级项「文件(&F)」;再在其下添加「退出(&X)」。属性面板把退出项
Name设为exitToolStripMenuItem。 - 选中工具条,用项集合编辑器(或智能标记)添加
ToolStripButton,Name设为btnExit,Text设为「退出」,按需设DisplayStyle(仅文本 / 图像与文本)。 - 选中状态条,添加
ToolStripStatusLabel,Name设为statusLabel,Text设为「就绪」,Spring = True以便占满剩余宽度。 - 属性面板切到事件:为
exitToolStripMenuItem与btnExit的「单击」各生成处理程序(也可共用一个方法)。
提示:优先在设计器完成布局与命名,业务逻辑写在窗体主文件;不要手改
InitializeComponent。拖拽与源码/设计切换见 文件与编辑器。
关键代码(C# / VB.NET)
下列示例假设设计器已生成 menuStrip1、toolStrip1、statusStrip1、exitToolStripMenuItem、btnExit、statusLabel。退出与工具栏按钮可绑到同一处理程序:
C#
using System;
using System.Windows.Forms;
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
// 菜单与工具栏共用退出逻辑
exitToolStripMenuItem.Click += Exit_Click;
btnExit.Click += Exit_Click;
statusLabel.Text = "就绪";
}
private void Exit_Click(object sender, EventArgs e)
{
statusLabel.Text = "正在退出…";
Close();
}
}
VB.NET
Imports System
Imports System.Windows.Forms
Public Class MainForm
Inherits Form
Public Sub New()
InitializeComponent()
AddHandler exitToolStripMenuItem.Click, AddressOf Exit_Click
AddHandler btnExit.Click, AddressOf Exit_Click
statusLabel.Text = "就绪"
End Sub
Private Sub Exit_Click(sender As Object, e As EventArgs)
statusLabel.Text = "正在退出…"
Close()
End Sub
End Class
若希望完全用代码搭建三栏(便于对照 Designer 结果),也可在构造函数中动态创建:
C#
using System;
using System.Windows.Forms;
public class MainForm : Form
{
private MenuStrip menuStrip;
private ToolStrip toolStrip;
private StatusStrip statusStrip;
private ToolStripStatusLabel statusLabel;
public MainForm()
{
Text = "传统菜单 / 工具栏 / 状态栏";
Width = 640;
Height = 400;
menuStrip = new MenuStrip();
ToolStripMenuItem fileMenu = new ToolStripMenuItem("文件(&F)");
ToolStripMenuItem exitItem = new ToolStripMenuItem("退出(&X)");
exitItem.Click += Exit_Click;
fileMenu.DropDownItems.Add(exitItem);
menuStrip.Items.Add(fileMenu);
MainMenuStrip = menuStrip;
toolStrip = new ToolStrip();
ToolStripButton btnExit = new ToolStripButton("退出");
btnExit.Click += Exit_Click;
toolStrip.Items.Add(btnExit);
statusStrip = new StatusStrip();
statusLabel = new ToolStripStatusLabel("就绪");
statusLabel.Spring = true;
statusStrip.Items.Add(statusLabel);
Controls.Add(statusStrip);
Controls.Add(toolStrip);
Controls.Add(menuStrip);
}
private void Exit_Click(object sender, EventArgs e)
{
statusLabel.Text = "正在退出…";
Close();
}
}
VB.NET
Imports System
Imports System.Windows.Forms
Public Class MainForm
Inherits Form
Private menuStrip As MenuStrip
Private toolStrip As ToolStrip
Private statusStrip As StatusStrip
Private statusLabel As ToolStripStatusLabel
Public Sub New()
Text = "传统菜单 / 工具栏 / 状态栏"
Width = 640
Height = 400
menuStrip = New MenuStrip()
Dim fileMenu As New ToolStripMenuItem("文件(&F)")
Dim exitItem As New ToolStripMenuItem("退出(&X)")
AddHandler exitItem.Click, AddressOf Exit_Click
fileMenu.DropDownItems.Add(exitItem)
menuStrip.Items.Add(fileMenu)
MainMenuStrip = menuStrip
toolStrip = New ToolStrip()
Dim btnExit As New ToolStripButton("退出")
AddHandler btnExit.Click, AddressOf Exit_Click
toolStrip.Items.Add(btnExit)
statusStrip = New StatusStrip()
statusLabel = New ToolStripStatusLabel("就绪")
statusLabel.Spring = True
statusStrip.Items.Add(statusLabel)
Controls.Add(statusStrip)
Controls.Add(toolStrip)
Controls.Add(menuStrip)
End Sub
Private Sub Exit_Click(sender As Object, e As EventArgs)
statusLabel.Text = "正在退出…"
Close()
End Sub
End Class
案例 B · 现代化控件(Krypton)
目标:在 KryptonForm 上使用跟随主题的工具栏与状态栏。LCode 工具箱没有 KryptonMenuStrip;顶栏菜单常用做法是:继续放传统 MenuStrip,或改用 KryptonRibbon 功能区。右键菜单用 KryptonContextMenu。
设计器步骤
- 新建或打开窗体后,将基类改为
KryptonForm(需引用Krypton.Toolkit)。也可通过工程 → 添加窗口创建已继承KryptonForm的窗体。详见 窗体应用入门「现代化窗口」。 - 打开设计面,从工具箱「LCode Components」拖入:
- 工具条(
KryptonToolStrip)—Dock = Top。 - 状态条(
KryptonStatusStrip)—Dock = Bottom。
- 工具条(
- (菜单栏等价)任选其一:
- 再拖传统菜单条(
MenuStrip)到同一KryptonForm,建「文件 → 退出」——外观仍是系统菜单,但可与 Krypton 工具栏/状态栏混用。 - 或拖 Ribbon(
KryptonRibbon)到KryptonForm(不可放在普通Form上),用功能区承载命令。Ribbon 分组与按钮细节见控件手册。
- 再拖传统菜单条(
- 在
KryptonToolStrip上添加ToolStripButton(子项类型与传统工具栏相同),Name如btnExit。 - 在
KryptonStatusStrip上添加ToolStripStatusLabel,Name如statusLabel。 - 需要右键菜单时,拖上下文菜单(
KryptonContextMenu)到组件托盘,配置项后赋给控件的KryptonContextMenu属性(不要当成顶栏菜单用)。
关键代码(KryptonForm + 工具栏/状态栏 + MenuStrip)
C#
using System;
using System.Windows.Forms;
using Krypton.Toolkit;
public class MainForm : KryptonForm
{
private MenuStrip menuStrip;
private KryptonToolStrip toolStrip;
private KryptonStatusStrip statusStrip;
private ToolStripStatusLabel statusLabel;
public MainForm()
{
Text = "现代化工具栏 / 状态栏";
Width = 640;
Height = 400;
menuStrip = new MenuStrip();
ToolStripMenuItem fileMenu = new ToolStripMenuItem("文件(&F)");
ToolStripMenuItem exitItem = new ToolStripMenuItem("退出(&X)");
exitItem.Click += Exit_Click;
fileMenu.DropDownItems.Add(exitItem);
menuStrip.Items.Add(fileMenu);
MainMenuStrip = menuStrip;
toolStrip = new KryptonToolStrip();
toolStrip.Dock = DockStyle.Top;
toolStrip.GripStyle = ToolStripGripStyle.Hidden;
ToolStripButton btnExit = new ToolStripButton("退出");
btnExit.Click += Exit_Click;
toolStrip.Items.Add(btnExit);
statusStrip = new KryptonStatusStrip();
statusStrip.Dock = DockStyle.Bottom;
statusLabel = new ToolStripStatusLabel("就绪");
statusLabel.Spring = true;
statusStrip.Items.Add(statusLabel);
Controls.Add(statusStrip);
Controls.Add(toolStrip);
Controls.Add(menuStrip);
}
private void Exit_Click(object sender, EventArgs e)
{
statusLabel.Text = "正在退出…";
Close();
}
}
VB.NET
Imports System
Imports System.Windows.Forms
Imports Krypton.Toolkit
Public Class MainForm
Inherits KryptonForm
Private menuStrip As MenuStrip
Private toolStrip As KryptonToolStrip
Private statusStrip As KryptonStatusStrip
Private statusLabel As ToolStripStatusLabel
Public Sub New()
Text = "现代化工具栏 / 状态栏"
Width = 640
Height = 400
menuStrip = New MenuStrip()
Dim fileMenu As New ToolStripMenuItem("文件(&F)")
Dim exitItem As New ToolStripMenuItem("退出(&X)")
AddHandler exitItem.Click, AddressOf Exit_Click
fileMenu.DropDownItems.Add(exitItem)
menuStrip.Items.Add(fileMenu)
MainMenuStrip = menuStrip
toolStrip = New KryptonToolStrip()
toolStrip.Dock = DockStyle.Top
toolStrip.GripStyle = ToolStripGripStyle.Hidden
Dim btnExit As New ToolStripButton("退出")
AddHandler btnExit.Click, AddressOf Exit_Click
toolStrip.Items.Add(btnExit)
statusStrip = New KryptonStatusStrip()
statusStrip.Dock = DockStyle.Bottom
statusLabel = New ToolStripStatusLabel("就绪")
statusLabel.Spring = True
statusStrip.Items.Add(statusLabel)
Controls.Add(statusStrip)
Controls.Add(toolStrip)
Controls.Add(menuStrip)
End Sub
Private Sub Exit_Click(sender As Object, e As EventArgs)
statusLabel.Text = "正在退出…"
Close()
End Sub
End Class
扩展阅读:KryptonForm、KryptonToolStrip、KryptonStatusStrip、KryptonContextMenu、KryptonRibbon。属性大表以控件手册为准,本页只保留专题要点。
属性 / 事件要点
| 对象 | 属性 / 事件 | 说明 |
|---|---|---|
Form / KryptonForm |
MainMenuStrip |
指定主菜单条;有 MenuStrip 时应赋值,否则 Alt 快捷键等行为可能异常 |
MenuStrip / ToolStrip / StatusStrip |
Dock |
菜单/工具栏多为 Top,状态栏为 Bottom |
ToolStrip / KryptonToolStrip |
Items |
按钮、分隔线、下拉、组合框等子项集合 |
ToolStrip / KryptonToolStrip |
GripStyle |
Hidden 可隐藏左侧拖动手柄 |
ToolStripItem |
Click |
菜单项与工具栏按钮的常用单击事件;可共用同一处理程序 |
ToolStripStatusLabel |
Text / Spring |
状态文案;Spring = true 时标签伸展占满剩余空间 |
ToolStripMenuItem |
ShortcutKeys |
快捷键(如 Ctrl+S);需与菜单文本中的 & 加速键区分开 |
KryptonContextMenu |
Items |
右键菜单项集合;赋给控件的 KryptonContextMenu 属性后弹出 |
KryptonRibbon |
(分组 / 按钮) | 仅可放在 KryptonForm;无独立 RibbonForm 类 |
常见问题与「暂不支持」项
- 工具箱里没有 Krypton 菜单条?正确。当前收录的是
KryptonToolStrip、KryptonStatusStrip、KryptonContextMenu与KryptonRibbon,没有KryptonMenuStrip。顶栏菜单请用传统MenuStrip,或改用 Ribbon。 - 把 KryptonRibbon 拖到普通 Form 上?设计器会拦截:Ribbon 需要
KryptonForm。请先改基类或「添加窗口」创建 Krypton 窗体。 - KryptonForm 不能当子控件拖进别的窗体。它是顶级窗体;通过添加窗口创建,不要从工具箱往已有设计面里「嵌一个窗体」。
- 菜单与工具栏命令如何统一启用/禁用?可手写同步
Enabled;或查阅控件手册中的KryptonCommand绑定。复杂命令路由框架暂不支持,待更新完善。 - 可视化菜单设计器的高级就地编辑选项。若某环境关闭了 ToolStrip 就地编辑,仍可用属性面板的 Items 集合编辑器;专项增强暂不支持,待更新完善。
- 断点调试菜单单击路径。当前运行器以运行 +
Console.WriteLine为主;断点单步暂不支持,待更新完善。见 运行与编译。
相关阅读
- C# / VB.NET 窗体应用入门 — 新建窗体、传统 Form 与 KryptonForm
- 文件与编辑器 — 设计器拖拽、属性与事件
- 运行与编译 — 运行验证与日志
- KryptonForm、KryptonToolStrip、KryptonStatusStrip、KryptonContextMenu
- KryptonRibbon — 功能区菜单等价物
- MDI 窗口模式 — 多文档与菜单合并相关场景