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:项目资源文件(推荐)

  1. 在 Properties/Resources.resx 中添加图片
  2. 通过 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);

更多推荐