C# WinForm GUI之WinForm基础

  • 创建WinForm
  • 窗体的基本操作
  • 控件的常用属性、方法和事件

Windows窗体(Windows Form)。
WinForm,可视界面,显示信息,窗体和控件接收用户操作(鼠标点击,输入等),生成事件,并作出反应(事件发生时进行相应处理)。

创建WinForm

窗体(Form)是存放控件(TextBox、Button、Label等)的容器,显示信息。C#桌面Windows应用程序可由多个窗体构成。

  1. Visual Studio 2019创建窗体

创建C# Windows窗体应用(.NET Framework)项目,添加窗体(Windows窗体)。

  1. 文本编辑器创建窗体

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

窗体的基本操作

  1. 创建新窗体
    vs2019工具箱添加各种控件。
  2. 设置属性
    属性窗口设置控件属性。
  3. 编写代码
    双击Button控件触发Button的Click事件,编写代码。
    或者先编写代码,控件事件属性中去设置。
  4. 运行项目
    F5运行。
  5. 窗体交互
    窗体间传递数据及相互调用。

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();}#endregionprivate 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();}#endregionprivate 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

控件的常用属性、方法和事件

  1. 控件焦点
    控件具有焦点时,才能接收用户的输入。比如文本框具有焦点才能输入文字。
    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();}#endregionprivate 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
  1. 窗体常用属性
    所有控件派生于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. 常用方法和事件

(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事件属性窗口可查看。

C# WinForm GUI之WinForm基础相关推荐

  1. Winform设计-小百货 涵盖基础插件学习(适合新手)

    Winform设计-小百货 涵盖基础插件学习(适合新手) 第一次写winform,主要是为了加快对  事件的 理解. 代码如下: private void Form1_Load(object send ...

  2. Winform UI框架 | Winform界面快速开发框架

    Winform UI框架 | Winform界面快速开发框架 什么是Winform? WinForm是.Net开发平台中对Windows Form的一种称谓,Windows窗体可用于设计窗体和可视控件 ...

  3. 大恒相机sdk二次开发 _c#从0开始新建winform窗体实现相机基础采集功能

    1.打开VS, 这里安装的是VS2019, 新建winform窗体. 2.打开水星驱动安装目录下的例程作为参考. 水星驱动安装包可以在大恒图像官网下载最新版本, 安装目录不要有中文路径, 安装之前把杀 ...

  4. winform使用DevExpress美化--基础

    一.基础类控件使用 !!!使用之前要添加LayoutConTrol控件 拖进来之后要调整好该控件的大小,该控件类似于HTML的DIV,要使用的其他控件都需要放到他上面(也可以理解为一个容器),才会有美 ...

  5. winform框架:winform老矣,尚能饭否

    之前由于需要,使用了c#的winform写了个桌面应用.后面我把 在此基础上把页面(UI).业务层(BLL) 和数据交互层(DAL)分离了形成了一个简单的框架,其中 封装了 弹窗 .http请求.通用 ...

  6. winform php 交互,WinForm开发,窗体显示和窗体传值

    以前对WinForm窗体显示和窗体间传值了解不是很清楚 最近做了一些WinForm开发,把用到的相关知识整理如下 A.WinForm中窗体显示 显示窗体可以有以下2种方法: Form.ShowDial ...

  7. 打印系统开发(15)——WinForm开发(43)——winform 使用打印机

    winform 使用打印机 工具箱里的pageSetupDialog ,printDialog,printDocument,printPreviewDialog 拖到屏幕上 用printDocumen ...

  8. 打印系统开发(24)——WinForm开发(45)——winform打印,自己设置打印纸张大小例如500*800px。应该怎么做呢?

    这是在论坛上看到的帖子,整理了一下.文章末尾给出论坛地址. ====================================================== winform打印,自己设置打 ...

  9. php winform通信,C# Winform 通过Socket实现客户端和服务端TCP通信

    操作界面如下: 1.声明Socket 第一个参数:寻址方式,第二个参数:传输数据的方式,第三个参数:通信协议 Socket socket = new Socket(AddressFamily.Inte ...

最新文章

  1. 微服务海量日志怎么处理,推荐你试试这款工具....
  2. 某资深互联网人分析:支付宝面临巨大困难,被微信争夺市场,年轻用户流失严重...
  3. input中v-model和value不能同时调用时解决方案
  4. newcoder ACM模式
  5. python打包成.exe程序
  6. ElasticSearch wildcard查询(英文检索)
  7. python学习笔记2018-9-18
  8. 养肝粥,用电脑过度人群必备! - 健康程序员,至尚生活!
  9. Active Record 数据库模式-增删改查操作
  10. 用matlab解系统框图,第10章MATLAB的控制系统数学建模课题.ppt
  11. 2021FME博客大赛 —— 利用FME实现三调地类图斑统计分析
  12. Qt 字符编码转换(UTF-8 转换为 GBK)
  13. c语言中文网GUI,golang gui介绍
  14. 山东青年政治学院计算机专业在哪个校区,山东青年政治学院位置
  15. 物联网产品的发展简介(二)【产品篇02】
  16. 蓝牙模块 HC08_两个STM32开发板无线通信
  17. Fabric.js 上划线、中划线(删除线)、下划线
  18. Proxyee-down 3.x的下载与安装
  19. Python3记录--个人常用函数及资源(持续更新)
  20. java元编程_一文读懂元编程

热门文章

  1. 开涛的博客 spring
  2. SAP Commerce Cloud Github 仓库管理规范
  3. python图像处理:核磁共振图像(3D)的缩放
  4. WAIC | 九章云极方磊:Hypernets——自动化机器学习的基础框架
  5. easypoi 实现多sheet导出excel
  6. 计算机上的aece代表什么意思,Myristicaceae是什么意思
  7. 互联网晚报 | 6月28日 星期二|​ QQ回应大规模账号被盗;iPhone 14系列新机最快8月初量产;微信表情符号写入判决...
  8. 第八章-数据处理的两个基本问题
  9. [转] ReactNative Animated动画详解
  10. 360众筹网_360众筹平台