HOW TO:使用功能表合併和 ToolStrip 控制項建立 MDI 表單
System.Windows.Forms 命名空間支援多重文件介面 (MDI) 應用程式,而 MenuStrip 控制項則支援功能表合併。 MDI 表單也可以使用 ToolStrip 控制項建立。
在 Visual Studio 中對此功能有相當廣泛的支援。
如需詳細資訊,請參閱逐步解說:使用功能表合併和 ToolStrip 控制項建立 MDI 表單 和 逐步解說:使用功能表合併和 ToolStrip 控制項建立 MDI 表單 和 逐步解說:使用功能表合併和 ToolStrip 控制項建立 MDI 表單 和 逐步解說:使用功能表合併和 ToolStrip 控制項建立 MDI 表單.
範例
下列程式碼範例會示範如何在 MDI 表單上使用 ToolStripPanel 控制項。 這個表單也支援與子功能表的功能表合併。
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Drawing
Imports System.Windows.Forms
' This code example demonstrates an MDI form
' that supports menu merging and moveable
' ToolStrip controls
Public Class Form1
Inherits Form
Private menuStrip1 As MenuStrip
Private toolStripMenuItem1 As ToolStripMenuItem
Private WithEvents newToolStripMenuItem As ToolStripMenuItem
Private toolStripPanel1 As ToolStripPanel
Private toolStrip1 As ToolStrip
Private toolStripPanel2 As ToolStripPanel
Private toolStrip2 As ToolStrip
Private toolStripPanel3 As ToolStripPanel
Private toolStrip3 As ToolStrip
Private toolStripPanel4 As ToolStripPanel
Private toolStrip4 As ToolStrip
Private components As System.ComponentModel.IContainer = Nothing
Public Sub New()
InitializeComponent()
End Sub
Protected Overrides Sub Dispose(disposing As Boolean)
If disposing AndAlso (components IsNot Nothing) Then
components.Dispose()
End If
MyBase.Dispose(disposing)
End Sub
' This method creates a new ChildForm instance
' and attaches it to the MDI parent form.
Private Sub newToolStripMenuItem_Click( _
ByVal sender As Object, _
ByVal e As EventArgs) _
Handles newToolStripMenuItem.Click
Dim f As New ChildForm()
f.MdiParent = Me
f.Text = "Form - " + Me.MdiChildren.Length.ToString()
f.Show()
End Sub
#Region "Windows Form Designer generated code"
Private Sub InitializeComponent()
Me.menuStrip1 = New System.Windows.Forms.MenuStrip
Me.toolStripMenuItem1 = New System.Windows.Forms.ToolStripMenuItem
Me.newToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem
Me.toolStripPanel1 = New System.Windows.Forms.ToolStripPanel
Me.toolStrip1 = New System.Windows.Forms.ToolStrip
Me.toolStripPanel2 = New System.Windows.Forms.ToolStripPanel
Me.toolStrip2 = New System.Windows.Forms.ToolStrip
Me.toolStripPanel3 = New System.Windows.Forms.ToolStripPanel
Me.toolStrip3 = New System.Windows.Forms.ToolStrip
Me.toolStripPanel4 = New System.Windows.Forms.ToolStripPanel
Me.toolStrip4 = New System.Windows.Forms.ToolStrip
Me.menuStrip1.SuspendLayout()
Me.toolStripPanel1.SuspendLayout()
Me.toolStripPanel2.SuspendLayout()
Me.toolStripPanel3.SuspendLayout()
Me.toolStripPanel4.SuspendLayout()
Me.SuspendLayout()
'
'menuStrip1
'
Me.menuStrip1.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Me.toolStripMenuItem1})
Me.menuStrip1.Location = New System.Drawing.Point(0, 0)
Me.menuStrip1.MdiWindowListItem = Me.toolStripMenuItem1
Me.menuStrip1.Name = "menuStrip1"
Me.menuStrip1.Size = New System.Drawing.Size(292, 24)
Me.menuStrip1.TabIndex = 0
Me.menuStrip1.Text = "menuStrip1"
'
'toolStripMenuItem1
'
Me.toolStripMenuItem1.DropDownItems.AddRange(New System.Windows.Forms.ToolStripItem() {Me.newToolStripMenuItem})
Me.toolStripMenuItem1.Name = "toolStripMenuItem1"
Me.toolStripMenuItem1.Size = New System.Drawing.Size(57, 20)
Me.toolStripMenuItem1.Text = "Window"
'
'newToolStripMenuItem
'
Me.newToolStripMenuItem.Name = "newToolStripMenuItem"
Me.newToolStripMenuItem.Size = New System.Drawing.Size(106, 22)
Me.newToolStripMenuItem.Text = "New"
'
'toolStripPanel1
'
Me.toolStripPanel1.Controls.Add(Me.toolStrip1)
Me.toolStripPanel1.Dock = System.Windows.Forms.DockStyle.Left
Me.toolStripPanel1.Location = New System.Drawing.Point(0, 49)
Me.toolStripPanel1.Name = "toolStripPanel1"
Me.toolStripPanel1.Orientation = System.Windows.Forms.Orientation.Vertical
Me.toolStripPanel1.RowMargin = New System.Windows.Forms.Padding(0, 3, 0, 0)
Me.toolStripPanel1.Size = New System.Drawing.Size(26, 199)
'
'toolStrip1
'
Me.toolStrip1.Dock = System.Windows.Forms.DockStyle.None
Me.toolStrip1.Location = New System.Drawing.Point(0, 3)
Me.toolStrip1.Name = "toolStrip1"
Me.toolStrip1.Size = New System.Drawing.Size(26, 109)
Me.toolStrip1.TabIndex = 0
'
'toolStripPanel2
'
Me.toolStripPanel2.Controls.Add(Me.toolStrip2)
Me.toolStripPanel2.Dock = System.Windows.Forms.DockStyle.Top
Me.toolStripPanel2.Location = New System.Drawing.Point(0, 24)
Me.toolStripPanel2.Name = "toolStripPanel2"
Me.toolStripPanel2.Orientation = System.Windows.Forms.Orientation.Horizontal
Me.toolStripPanel2.RowMargin = New System.Windows.Forms.Padding(3, 0, 0, 0)
Me.toolStripPanel2.Size = New System.Drawing.Size(292, 25)
'
'toolStrip2
'
Me.toolStrip2.Dock = System.Windows.Forms.DockStyle.None
Me.toolStrip2.Location = New System.Drawing.Point(3, 0)
Me.toolStrip2.Name = "toolStrip2"
Me.toolStrip2.Size = New System.Drawing.Size(109, 25)
Me.toolStrip2.TabIndex = 0
'
'toolStripPanel3
'
Me.toolStripPanel3.Controls.Add(Me.toolStrip3)
Me.toolStripPanel3.Dock = System.Windows.Forms.DockStyle.Right
Me.toolStripPanel3.Location = New System.Drawing.Point(266, 49)
Me.toolStripPanel3.Name = "toolStripPanel3"
Me.toolStripPanel3.Orientation = System.Windows.Forms.Orientation.Vertical
Me.toolStripPanel3.RowMargin = New System.Windows.Forms.Padding(0, 3, 0, 0)
Me.toolStripPanel3.Size = New System.Drawing.Size(26, 199)
'
'toolStrip3
'
Me.toolStrip3.Dock = System.Windows.Forms.DockStyle.None
Me.toolStrip3.Location = New System.Drawing.Point(0, 3)
Me.toolStrip3.Name = "toolStrip3"
Me.toolStrip3.Size = New System.Drawing.Size(26, 109)
Me.toolStrip3.TabIndex = 0
'
'toolStripPanel4
'
Me.toolStripPanel4.Controls.Add(Me.toolStrip4)
Me.toolStripPanel4.Dock = System.Windows.Forms.DockStyle.Bottom
Me.toolStripPanel4.Location = New System.Drawing.Point(0, 248)
Me.toolStripPanel4.Name = "toolStripPanel4"
Me.toolStripPanel4.Orientation = System.Windows.Forms.Orientation.Horizontal
Me.toolStripPanel4.RowMargin = New System.Windows.Forms.Padding(3, 0, 0, 0)
Me.toolStripPanel4.Size = New System.Drawing.Size(292, 25)
'
'toolStrip4
'
Me.toolStrip4.Dock = System.Windows.Forms.DockStyle.None
Me.toolStrip4.Location = New System.Drawing.Point(3, 0)
Me.toolStrip4.Name = "toolStrip4"
Me.toolStrip4.Size = New System.Drawing.Size(109, 25)
Me.toolStrip4.TabIndex = 0
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.Add(Me.toolStripPanel3)
Me.Controls.Add(Me.toolStripPanel1)
Me.Controls.Add(Me.toolStripPanel2)
Me.Controls.Add(Me.menuStrip1)
Me.Controls.Add(Me.toolStripPanel4)
Me.IsMdiContainer = True
Me.MainMenuStrip = Me.menuStrip1
Me.Name = "Form1"
Me.Text = "Form1"
Me.menuStrip1.ResumeLayout(False)
Me.menuStrip1.PerformLayout()
Me.toolStripPanel1.ResumeLayout(False)
Me.toolStripPanel1.PerformLayout()
Me.toolStripPanel2.ResumeLayout(False)
Me.toolStripPanel2.PerformLayout()
Me.toolStripPanel3.ResumeLayout(False)
Me.toolStripPanel3.PerformLayout()
Me.toolStripPanel4.ResumeLayout(False)
Me.toolStripPanel4.PerformLayout()
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
#End Region
End Class
Public Class ChildForm
Inherits Form
Private menuStrip1 As System.Windows.Forms.MenuStrip
Private toolStripMenuItem1 As System.Windows.Forms.ToolStripMenuItem
Private components As System.ComponentModel.IContainer = Nothing
Public Sub New()
InitializeComponent()
End Sub
Protected Overrides Sub Dispose(disposing As Boolean)
If disposing AndAlso (components IsNot Nothing) Then
components.Dispose()
End If
MyBase.Dispose(disposing)
End Sub
#Region "Windows Form Designer generated code"
Private Sub InitializeComponent()
Me.menuStrip1 = New System.Windows.Forms.MenuStrip()
Me.toolStripMenuItem1 = New System.Windows.Forms.ToolStripMenuItem()
Me.menuStrip1.SuspendLayout()
Me.SuspendLayout()
'
' menuStrip1
'
Me.menuStrip1.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Me.toolStripMenuItem1})
Me.menuStrip1.Location = New System.Drawing.Point(0, 0)
Me.menuStrip1.Name = "menuStrip1"
Me.menuStrip1.Size = New System.Drawing.Size(292, 24)
Me.menuStrip1.TabIndex = 0
Me.menuStrip1.Text = "menuStrip1"
'
' toolStripMenuItem1
'
Me.toolStripMenuItem1.Name = "toolStripMenuItem1"
Me.toolStripMenuItem1.Size = New System.Drawing.Size(90, 20)
Me.toolStripMenuItem1.Text = "ChildMenuItem"
'
' ChildForm
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6F, 13F)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.Add(menuStrip1)
Me.MainMenuStrip = Me.menuStrip1
Me.Name = "ChildForm"
Me.Text = "ChildForm"
Me.menuStrip1.ResumeLayout(False)
Me.menuStrip1.PerformLayout()
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
#End Region
End Class
Public Class Program
' <summary>
' The main entry point for the application.
' </summary>
<STAThread()> _
Shared Sub Main()
Application.EnableVisualStyles()
Application.Run(New Form1())
End Sub
End Class
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace MdiFormCS
{
// This code example demonstrates an MDI form
// that supports menu merging and moveable
// ToolStrip controls
public class Form1 : Form
{
private MenuStrip menuStrip1;
private ToolStripMenuItem toolStripMenuItem1;
private ToolStripMenuItem newToolStripMenuItem;
private ToolStripPanel toolStripPanel1;
private ToolStrip toolStrip1;
private ToolStripPanel toolStripPanel2;
private ToolStrip toolStrip2;
private ToolStripPanel toolStripPanel3;
private ToolStrip toolStrip3;
private ToolStripPanel toolStripPanel4;
private ToolStrip toolStrip4;
private System.ComponentModel.IContainer components = null;
public Form1()
{
InitializeComponent();
}
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
// This method creates a new ChildForm instance
// and attaches it to the MDI parent form.
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
ChildForm f = new ChildForm();
f.MdiParent = this;
f.Text = "Form - " + this.MdiChildren.Length.ToString();
f.Show();
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.menuStrip1 = new System.Windows.Forms.MenuStrip();
this.toolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
this.newToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.toolStripPanel1 = new System.Windows.Forms.ToolStripPanel();
this.toolStrip1 = new System.Windows.Forms.ToolStrip();
this.toolStripPanel2 = new System.Windows.Forms.ToolStripPanel();
this.toolStrip2 = new System.Windows.Forms.ToolStrip();
this.toolStripPanel3 = new System.Windows.Forms.ToolStripPanel();
this.toolStrip3 = new System.Windows.Forms.ToolStrip();
this.toolStripPanel4 = new System.Windows.Forms.ToolStripPanel();
this.toolStrip4 = new System.Windows.Forms.ToolStrip();
this.menuStrip1.SuspendLayout();
this.toolStripPanel1.SuspendLayout();
this.toolStripPanel2.SuspendLayout();
this.toolStripPanel3.SuspendLayout();
this.toolStripPanel4.SuspendLayout();
this.SuspendLayout();
//
// menuStrip1
//
this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.toolStripMenuItem1});
this.menuStrip1.Location = new System.Drawing.Point(0, 0);
this.menuStrip1.MdiWindowListItem = this.toolStripMenuItem1;
this.menuStrip1.Name = "menuStrip1";
this.menuStrip1.Size = new System.Drawing.Size(292, 24);
this.menuStrip1.TabIndex = 0;
this.menuStrip1.Text = "menuStrip1";
//
// toolStripMenuItem1
//
this.toolStripMenuItem1.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.newToolStripMenuItem});
this.toolStripMenuItem1.Name = "toolStripMenuItem1";
this.toolStripMenuItem1.Size = new System.Drawing.Size(57, 20);
this.toolStripMenuItem1.Text = "Window";
//
// newToolStripMenuItem
//
this.newToolStripMenuItem.Name = "newToolStripMenuItem";
this.newToolStripMenuItem.Size = new System.Drawing.Size(106, 22);
this.newToolStripMenuItem.Text = "New";
this.newToolStripMenuItem.Click += new System.EventHandler(this.newToolStripMenuItem_Click);
//
// toolStripPanel1
//
this.toolStripPanel1.Controls.Add(this.toolStrip1);
this.toolStripPanel1.Dock = System.Windows.Forms.DockStyle.Left;
this.toolStripPanel1.Location = new System.Drawing.Point(0, 49);
this.toolStripPanel1.Name = "toolStripPanel1";
this.toolStripPanel1.Orientation = System.Windows.Forms.Orientation.Vertical;
this.toolStripPanel1.RowMargin = new System.Windows.Forms.Padding(0, 3, 0, 0);
this.toolStripPanel1.Size = new System.Drawing.Size(26, 199);
//
// toolStrip1
//
this.toolStrip1.Dock = System.Windows.Forms.DockStyle.None;
this.toolStrip1.Location = new System.Drawing.Point(0, 3);
this.toolStrip1.Name = "toolStrip1";
this.toolStrip1.Size = new System.Drawing.Size(26, 109);
this.toolStrip1.TabIndex = 0;
//
// toolStripPanel2
//
this.toolStripPanel2.Controls.Add(this.toolStrip2);
this.toolStripPanel2.Dock = System.Windows.Forms.DockStyle.Top;
this.toolStripPanel2.Location = new System.Drawing.Point(0, 24);
this.toolStripPanel2.Name = "toolStripPanel2";
this.toolStripPanel2.Orientation = System.Windows.Forms.Orientation.Horizontal;
this.toolStripPanel2.RowMargin = new System.Windows.Forms.Padding(3, 0, 0, 0);
this.toolStripPanel2.Size = new System.Drawing.Size(292, 25);
//
// toolStrip2
//
this.toolStrip2.Dock = System.Windows.Forms.DockStyle.None;
this.toolStrip2.Location = new System.Drawing.Point(3, 0);
this.toolStrip2.Name = "toolStrip2";
this.toolStrip2.Size = new System.Drawing.Size(109, 25);
this.toolStrip2.TabIndex = 0;
//
// toolStripPanel3
//
this.toolStripPanel3.Controls.Add(this.toolStrip3);
this.toolStripPanel3.Dock = System.Windows.Forms.DockStyle.Right;
this.toolStripPanel3.Location = new System.Drawing.Point(266, 49);
this.toolStripPanel3.Name = "toolStripPanel3";
this.toolStripPanel3.Orientation = System.Windows.Forms.Orientation.Vertical;
this.toolStripPanel3.RowMargin = new System.Windows.Forms.Padding(0, 3, 0, 0);
this.toolStripPanel3.Size = new System.Drawing.Size(26, 199);
//
// toolStrip3
//
this.toolStrip3.Dock = System.Windows.Forms.DockStyle.None;
this.toolStrip3.Location = new System.Drawing.Point(0, 3);
this.toolStrip3.Name = "toolStrip3";
this.toolStrip3.Size = new System.Drawing.Size(26, 109);
this.toolStrip3.TabIndex = 0;
//
// toolStripPanel4
//
this.toolStripPanel4.Controls.Add(this.toolStrip4);
this.toolStripPanel4.Dock = System.Windows.Forms.DockStyle.Bottom;
this.toolStripPanel4.Location = new System.Drawing.Point(0, 248);
this.toolStripPanel4.Name = "toolStripPanel4";
this.toolStripPanel4.Orientation = System.Windows.Forms.Orientation.Horizontal;
this.toolStripPanel4.RowMargin = new System.Windows.Forms.Padding(3, 0, 0, 0);
this.toolStripPanel4.Size = new System.Drawing.Size(292, 25);
//
// toolStrip4
//
this.toolStrip4.Dock = System.Windows.Forms.DockStyle.None;
this.toolStrip4.Location = new System.Drawing.Point(3, 0);
this.toolStrip4.Name = "toolStrip4";
this.toolStrip4.Size = new System.Drawing.Size(109, 25);
this.toolStrip4.TabIndex = 0;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.toolStripPanel3);
this.Controls.Add(this.toolStripPanel1);
this.Controls.Add(this.toolStripPanel2);
this.Controls.Add(this.menuStrip1);
this.Controls.Add(this.toolStripPanel4);
this.IsMdiContainer = true;
this.MainMenuStrip = this.menuStrip1;
this.Name = "Form1";
this.Text = "Form1";
this.menuStrip1.ResumeLayout(false);
this.menuStrip1.PerformLayout();
this.toolStripPanel1.ResumeLayout(false);
this.toolStripPanel1.PerformLayout();
this.toolStripPanel2.ResumeLayout(false);
this.toolStripPanel2.PerformLayout();
this.toolStripPanel3.ResumeLayout(false);
this.toolStripPanel3.PerformLayout();
this.toolStripPanel4.ResumeLayout(false);
this.toolStripPanel4.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
}
public class ChildForm : Form
{
private System.Windows.Forms.MenuStrip menuStrip1;
private System.Windows.Forms.ToolStripMenuItem toolStripMenuItem1;
private System.ComponentModel.IContainer components = null;
public ChildForm()
{
InitializeComponent();
}
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.menuStrip1 = new System.Windows.Forms.MenuStrip();
this.toolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
this.menuStrip1.SuspendLayout();
this.SuspendLayout();
//
// menuStrip1
//
this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.toolStripMenuItem1});
this.menuStrip1.Location = new System.Drawing.Point(0, 0);
this.menuStrip1.Name = "menuStrip1";
this.menuStrip1.Size = new System.Drawing.Size(292, 24);
this.menuStrip1.TabIndex = 0;
this.menuStrip1.Text = "menuStrip1";
//
// toolStripMenuItem1
//
this.toolStripMenuItem1.Name = "toolStripMenuItem1";
this.toolStripMenuItem1.Size = new System.Drawing.Size(90, 20);
this.toolStripMenuItem1.Text = "ChildMenuItem";
//
// ChildForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.menuStrip1);
this.MainMenuStrip = this.menuStrip1;
this.Name = "ChildForm";
this.Text = "ChildForm";
this.menuStrip1.ResumeLayout(false);
this.menuStrip1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
}
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Form1());
}
}
}
編譯程式碼
此程式碼範例需要:
- System.Drawing 和 System.Windows.Forms 組件的參考。
如需從 Visual Basic 或 Visual C# 的命令列建置這個範例的詳細資訊,請參閱從命令列建置 (Visual Basic) 或使用 csc.exe 建置命令列。 您也可以透過將程式碼貼入新的專案,在 Visual Studio 中建置此範例。 如需詳細資訊,請參閱HOW TO:使用 Visual Studio 編譯及執行完整的 Windows Form 程式碼範例 和 HOW TO:使用 Visual Studio 編譯及執行完整的 Windows Form 程式碼範例 和 HOW TO:使用 Visual Studio 編譯及執行完整的 Windows Form 程式碼範例 和 HOW TO:使用 Visual Studio 編譯及執行完整的 Windows Form 程式碼範例 和 如何:使用 Visual Studio 編譯及執行完整的 Windows Form 程式碼範例.