当前位置:网站首页>C#应用程序界面开发基础——窗体控制
C#应用程序界面开发基础——窗体控制
2022-06-28 19:29:00 【DXB2021】
From窗体
创建WinForm程序
创建WinForm程序的步骤
打开Visual Studio,点击“创建新项目”,使用快捷键Ctrl+Shift+N可以快速地打开“新建项目”对话框。

在拉下菜单中选择“C#”、“Windows”、“桌面”中,在列表框选择“Windows窗体应用(.NET Framework)”。

点击“下一步”。
点击“创建”。

在解决方案资源管理器,双击Program.cs文件,会跳转到Windows控制台应用界面,在“编辑”窗口中是一段自动生成的WinForm程序.

代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
internal static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}再单击“启动”按键,会弹出一个空白的窗体。

创建窗体的实质
窗体的实质就是利用System.Windows.Forms.Form类或者是该类的派生类来创建。
右击“窗体”后,选择“查看代码”命令,就会跳转到Form1.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 WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
}
窗体的添加与删除
右击项目名称WindowsFormsApp1,在弹出的快捷菜单中选择“添加”-“Windows窗体”或者“添加”-“新建项”命令。

最后在“添加新项”圣诞框中选择“窗体(Windows窗体)”。

删除窗体,只需要在解决方案管理器中,选中要删除的窗体名称,右击,在弹出的快捷菜单选择“删除”命令即可。

窗体的属性
1、“属性”面板
打开“属性”面板有以下三种方法:
(1)
右键命令

(2)
“视图”命令

(3)
右下角

2、C# WinForm窗体基础属性
(1)窗口样式中的属性值,可以用来更改图标、最大化窗口透明度。
| 属性值 | 说明 |
| Icon | 更改图标样式(左上角图标) |
| MaximizeBox:true; | 显示右上角最大化按钮 |
| MinimizeBox:true; | 显示右上角最小化按钮 |
| ShowInco:true; | 显示左上角小图标 |
| ShowInTaskbar:ture; | 窗体显示在任务栏 |
| TopMost:ture; | 窗口置顶显示 |
| Opacity:100% | 整个窗口透明度 |
(2)布局中的属性值,可以用来改变窗体的大小以及启动程序后窗体的显示位置。
| 属性值 | 说明 |
| AutoScroll:true/false; | 如果控件超出窗口是否自动显示滚动条 |
| AutoSize:true/false; | 窗口的范围是否会超出控件的大小 |
| MaximumSize:0,0; | 窗口可以拖曳的最大的大小 |
| MinimumSize:0,0; | 窗口可以拖曳的最小的大小 |
| Size:300,300; | 窗口打开时默认的大小 |
| StartPosition:centerScreen; | 窗口打开时默认桌面位置,居中 |
| WindowState:Maximized; | 默认打开窗口最大化。 |
(3)外观的属性值,主要用来更改窗体颜色、字体和标题等。
| 属性值 | 说明 |
| Font:宋体,9pt; | 可以修改字体大小,字体越大控件越大 |
| Text; | 输入文本 |
| TextAlign; | 文字位置 |
| FormBorderStyle:FixedSingle; | 窗口不可拖曳大小 |
| FormBorderStyle:None; | 隐藏窗口的边框 |
| DropDownStyle:DropDownList; | 让下拉框无法输入文本 |
3、设置窗体属性
窗体的图标是系统 默认的图标。

更改图标,在“属性”面板中,选择Icon性格

窗体的颜色 和背景,通过BackgroundImage属性进行设置。选择“属性”面板中的BackgroundImage属性。

窗体的常用事件
所谓事件,就是指要发生的事情,可以简单地理解为用户的操作,它是由对象引发的。窗体的所有事件,都可以在“属性”面板中进行查看。
1、添加事件
为窗体添加一件事件,只要在事件面板里选择要添加的事件,Load后面的空格里双击,相应的事件将会自动生成。
(1)窗体在加载时,就会触发一个窗体加载事件Load。


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 WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
MessageBox.Show("窗体加载完成");
}
}
}
(2)单击窗体时,触发Click(单击)事件。

Click双击。

代码如下:
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 WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
MessageBox.Show("窗体加载完成");
}
private void Form1_Click(object sender, EventArgs e)
{
MessageBox.Show("单击窗体");
}
}
}

(3)关闭窗体时,触发FormClosing(关闭)事件

代码如下:
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 WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
MessageBox.Show("窗体加载完成");
}
private void Form1_Click(object sender, EventArgs e)
{
MessageBox.Show("单击窗体");
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
MessageBox.Show("确定要关闭窗体?");
}
}
}

2、删除事件
没看懂, 不掌握。
窗体的显示与隐藏
窗体标识符.Show()
窗体标识符.Hide()
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();
}
}
}
运行结果如下:

点击新建窗体:
层叠排列:

水平平铺:

垂直平铺:

文件类控件
选择类控件
分组类控件
菜单栏、工具栏和状态栏控件
边栏推荐
- R语言GLM广义线性模型:逻辑回归、泊松回归拟合小鼠临床试验数据(剂量和反应)示例和自测题
- 行业分析| 快对讲,楼宇对讲
- JVM memory structure
- Intelligent computing system 1 environment construction
- 请问下flinkcdc用flinksql提交的话只能一个表提交一个任务吗?那有几千张表的时候还能这么
- Upward and downward transformation
- 智能计算系统2 bangc算子开发的demo (CPU和MLU270的异构编程流程)
- How to change the status bar at the bottom of win11 to black? How to change the status bar at the bottom of win11 to black
- 图神经网络入门 (GNN, GCN)
- Nanopc-t4 (rk3399) Game1 OLED (I2C) display time weather temperature
猜你喜欢

How does redis implement inventory deduction? How to prevent oversold?

R language GLM generalized linear model: logistic regression, Poisson regression fitting mouse clinical trial data (dose and response) examples and self-test questions

The first meta universe concept novel, meta universe 2086, won the upper attack meta universe award in 2022

秋招经验分享 | 银行笔面试该怎么准备

Parallax JS special effect JS carousel map plug-in

Variable autoencoders (vaes)

How many objects are created after new string ("hello")?

为什么C语言用使用其他文件的结构体变量时,声明结构体别名还不行,必须使用本名?(使用别名时不能加struct)

pd. Difference between before and after cut interval parameter setting

Month on month SQL implementation
随机推荐
集合之ArrayList
How to resolve kernel errors? Solution to kernel error of win11 system
100人成绩的平均
MDM data analysis function description
我是刚购买的adb mysql服务,我每做一个操作,比如建表,都会弹出这个问题,请问这是什么问题?
Parallax JS special effect JS carousel map plug-in
Paper 3 vscode & texlive & sumatrapdf create a perfect tool for writing papers
腾讯汤道生:面向数实融合新世界,开发者是最重要的“建筑师”
Show the actual work case of creating intermediate data table with SQL
I just bought the ADB MySQL service. Every time I do an operation, such as creating a table, this problem will pop up. What is the problem?
Ffmpeg usage in video compression processing
How to learn JS through w3school / how to use the JS reference manual of w3school
Jenkins Pipeline 对Job参数的处理
Grafana biaxial graph with your hands
释放互联网价值的 Web3
The first meta universe concept novel, meta universe 2086, won the upper attack meta universe award in 2022
阿里开源(EasyExcel)
Demo of intelligent computing system 2 bangc operator development (heterogeneous programming flow of CPU and mlu270)
C#连接数据库完成增删改查操作
Ffmpeg learning summary