C# WinForm GUI之WinForm基础
Windows窗体(Windows Form)。
WinForm,可视界面,显示信息,窗体和控件接收用户操作(鼠标点击,输入等),生成事件,并作出反应(事件发生时进行相应处理)。
创建WinForm
窗体(Form)是存放控件(TextBox、Button、Label等)的容器,显示信息。C#桌面Windows应用程序可由多个窗体构成。
- Visual Studio 2019创建窗体
创建C# Windows窗体应用(.NET Framework)项目,添加窗体(Windows窗体)。
- 文本编辑器创建窗体
MyForm.cs
using System;
using System.Windows.Forms;
class MyForm : System.Windows.Forms.Form{
MyForm(){
this.Text = "记事本";
}
static void Main(){
Application.Run(new MyForm());
}
}
##-t:winexe生成gui程序,无命令行窗口
csc MyForm.cs -t:winexe
窗体的基本操作
- 创建新窗体
vs2019工具箱添加各种控件。 - 设置属性
属性窗口设置控件属性。 - 编写代码
双击Button控件触发Button的Click事件,编写代码。
或者先编写代码,控件事件属性中去设置。 - 运行项目
F5运行。 - 窗体交互
窗体间传递数据及相互调用。
Form1.Designer.cs
namespace WindowsFormsApp1
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要修改
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(297, 218);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(171, 48);
this.button1.TabIndex = 0;
this.button1.Text = "跳转到Form2";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(297, 138);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(171, 25);
this.textBox1.TabIndex = 1;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
}
}
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();
}
public static string str;//用于窗体间传递
private void button1_Click(object sender, EventArgs e)
{
str = textBox1.Text;
Form2 f2 = new Form2();
f2.Show();
this.Hide();
}
}
}
Form2.Designer.cs
namespace WindowsFormsApp1
{
partial class Form2
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(337, 85);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(140, 25);
this.textBox1.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(337, 139);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(140, 41);
this.button1.TabIndex = 1;
this.button1.Text = "跳转到Form1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(337, 213);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(140, 46);
this.button2.TabIndex = 2;
this.button2.Text = "结束程序";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form2
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.Name = "Form2";
this.Text = "Form2";
this.Load += new System.EventHandler(this.Form2_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
}
}
Form2.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 Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
this.textBox1.Text = Form1.str;
}
private void button1_Click(object sender, EventArgs e)
{
Form1 f1 = new Form1();
f1.Show();
this.Close();
}
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
csc *.cs -t:winexe
控件的常用属性、方法和事件
- 控件焦点
控件具有焦点时,才能接收用户的输入。比如文本框具有焦点才能输入文字。
Enabled属性决定控件是否回应用户事件,Visible属性决定控件是否可见。
只有Enabled和Visible属性为true,才能接收焦点。
GroupBox、PictureBox、Timer等不能接收焦点。
Tab键能够转移焦点,TabIndex属性能够调整焦点顺序。
Form1.Designer.cs
namespace test7_2
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要修改
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(206, 268);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 3;
this.button1.Text = "计算";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(414, 284);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 4;
this.button2.Text = "退出";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(414, 72);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 25);
this.textBox1.TabIndex = 0;
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(423, 127);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(100, 25);
this.textBox2.TabIndex = 1;
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(423, 195);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(100, 25);
this.textBox3.TabIndex = 2;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(229, 72);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(45, 15);
this.label1.TabIndex = 11;
this.label1.Text = "加 数";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(232, 127);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(52, 15);
this.label2.TabIndex = 11;
this.label2.Text = "被加数";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(235, 182);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(45, 15);
this.label3.TabIndex = 11;
this.label3.Text = "结 果";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
}
}
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 test7_2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int str = Convert.ToInt32(textBox1.Text) + Convert.ToInt32(textBox2.Text);
textBox3.Text = Convert.ToString(str);
}
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace test7_2
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
csc *.cs -t:winexe
- 窗体常用属性
所有控件派生于Control类。
(1)name
唯一标识控件ID。
(2)Text
窗体标题栏显示文本,button、label等控件常用属性。
(3)BackColor和BackgroundImage
BackColor背景颜色;
BackgroundImage背景图片(根据大小平铺、拉伸、居中或缩放等)。
(4)Font、ForeColor、FormBorderStyle
Font显示文本字体设置,字体名、字号、样式等;
ForeColor文本和图形的默认前景色;
FormBorderStyle边框外观和样式,默认Sizeable。
(5)Icon
Icon指定System菜单和任务栏显示图标。
System.Drawing.Bitmap.FormFile(IconPath)
(6)MaximizeBox、MaximumSize、MinimizeBox、MinimizeSize
MaximizeBox指定启用或禁止System菜单和标题栏上“最大化”命令;
MaximumSize窗体最大大小,默认(0,0,)无限制;
MinimizeBox指定启用或禁止System菜单和标题栏上“最大化”命令;
MinimizeSize窗体最小大小。
(7)Size
初始大小,Width,Height。
(8)Enable
是否响应用户事件,默认true。
false则只能移动位置、调整大小、关闭或最小最大化,不能操作窗体内控件。
Form1.Enable=false;
(9)Visible
是否隐藏。
Form1.Visible=false;
- 常用方法和事件
(1)Show
public void Show();
public void Show(IWin32Windows owner);
//owner:任何实现IWin32Windows(表示拥有此窗体的顶级窗口)的对象。
显示窗体。
Form1 f1 = new Fom1();
f1.Show();
(2)Hide
隐藏窗体。
Form1 f1 = new Fom1();
f1.Hide();
(3)Close
关闭窗体。
Form1 f1 = new Fom1();
f1.Close();
(4)Click、DobuleClick
单击鼠标左键触发;
双击事件。
(5)Load
窗体加载时触发,窗体空白处双击生成。
private void Form1_Load(object sender, EventArgs e){
}
(6)FormClosing
窗体关闭时触发。
private void Form1_FormClosing(object sender, FormClosingEventArgs e){
}
(7)其他事件
vs2019事件属性窗口可查看。