c#软件开发学习笔记--winform窗体(窗体、事件、常用控件)
·
WinForm 学习总结文档
一、WinForm 基础概念
1.1 部分类(Partial Class)
WinForm 应用程序使用部分类(Partial Class)技术,将设计界面代码和逻辑代码分离到不同文件中:
// Form1.cs - 逻辑代码
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent(); // 调用设计器生成的初始化方法
}
}
// Form1.Designer.cs - 设计器生成的代码
partial class Form1
{
private void InitializeComponent()
{
// 控件初始化代码
}
}
核心要点:
- 编译时两个部分类合并为一个完整类
- 便于界面设计和业务逻辑的分工协作
二、窗体(Form)核心属性
2.1 基本属性
| 属性 | 说明 | 示例值 |
|---|---|---|
Name | 控件唯一标识,用于代码访问 | Form1 |
Text | 窗体标题栏文本 | "主界面" |
Icon | 窗体图标,仅支持 ICO 格式 | Properties.Resources.icon |
StartPosition | 窗体初始显示位置 | CenterScreen |
Location | 窗体左上角坐标 | new Point(100, 100) |
Size | 窗体尺寸 | new Size(800, 600) |
MaximizeBox | 最大化按钮是否可用 | true/false |
WindowState | 窗体状态 | Normal/Maximized/Minimized |
FormBorderStyle | 边框样式 | Sizable(可调整大小) |
2.2 外观属性
// 背景颜色
this.BackColor = Color.IndianRed;
// 背景图片设置
this.BackgroundImage = Properties.Resources.background;
this.BackgroundImageLayout = ImageLayout.Stretch; // 拉伸填充
背景图片布局模式:
Center:居中显示Tile:平铺填充(默认)Stretch:拉伸适应容器Zoom:保持比例缩放
2.3 字体设置
this.Font = new Font("楷体", 26.25F, FontStyle.Bold | FontStyle.Italic);
三、常用控件
3.1 Label 控件
作用:显示文本或图片
// 方式1:设计器拖放
this.label1.Text = "用户:";
this.label1.ForeColor = Color.White;
this.label1.BackColor = Color.FromArgb(255, 0, 0); // RGB+透明度
// 方式2:代码动态创建
Label l1 = new Label();
l1.Text = "good good study";
l1.Size = new Size(100, 20);
l1.Location = new Point(200, 0);
l1.TextAlign = ContentAlignment.MiddleCenter;
l1.Image = Properties.Resources.background; // 从资源文件加载图片
this.Controls.Add(l1);
Label 属性要点:
ImageAlign:图片对齐方式TextAlign:文本对齐方式AutoSize:自动调整大小
3.2 TextBox 控件
作用:接收用户输入
TextBox t1 = new TextBox()
{
Text = "你好",
Font = new Font("楷体", 20, FontStyle.Bold),
Size = new Size(300, 100),
Location = new Point(100, 300),
TextAlign = HorizontalAlignment.Center,
MaxLength = 10, // 最大输入长度
PasswordChar = '&', // 密码掩码字符
Multiline = true, // 多行模式
ScrollBars = ScrollBars.Both, // 滚动条
WordWrap = false // 不自动换行
};
t1.Clear(); // 清空文本
this.Controls.Add(t1);
TextBox 常用方法:
Focus():获取焦点Clear():清空内容
3.3 Button 控件
作用:触发点击事件
Button btn = new Button()
{
Text = "OK",
Location = new Point(30, 100),
Enabled = true // 是否可用
};
// 代码绑定点击事件(Lambda 表达式)
btn.Click += (sender, e) =>
{
MessageBox.Show("按钮被点击");
};
// 绑定到方法
btn.Click += button1_Click;
// 移除事件
btn.Click -= button1_Click;
this.Controls.Add(btn);
事件处理方法:
private void button1_Click(object sender, EventArgs e)
{
// sender 是触发事件的控件
Button clickedBtn = (Button)sender;
label1.Text = clickedBtn.Text;
}
四、事件处理机制
4.1 事件概念
事件是用户操作(如点击、输入等)的响应机制,WinForm 控件已预定义常用事件。
4.2 事件绑定方式
方式1:设计器双击控件自动生成
private void textBox1_TextChanged(object sender, EventArgs e)
{
Console.WriteLine("文本内容变化");
}
方式2:代码显式绑定
t1.TextChanged += (sender, e) =>
{
Console.WriteLine("内容改变");
};
4.3 自定义事件
// 定义委托
public delegate void MyDel(int a);
// 定义事件
public event MyDel Change;
// 绑定事件处理
Change += (a) =>
{
MessageBox.Show(a + " 触发事件");
};
// 触发事件
Change.Invoke(10);
五、窗体生命周期事件
5.1 常用生命周期事件
| 事件 | 触发时机 | 典型用途 |
|---|---|---|
Load | 窗体首次显示前 | 加载资源(图片、文件、数据) |
FormClosing | 窗体关闭过程中 | 询问保存、清理资源、移除事件绑定 |
5.2 事件示例
public Form1()
{
InitializeComponent();
MessageBox.Show("构造函数"); // 最先执行
}
private void Form1_Load(object sender, EventArgs e)
{
MessageBox.Show("Load事件"); // 窗体显示前执行
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
MessageBox.Show("窗体关闭事件"); // 关闭前执行
}
六、资源管理
6.1 图片资源导入
方式1:本地路径
// 绝对路径
l1.Image = Image.FromFile(@"C:\Users\Administrator\Desktop\img\pic.png");
// 相对路径(相对于 bin/Debug)
l1.Image = Image.FromFile("../../award_type_0.png");
方式2:项目资源文件(推荐)
- 在
Properties/Resources.resx中添加图片 - 通过
Properties.Resources访问:
l1.Image = Properties.Resources.background;
6.2 资源清理
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose(); // 释放组件资源
}
base.Dispose(disposing);
}
七、实用技巧
7.1 Color 颜色设置
// 方式1:使用预定义颜色
Color.Red;
Color.IndianRed;
// 方式2:RGB 值
Color.FromArgb(255, 0, 0); // 红色
// 方式3:带透明度的 RGB
Color.FromArgb(128, 255, 0, 0); // 半透明红色(A=128)
7.2 Point 和 Size
// 位置
Point location = new Point(x, y);
// 尺寸
Size size = new Size(width, height);
7.3 MessageBox 弹窗
MessageBox.Show("提示信息");
MessageBox.Show("标题", "提示", MessageBoxButtons.OKCancel);
更多推荐


所有评论(0)