当前位置:网站首页>C#应用程序界面开发基础——窗体控制(2)——MDI窗体
C#应用程序界面开发基础——窗体控制(2)——MDI窗体
2022-07-03 01:03:00 【DXB2021】
MDI窗体
单文档界面(SDI)
多文档界面(MDI)
MDI窗体的概念
MDI窗体(Multiple-Document Interface,多文档界面)用于同时显示多个文档。
在项目中使用MDI窗体时,通常将一个MDI窗口窗体作为父窗体,父窗体可以将多个子窗体包容在它的工作区之中。
设置MDI窗体
1、MDI容器窗体
将某个窗体设置为窗口窗体,有两种方法:
1、在窗体的“属性”面板中,将IsMidContainer属性设置为True即可。

False:

True:

2、在窗体的Load事件中加入以下语句

代码如下:
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.IsMdiContainer = true;
}
}
}
2、设置MDI子窗体
MDI子窗体就是一般的窗体。只需要将某个窗体实例的MdiParent属性设置到一个MDI父窗体,它就是那位父窗体的子窗体,语法格式如下:
窗体实例名.MdiParent=父窗体对象;
设置MDI容器:

修改后:

从Form5属性里,通过Name属性来进行修改为“MainForm”

修改后:

Text属性表示窗体标题,修改为“MDI窗体”

修改后:

新建子窗体(Form1.cs、Form2.cs、Form3.cs、Form4.cs):

代码内容:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Form5
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e)
{
Form1 form1 = new Form1();
form1.Show();
form1.MdiParent = this;
Form2 form2 = new Form2();
form2.Show();
form2.MdiParent = this;
Form3 form3 = new Form3();
form3.Show();
form3.MdiParent = this;
Form4 form4 = new Form4();
form4.Show();
form4.MdiParent = this;
}
}
}完成后:

运行结果如下:

排列MDI窗体
语法格式如下:
public void LayoutMdi(MdiLayout value)
value是MdiLayout的枚举值之一,用来定义MDI子窗体的布局。
| 枚举成员 | 说明 |
| Cascade | 层叠排列MDI子窗体 |
| TileHorizontal | 水平平铺MDI子窗体 |
| TileVertical | 垂直平铺MDI子窗体 |
新建一个Windows窗体应用程序,在Name属性中将窗体唯一标识符命名为MainForm,并且在Text属性中,将主窗体的标题命名为“MDI窗体”。
在菜单栏中选择“视图”命令 ,再选择“工具箱”命令,就会弹出工具箱。

在工具箱中选择MenuStrip控件,此时就会在窗体中出现一排菜单栏,并依次输入“新建窗体”、“层叠排列”、“水平平铺”、“垂直平铺”、“关闭”。



接着,再将IsMdiContainer属性设置为True。
再创建一个子窗体,并命名为“ChildForm”,下面就可以通过双击菜单项,进行添加事件。

代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Form5
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e)
{
Form1 form1 = new Form1();
form1.Show();
form1.MdiParent = this;
Form2 form2 = new Form2();
form2.Show();
form2.MdiParent = this;
Form3 form3 = new Form3();
form3.Show();
form3.MdiParent = this;
Form4 form4 = new Form4();
form4.Show();
form4.MdiParent = this;
}
private void 新建窗体ToolStripMenuItem_Click(object sender, EventArgs e)
{
ChildForm mychildForm=new ChildForm();
mychildForm.Show();
mychildForm.MdiParent = this;
}
private void 层叠排列ToolStripMenuItem_Click(object sender, EventArgs e)
{
LayoutMdi(MdiLayout.Cascade);
}
private void 水平平铺ToolStripMenuItem_Click(object sender, EventArgs e)
{
LayoutMdi(MdiLayout.TileHorizontal);
}
private void 垂直平铺ToolStripMenuItem_Click(object sender, EventArgs e)
{
LayoutMdi(MdiLayout.TileVertical);
}
private void 关闭ToolStripMenuItem_Click(object sender, EventArgs e)
{
Close();
}
}
}
运行结果如下:

点击新建窗体:


层叠排列:

水平平铺:

垂直平铺:

边栏推荐
- Button wizard play strange learning - automatic return to the city route judgment
- 【第29天】给定一个整数,请你求出它的因子数
- Detailed explanation of Q-learning examples of reinforcement learning
- MySQL --- 数据库查询 - 基本查询
- 不登陆或者登录解决oracle数据库账号被锁定。
- 1696C. Fishingprince Plays With Array【思维题 + 中间状态 + 优化存储】
- R language uses coin package to apply permutation tests to independence problems (permutation tests, whether response variables are independent of groups, are two numerical variables independent, and
- [flutter] icons component (fluttericon Download Icon | customize SVG icon to generate TTF font file | use the downloaded TTF icon file)
- The meaning of wildcard, patsubst and notdir in makefile
- Cut point of undirected graph
猜你喜欢

Excel if formula determines whether the two columns are the same

【FPGA教程案例6】基于vivado核的双口RAM设计与实现

强化学习 Q-learning 实例详解

信息熵的基础

异步、邮件、定时三大任务

Give you an array numbers that may have duplicate element values. It was originally an array arranged in ascending order, and it was rotated once according to the above situation. Please return the sm

给你一个可能存在 重复 元素值的数组 numbers ,它原来是一个升序排列的数组,并按上述情形进行了一次旋转。请返回旋转数组的最小元素。【剑指Offer】

JDBC courses

用Go+绘制爱心给心爱的她表白
![[Androd] Gradle 使用技巧之模块依赖替换](/img/5f/968db696932f155a8c4a45f67135ac.png)
[Androd] Gradle 使用技巧之模块依赖替换
随机推荐
Correctly distinguish the similarities and differences among API, rest API, restful API and web service
Database SQL language 01 where condition
d. LDC build shared library
R language ggplot2 visualization: use ggplot2 to display dataframe data that are all classified variables in the form of thermal diagram, and customize the legend color legend of factor
Niu Ke swipes questions and clocks in
Basic remote connection tool xshell
Concise analysis of redis source code 11 - Main IO threads and redis 6.0 multi IO threads
1696C. Fishingprince plays with array [thinking questions + intermediate state + optimized storage]
电话网络问题
leetcode:871. 最低加油次数【以前pat做过 + 最大堆 +贪心】
Mongodb common commands of mongodb series
Kivy tutorial - example of using Matplotlib in Kivy app
Draw love with go+ to express love to her beloved
Makefile中wildcard、patsubst、notdir的含义
Thinkphp+redis realizes simple lottery
On Fibonacci sequence
测试右移:线上质量监控 ELK 实战
Find a benchmark comrade in arms | a million level real-time data platform, which can be used for free for life
matlab查找某一行或者某一列在矩阵中的位置
LDC Build Shared Library