当前位置:网站首页>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()
边栏推荐
- 不登陆或者登录解决oracle数据库账号被锁定。
- Arduino DY-SV17F自动语音播报
- 力扣 204. 计数质数
- MySQL --- 数据库查询 - 条件查询
- 按键精灵打怪学习-自动回城路线的判断
- 【FH-GFSK】FH-GFSK信号分析与盲解调研究
- Draw love with go+ to express love to her beloved
- leetcode:871. Minimum refueling times [Pat has done before + maximum stacking + greed]
- Dotconnect for PostgreSQL data provider
- MySQL foundation 05 DML language
猜你喜欢
基本远程连接工具Xshell
leetcode:701. Insertion in binary search tree [BST insertion]
Assets, vulnerabilities, threats and events of the four elements of safe operation
Niu Ke swipes questions and clocks in
JS inheritance and prototype chain
Expérience de recherche d'emploi d'un programmeur difficile
MySQL basics 03 introduction to MySQL types
[androd] module dependency replacement of gradle's usage skills
How wide does the dual inline for bread board need?
Asynchronous, email and scheduled tasks
随机推荐
Embrace the safety concept of platform delivery
基本远程连接工具Xshell
[FPGA tutorial case 6] design and implementation of dual port RAM based on vivado core
2022 Jiangxi Provincial Safety Officer B certificate reexamination examination and Jiangxi Provincial Safety Officer B certificate simulation examination question bank
【无标题】
Tp6 fast installation uses mongodb to add, delete, modify and check
Do not log in or log in to solve the problem that the Oracle database account is locked.
【FPGA教程案例5】基于vivado核的ROM设计与实现
测试右移:线上质量监控 ELK 实战
JS inheritance and prototype chain
有向图的强连通分量
每日一题之干草堆的移动
Button wizard play strange learning - automatic return to the city route judgment
按键精灵打怪学习-前台和内网发送后台验证码
Delete duplicate elements in the ordered linked list -ii
【我的OpenGL学习进阶之旅】关于欧拉角、旋转顺序、旋转矩阵、四元数等知识的整理
Matlab finds the position of a row or column in the matrix
Find a benchmark comrade in arms | a million level real-time data platform, which can be used for free for life
Create your first Kivy program Hello word (tutorial includes source code)
Now that the teenager has returned, the world's fireworks are the most soothing and ordinary people return to work~