当前位置:网站首页>C#应用程序界面开发基础——窗体控制(1)——Form窗体
C#应用程序界面开发基础——窗体控制(1)——Form窗体
2022-07-03 01:03: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()
边栏推荐
- 【系统分析师之路】第五章 复盘软件工程(开发模型开发方法)
- Excel removes the data after the decimal point and rounds the number
- MySQL foundation 07-dcl
- JS inheritance and prototype chain
- leetcode 2097 — 合法重新排列数对
- MySQL --- 数据库查询 - 条件查询
- 异步、郵件、定時三大任務
- Concise analysis of redis source code 11 - Main IO threads and redis 6.0 multi IO threads
- 一位苦逼程序员的找工作经历
- SwiftUI 组件大全之使用 SceneKit 和 SwiftUI 构建交互式 3D 饼图(教程含源码)
猜你喜欢

Leetcode 2097 - Legal rearrangement of pairs

Meituan dynamic thread pool practice ideas, open source

Using tensorboard to visualize the model, data and training process

How is the mask effect achieved in the LPL ban/pick selection stage?
![[flutter] icons component (fluttericon Download Icon | customize SVG icon to generate TTF font file | use the downloaded TTF icon file)](/img/ca/1d2473ae51c59b84864352eb17de94.jpg)
[flutter] icons component (fluttericon Download Icon | customize SVG icon to generate TTF font file | use the downloaded TTF icon file)

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

Soft exam information system project manager_ Real topic over the years_ Wrong question set in the second half of 2019_ Morning comprehensive knowledge question - Senior Information System Project Man

无向图的割点

Asynchronous, email and scheduled tasks

Androd gradle's substitution of its use module dependency
随机推荐
d. LDC build shared library
The latest analysis of tool fitter (technician) in 2022 and the test questions and analysis of tool fitter (technician)
用Go+绘制爱心给心爱的她表白
wirehark数据分析与取证A.pacapng
MongoDB系列之MongoDB常用命令
电话网络问题
Machine learning terminology
MySQL basics 03 introduction to MySQL types
MySQL
excel表格计算时间日期的差值,并转化为分钟数
[untitled]
异步、郵件、定時三大任務
【无标题】
2022 cable crane driver examination registration and cable crane driver certificate examination
Detailed explanation of Q-learning examples of reinforcement learning
The difference between tail -f, tail -f and tail
Leetcode 2097 - Legal rearrangement of pairs
有向图的强连通分量
Create your first Kivy program Hello word (tutorial includes source code)
Excel if formula determines whether the two columns are the same