当前位置:网站首页>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()
边栏推荐
- SwiftUI 组件大全之使用 SceneKit 和 SwiftUI 构建交互式 3D 饼图(教程含源码)
- d. LDC build shared library
- leetcode:871. Minimum refueling times [Pat has done before + maximum stacking + greed]
- Excel removes the data after the decimal point and rounds the number
- 【C语言】指针与数组笔试题详解
- The meaning of wildcard, patsubst and notdir in makefile
- Type expansion of non ts/js file modules
- MySQL basics 03 introduction to MySQL types
- LDC Build Shared Library
- Button wizard play strange learning - automatic return to the city route judgment
猜你喜欢
Database SQL language 02 connection query
MySQL
Leetcode 6103 - minimum fraction to delete an edge from the tree
Meituan dynamic thread pool practice ideas, open source
MySQL --- 数据库查询 - 条件查询
强化学习 Q-learning 实例详解
【无标题】
leetcode:701. Insertion in binary search tree [BST insertion]
电话网络问题
matlab 多普勒效应产生振动信号和处理
随机推荐
Tp6 fast installation uses mongodb to add, delete, modify and check
wirehark数据分析与取证A.pacapng
一比特苦逼程序員的找工作經曆
MySQL - database query - condition query
Daily topic: movement of haystack
MySQL basic usage 02
[C language] detailed explanation of pointer and array written test questions
The R language uses the ctree function in the party package to build conditional inference decision trees, uses the plot function to visualize the trained conditional inference decision tree, and the
leetcode 6103 — 从树中删除边的最小分数
Button wizard play strange learning - go back to the city to buy medicine and add blood
Kivy教程大全之如何在 Kivy 中创建下拉列表
Create your first Kivy program Hello word (tutorial includes source code)
Correctly distinguish the similarities and differences among API, rest API, restful API and web service
[fh-gfsk] fh-gfsk signal analysis and blind demodulation research
leetcode:871. Minimum refueling times [Pat has done before + maximum stacking + greed]
Key wizard play strange learning - multithreaded background coordinate recognition
Basis of information entropy
MySQL --- 数据库查询 - 条件查询
Androd gradle's substitution of its use module dependency
What operations need attention in the spot gold investment market?