赞
踩
目录
1、首先在Form1.cs的Form1_Load()中增加代码段
2、其次还要修改Form1.Designer.cs的窗体设计生成器
VS中选择C#的Windows窗体应用创建父窗体Form1,默认生成Program.cs、Form1.cs、Form1.Designer.cs三个C#文件,右键Form1.cs有两种显示模式,一种为查看代码Form1.cs,一种为查看设计器Form1.cs[设计]。查看设计器Form1.cs[设计],右键属性,把属性IsMdiContainer的值修改为True。
在资源管理器中,右键项目(比如App1),添加窗体(Windows窗体),生成窗体Form2。
以此类推可以生成窗体Form3、Form4。
在父窗体Form1中实现加载Form1时自动加载子窗体Form2,需要在Form1.cs的Form1_Load()中增加代码段:
- Form2 frm2 = new()
- {
- MdiParent = this
- };
- frm2.Show();
以此类推,可以自动加载子窗体Form3、Form4。
- //在主窗体中创建子窗体,并在主窗体打开时自动加载子窗体
- namespace App1
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
-
- private void Form1_Load(object sender, EventArgs e)
- {
- Form2 frm2 = new()//自动加载Form2
- {
- MdiParent = this
- };
- frm2.Show();
-
- Form3 frm3 = new()//自动加载Form3
- {
- MdiParent = this
- };
- frm3.Show();
-
- Form4 frm4 = new()//自动加载Form4
- {
- MdiParent = this
- };
- frm4.Show();
- }
- }
- }
- #region Windows Form Designer generated code
-
- /// <summary>
- /// Required method for Designer support - do not modify
- /// the contents of this method with the code editor.
- /// </summary>
- private void InitializeComponent()
- {
- SuspendLayout();
- //
- // Form1
- //
- AutoScaleDimensions = new SizeF(7F, 17F);
- AutoScaleMode = AutoScaleMode.Font;
- ClientSize = new Size(421, 393);
- IsMdiContainer = true;
- Name = "Form1";
- Text = "Form1";
- ResumeLayout(false);
- }
-
- #endregion
- }
- }
- #region Windows Form Designer generated code
-
- /// <summary>
- /// Required method for Designer support - do not modify
- /// the contents of this method with the code editor.
- /// </summary>
- private void InitializeComponent()
- {
- SuspendLayout();
- //
- // Form1
- //
- AutoScaleDimensions = new SizeF(7F, 17F);
- AutoScaleMode = AutoScaleMode.Font;
- ClientSize = new Size(421, 393);
- IsMdiContainer = true;
- Name = "Form1";
- Text = "Form1";
- Load += Form1_Load; //在父窗体中显示子窗体,增加
- ResumeLayout(false);
- }
- #endregion
- 语法如下:
- public void LayoutMdi(MdiLayout value)
- value:是MdiLayout枚举值之一,用来定义MDI子窗体的布局。
- MdiLayout枚举用于指定MDI父窗体中子窗体的布局,其格式:
- public enum MdiLayout
- MdiLayout的枚举成员如下表:
枚举成员 | 说明 |
Cascade | 所有子窗体均层叠在MDI父窗体工作区内 |
TileHorizontal | 所有子窗体均水平平铺在MDI父窗体工作区内 |
TileVertical | 所有子窗体均垂直平铺在MDI父窗体工作区内 |
在上述的实例中继续操作,在Form1窗体中,添加一个MenuStip控件用作该父窗体的菜单项。通过MenuStrip控件建立3个菜单项,分别为“水平平铺”、“垂直平铺”、“层叠排列”。运行程序时通过Form1_Load加载所有子窗体,通过“水平平铺”、“垂直平铺”、“层叠排列”对所有子窗体进行排列。
- #region Windows Form Designer generated code
-
- /// <summary>
- /// Required method for Designer support - do not modify
- /// the contents of this method with the code editor.
- /// </summary>
- private void InitializeComponent()
- {
- menuStrip1 = new MenuStrip();
- 水平平铺ToolStripMenuItem = new ToolStripMenuItem();
- 垂直平铺ToolStripMenuItem = new ToolStripMenuItem();
- 层叠排列ToolStripMenuItem = new ToolStripMenuItem();
- menuStrip1.SuspendLayout();
- SuspendLayout();
- //
- // menuStrip1
- //
- menuStrip1.Items.AddRange(new ToolStripItem[] { 水平平铺ToolStripMenuItem, 垂直平铺ToolStripMenuItem, 层叠排列ToolStripMenuItem });
- menuStrip1.Location = new Point(0, 0);
- menuStrip1.Name = "menuStrip1";
- menuStrip1.Size = new Size(484, 25);
- menuStrip1.TabIndex = 1;
- menuStrip1.Text = "menuStrip1";
- //
- // 水平平铺ToolStripMenuItem
- //
- 水平平铺ToolStripMenuItem.Name = "水平平铺ToolStripMenuItem";
- 水平平铺ToolStripMenuItem.Size = new Size(68, 21);
- 水平平铺ToolStripMenuItem.Text = "水平平铺";
- //
- // 垂直平铺ToolStripMenuItem
- //
- 垂直平铺ToolStripMenuItem.Name = "垂直平铺ToolStripMenuItem";
- 垂直平铺ToolStripMenuItem.Size = new Size(68, 21);
- 垂直平铺ToolStripMenuItem.Text = "垂直平铺";
- //
- // 层叠排列ToolStripMenuItem
- //
- 层叠排列ToolStripMenuItem.Name = "层叠排列ToolStripMenuItem";
- 层叠排列ToolStripMenuItem.Size = new Size(68, 21);
- 层叠排列ToolStripMenuItem.Text = "层叠排列";
- //
- // Form1
- //
- AutoScaleDimensions = new SizeF(7F, 17F);
- AutoScaleMode = AutoScaleMode.Font;
- ClientSize = new Size(484, 461);
- Controls.Add(menuStrip1);
- IsMdiContainer = true;
- MainMenuStrip = menuStrip1;
- Name = "Form1";
- SizeGripStyle = SizeGripStyle.Hide;
- Text = "Form1";
- Load += Form1_Load;
- menuStrip1.ResumeLayout(false);
- menuStrip1.PerformLayout();
- ResumeLayout(false);
- PerformLayout();
- }
-
- #endregion
-
- private MenuStrip menuStrip1;
- private ToolStripMenuItem 水平平铺ToolStripMenuItem;
- private ToolStripMenuItem 垂直平铺ToolStripMenuItem;
- private ToolStripMenuItem 层叠排列ToolStripMenuItem;
- }
- }
观察窗体设计生成器自动新增内容和Form1.cs自动新增的内容。
- //Form1.cs
- namespace App3
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- //在Form1_Load()中显示所有子窗体
- private void Form1_Load(object sender, EventArgs e)
- {
-
- Form2 frm2 = new()//自动加载Form2
- {
- MdiParent = this
- };
- frm2.Show();
- Form3 frm3 = new()//自动加载Form3
- {
- MdiParent = this
- };
- frm3.Show();
- Form4 frm4 = new()//自动加载Form4
- {
- MdiParent = this
- };
- frm4.Show();
- }
- //激活菜单项“水平平铺”的Click事件
- private void 水平平铺ToolStripMenuItem_Click(object sender, EventArgs e)
- {
-
- }
- }
- }
- //Form1.Designer.cs
- namespace App3
- {
- partial class Form1
- {
- /// <summary>
- /// Required designer variable.
- /// </summary>
- private System.ComponentModel.IContainer components = null;
-
- /// <summary>
- /// Clean up any resources being used.
- /// </summary>
- /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
- protected override void Dispose(bool disposing)
- {
- if (disposing && (components != null))
- {
- components.Dispose();
- }
- base.Dispose(disposing);
- }
-
- #region Windows Form Designer generated code
-
- /// <summary>
- /// Required method for Designer support - do not modify
- /// the contents of this method with the code editor.
- /// </summary>
- private void InitializeComponent()
- {
- menuStrip1 = new MenuStrip();
- 水平平铺ToolStripMenuItem = new ToolStripMenuItem();
- 垂直平铺ToolStripMenuItem = new ToolStripMenuItem();
- 层叠排列ToolStripMenuItem = new ToolStripMenuItem();
- menuStrip1.SuspendLayout();
- SuspendLayout();
- //
- // menuStrip1
- //
- menuStrip1.Items.AddRange(new ToolStripItem[] { 水平平铺ToolStripMenuItem, 垂直平铺ToolStripMenuItem, 层叠排列ToolStripMenuItem });
- menuStrip1.Location = new Point(0, 0);
- menuStrip1.Name = "menuStrip1";
- menuStrip1.Size = new Size(484, 25);
- menuStrip1.TabIndex = 1;
- menuStrip1.Text = "menuStrip1";
- //
- // 水平平铺ToolStripMenuItem
- //
- 水平平铺ToolStripMenuItem.Name = "水平平铺ToolStripMenuItem";
- 水平平铺ToolStripMenuItem.Size = new Size(68, 21);
- 水平平铺ToolStripMenuItem.Text = "水平平铺";
- 水平平铺ToolStripMenuItem.Click += 水平平铺ToolStripMenuItem_Click;
- //
- // 垂直平铺ToolStripMenuItem
- //
- 垂直平铺ToolStripMenuItem.Name = "垂直平铺ToolStripMenuItem";
- 垂直平铺ToolStripMenuItem.Size = new Size(68, 21);
- 垂直平铺ToolStripMenuItem.Text = "垂直平铺";
- //
- // 层叠排列ToolStripMenuItem
- //
- 层叠排列ToolStripMenuItem.Name = "层叠排列ToolStripMenuItem";
- 层叠排列ToolStripMenuItem.Size = new Size(68, 21);
- 层叠排列ToolStripMenuItem.Text = "层叠排列";
- //
- // Form1
- //
- AutoScaleDimensions = new SizeF(7F, 17F);
- AutoScaleMode = AutoScaleMode.Font;
- ClientSize = new Size(484, 461);
- Controls.Add(menuStrip1);
- IsMdiContainer = true;
- MainMenuStrip = menuStrip1;
- Name = "Form1";
- SizeGripStyle = SizeGripStyle.Hide;
- Text = "Form1";
- Load += Form1_Load;
- menuStrip1.ResumeLayout(false);
- menuStrip1.PerformLayout();
- ResumeLayout(false);
- PerformLayout();
- }
-
- #endregion
-
- private MenuStrip menuStrip1;
- private ToolStripMenuItem 水平平铺ToolStripMenuItem;
- private ToolStripMenuItem 垂直平铺ToolStripMenuItem;
- private ToolStripMenuItem 层叠排列ToolStripMenuItem;
- }
- }
继续观察窗体设计生成器自动新增内容和Form1.cs自动新增的内容。
- //Form1.cs
- namespace App3
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- //在Form1_Load()中显示所有子窗体
- private void Form1_Load(object sender, EventArgs e)
- {
-
- Form2 frm2 = new()//自动加载Form2
- {
- MdiParent = this
- };
- frm2.Show();
- Form3 frm3 = new()//自动加载Form3
- {
- MdiParent = this
- };
- frm3.Show();
- Form4 frm4 = new()//自动加载Form4
- {
- MdiParent = this
- };
- frm4.Show();
- }
- //激活菜单项“水平平铺”的Click事件
- private void 水平平铺ToolStripMenuItem_Click(object sender, EventArgs e)
- {
-
- }
- //激活菜单项“层叠排列”的Click事件
- private void 层叠排列ToolStripMenuItem_Click(object sender, EventArgs e)
- {
-
- }
- //激活菜单项“垂直平铺”的Click事件
- private void 垂直平铺ToolStripMenuItem_Click(object sender, EventArgs e)
- {
-
- }
- }
- }
- //Form1.Designer.cs
- namespace App3
- {
- partial class Form1
- {
- /// <summary>
- /// Required designer variable.
- /// </summary>
- private System.ComponentModel.IContainer components = null;
-
- /// <summary>
- /// Clean up any resources being used.
- /// </summary>
- /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
- protected override void Dispose(bool disposing)
- {
- if (disposing && (components != null))
- {
- components.Dispose();
- }
- base.Dispose(disposing);
- }
-
- #region Windows Form Designer generated code
-
- /// <summary>
- /// Required method for Designer support - do not modify
- /// the contents of this method with the code editor.
- /// </summary>
- private void InitializeComponent()
- {
- menuStrip1 = new MenuStrip();
- 水平平铺ToolStripMenuItem = new ToolStripMenuItem();
- 垂直平铺ToolStripMenuItem = new ToolStripMenuItem();
- 层叠排列ToolStripMenuItem = new ToolStripMenuItem();
- menuStrip1.SuspendLayout();
- SuspendLayout();
- //
- // menuStrip1
- //
- menuStrip1.Items.AddRange(new ToolStripItem[] { 水平平铺ToolStripMenuItem, 垂直平铺ToolStripMenuItem, 层叠排列ToolStripMenuItem });
- menuStrip1.Location = new Point(0, 0);
- menuStrip1.Name = "menuStrip1";
- menuStrip1.Size = new Size(484, 25);
- menuStrip1.TabIndex = 1;
- menuStrip1.Text = "menuStrip1";
- //
- // 水平平铺ToolStripMenuItem
- //
- 水平平铺ToolStripMenuItem.Name = "水平平铺ToolStripMenuItem";
- 水平平铺ToolStripMenuItem.Size = new Size(68, 21);
- 水平平铺ToolStripMenuItem.Text = "水平平铺";
- 水平平铺ToolStripMenuItem.Click += 水平平铺ToolStripMenuItem_Click; //激活点击事件
- //
- // 垂直平铺ToolStripMenuItem
- //
- 垂直平铺ToolStripMenuItem.Name = "垂直平铺ToolStripMenuItem";
- 垂直平铺ToolStripMenuItem.Size = new Size(68, 21);
- 垂直平铺ToolStripMenuItem.Text = "垂直平铺";
- 垂直平铺ToolStripMenuItem.Click += 垂直平铺ToolStripMenuItem_Click; //激活点击事件
- //
- // 层叠排列ToolStripMenuItem
- //
- 层叠排列ToolStripMenuItem.Name = "层叠排列ToolStripMenuItem";
- 层叠排列ToolStripMenuItem.Size = new Size(68, 21);
- 层叠排列ToolStripMenuItem.Text = "层叠排列";
- 层叠排列ToolStripMenuItem.Click += 层叠排列ToolStripMenuItem_Click; //激活点击事件
- //
- // Form1
- //
- AutoScaleDimensions = new SizeF(7F, 17F);
- AutoScaleMode = AutoScaleMode.Font;
- ClientSize = new Size(484, 461);
- Controls.Add(menuStrip1);
- IsMdiContainer = true;
- MainMenuStrip = menuStrip1;
- Name = "Form1";
- SizeGripStyle = SizeGripStyle.Hide;
- Text = "Form1";
- Load += Form1_Load;
- menuStrip1.ResumeLayout(false);
- menuStrip1.PerformLayout();
- ResumeLayout(false);
- PerformLayout();
- }
-
- #endregion
-
- private MenuStrip menuStrip1;
- private ToolStripMenuItem 水平平铺ToolStripMenuItem;
- private ToolStripMenuItem 垂直平铺ToolStripMenuItem;
- private ToolStripMenuItem 层叠排列ToolStripMenuItem;
- }
- }
在Form1.cs中添加事件属性,从而完成点击控件后执行的操作。
- //Form1.cs
- namespace App3
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- //在Form1_Load()中显示所有子窗体
- private void Form1_Load(object sender, EventArgs e)
- {
-
- Form2 frm2 = new()//自动加载Form2
- {
- MdiParent = this
- };
- frm2.Show();
- Form3 frm3 = new()//自动加载Form3
- {
- MdiParent = this
- };
- frm3.Show();
- Form4 frm4 = new()//自动加载Form4
- {
- MdiParent = this
- };
- frm4.Show();
- }
- //激活菜单项“水平平铺”的Click事件
- private void 水平平铺ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- LayoutMdi(MdiLayout.TileHorizontal); //添加点击事件的属性
- }
- //激活菜单项“层叠排列”的Click事件
- private void 层叠排列ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- LayoutMdi(MdiLayout.Cascade); //添加点击事件的属性
- }
- //激活菜单项“垂直平铺”的Click事件
- private void 垂直平铺ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- LayoutMdi(MdiLayout.TileVertical); //添加点击事件的属性
- }
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。